Using FileSelectFolder for Windows File Explorer AutoHotkey Menus

For Precise Selection of Windows Folders When Loading Files into a Launching Menu, Use the AutoHotkey FileSelectFolder Command

Last time, I mentioned the possibility of using the FileSelectFile command to pick the top-level folder when adding files to an AutoHotkey action menu using the barebones FileMenuLoad.ahk script. Upon further consideration, I realized that the FileSelectFolder command offered a more appropriate solution. Accidentally picking a file rather than a folder for the top-level—although workable—only introduces more complications. The FileSelectFolder command precludes the inadvertent selection of a file.

You can insert the command at the beginning of the script as follows:

^#!m::

FileSelectFolder, TopMenu, *C:\Users\%A_UserName%\QuickLinks

If errorlevel
	Return

LastMenu := TopMenu

Loop Files, %TopMenu%\*.*, DFR  ; Recurse into subfolders.
{
	; Skip any file that is H or S (System).
	If InStr(A_LoopFileAttrib, "H") or InStr(A_LoopFileAttrib, "S") 
		Continue
	; Add to Menu	
	Menu, %A_LoopFileDir%, Add, %A_LoopFileName% , Action
	If (A_LoopFileDir != LastMenu) and (LastMenu != TopMenu)
	{
;		MsgBox %TopMenu% %LastMenu%
		AddMenu(LastMenu)
	}
	; Save menu name
	LastMenu := A_LoopFileDir
}

AddMenu(LastMenu)

Menu, %TopMenu%, Show

Return


Action:

Run, % A_ThisMenu . "\" . A_ThisMenuItem

Return

AddMenu(MenuName)
{
	SplitPath, MenuName , DirName, OutDir, OutExtension, OutNameNoExt, OutDrive
	Menu, %OutDir%, Add, %DirName%, :%MenuName%
}

While the FileSelectFolder command works well enough, whether or not you use it depends upon what you want to accomplish. In most cases, you may find it easier to hardcode the target folder into your script.

I don’t think I would use this folder selection command to randomly search through the Window File Explorer structure looking for folders/subfolders to cram into a menu. The command works best when you use folders set up in a way that a menu makes natural sense (such a QuickLinks). Possibly, you have a number of folders grouped together for QuickLinks type action, but only want to make the selection at runtime.

Special Folder Starting Points

The FileSelectFolder command offers special codes for loading specific Windows folders:

FileSelectFolder, TopMenu, ::{20d04fe0-3aea-1069-a2d8-08002b30309d} , 0 ; My Computer
FileSelectFolder, TopMenu, ::{450d8fba-ad25-11d0-98a8-0800361b1103} , 0 ; My Documents
FileSelectFolder, TopMenu, ::{48e7caab-b918-4e58-a94d-505519c795dc}, 0 ; Start Menu Folder 

You can find these in the Windows Class Identifier List. This allows you to specify a standard starting location without entering the full path.

Tip: In order to activate roaming in higher-level folders, place an asterisk ( * ) in front of the default path:

FileSelectFolder, TopMenu, *::{20d04fe0-3aea-1069-a2d8-08002b30309d}, 0 ; My Computer
FileSelectFolder, TopMenu, *::{450d8fba-ad25-11d0-98a8-0800361b1103}, 0 ; My Documents
FileSelectFolder, TopMenu, *::{48e7caab-b918-4e58-a94d-505519c795dc}, 0 ; Start Menu Folder 

Play with the options to decide what works best for you.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s