This here's a function for building menus from paths. I know there's a script on the forum already, but this works better as a function.
Code:
FolderMenu(path, menu)
{
global
local f_FolderList, nextmenu, f_FileList, f_LenFileName, f_MenuItemName, varname, MenuName
If menu =
MenuName := String2Hex(path)
else
MenuName := menu
Loop, %path%\*, 2
f_FolderList = %f_FolderList%`n%A_LoopFileFullPath%
Sort, f_FolderList
Loop, parse, f_FolderList, `n
{
If A_LoopField =
continue
f_MenuItemName := FileShortName(A_LoopField)
nextmenu := FolderMenu(A_LoopField,"")
varname := MenuName . "path" . String2Hex(f_MenuItemName)
%varname% = %A_LoopField%
Menu, %MenuName%, add, %f_MenuItemName%, :%nextmenu%
}
Loop, %path%\*, 0
f_FileList = %f_FileList%`n%A_LoopFileFullPath%
Sort, f_FileList
Loop, parse, f_FileList, `n
{
If A_LoopField =
continue
f_MenuItemName := FileShortName(A_LoopField)
varname := MenuName . "path" . String2Hex(f_MenuItemName)
%varname% = %A_LoopField%
Menu, %MenuName%, add, %f_MenuItemName%, FolderMenuHandler
}
return MenuName
}
FolderMenuHandler:
If menu_RButton = true
{
Msgbox, Right Click
menu_RButton = false
}
varname := A_ThisMenu . "path" . String2Hex(A_ThisMenuItem)
cmd := %varname%
If cmd =
return
else
Run, %cmd%
return
FileShortName(path)
{
StringSplit, tokens, path, \/
last := tokens%tokens0%
StringSplit, half, last, .
If half1 =
return last
else
return half1
}
String2Hex(x) ; Convert a string to a huge hex number starting with X
{
StringLen Len, x
format = %A_FormatInteger%
SetFormat Integer, H
hex = X
Loop %Len%
{
Transform y, ASC, %x% ; ASCII code of 1st char, 15 < y < 256
StringTrimLeft y, y, 2 ; Remove leading 0x
hex = %hex%%y%
StringTrimLeft x, x, 1 ; Remove 1st char
}
SetFormat Integer, %format%
Return hex
}
I snagged the hex function from the list library by Laszlo Hars.
This function makes it really easy to insert paths onto your existing menus.
It recurses all subdirectories as well.
The first parameter is the path to build the menu from.
The second parameter can be the name of a menu to add the contents of the path to. This parameter can be blank, in which case the function generates a name. In all cases the function returns the menu name it used.
Here's an example of how to use it:
Code:
FolderMenu("C:\command\shortcuts\programs","TopMenu")
Menu, TopMenu, add, Opus, menu_opus
name := FolderMenu("C:\command\docs","")
Menu, TopMenu, add, Docs, :%name%
This produces a menu that looks like this:
Code:
Games >
Media >
Net >
Prog >
Tools >
Opus
Docs >
As you can see, the contents of the program directory (all folders) are added right on the menu, followed by a normal menu item, followed by the contents of the docs directory in a submenu. Very simple to use, yes? Selecting one of the items on the file menus will run it.
I'm currently trying to make right click open the context menu, but I'm not even going to bother with the c code I need for that until I get the right click part working, and I'm not sure ahk can manage it right now.