koro
Joined: 24 Sep 2006 Posts: 60
|
Posted: Sun Nov 26, 2006 5:44 pm Post subject: Nested and sorted folder structure for menus |
|
|
This little function adds the contents of a folder to a menu, and recursively all its subfolders, subsubfolders, etc in submenus , sorted by name in each submenu. It keeps track of the path to each folder and each program, so that you can call them when clicked as in the example below.
I hope it's useful to someone; to me it was
| Code: |
HIDDEN_MENU_EXTENSIONS = exe,lnk,bat,com,pif
; Example
CUSTOM_FOLDER = %A_Programs%
FolderAdd("CMenu", CUSTOM_FOLDER, "CustomMenuHandler")
Return
MButton::
Menu, CMenu, show
Return
CustomMenuHandler:
{
foo := %A_ThisMenu%_Path . "\" . %A_ThisMenu%_File_%A_ThisMenuItemPos%
Run, %foo%
} Return
FolderAdd(MenuName, Folder, Handler)
{
Global
Local i, FileList
%MenuName%_Path = %Folder%
Loop, %Folder%\*, 2
FileList = %FileList%%A_LoopFileName%`n
Sort, FileList
i=0
Loop, parse, FileList, `n
{
if A_LoopField = ; Ignore the blank item at the end of the list.
continue
i++
foo=%MenuName%_%i%
FolderAdd(foo, Folder . "\" . A_LoopField, Handler)
Menu, %MenuName%, Add, %A_LoopField%, :%MenuName%_%i%
}
Filelist=
Loop, %Folder%\*, 0
FileList = %FileList%%A_LoopFileName%`n
Sort, FileList
Loop, parse, FileList, `n
{
if A_LoopField =
continue
i++
foo:=FileExtension(A_LoopField)
If foo in %HIDDEN_MENU_EXTENSIONS%
StringTrimRight, foo, A_LoopField, StrLen(foo)+1
else
foo=%A_LoopField%
Menu, %MenuName%, Add, %foo%, %Handler%
%MenuName%_File_%i% = %A_LoopField%
}
If i=0
{
Menu, %MenuName%, Add, (Empty), %Handler%
Menu, %MenuName%, Disable, (Empty)
}
}
FileExtension(FileName)
{
StringGetPos, Ext, FileName, . , R
StringTrimLeft, Ext, FileName, Ext+1
Return Ext
}
|
|
|