Dynamic File Menu
Posted: 28 Dec 2013, 09:53
by BGM
Here is a little script that will load a file structure into a menu.
It's pretty good but not perfect. Everything works fine as long as the script discovers a file matching the filemask. If it finds such a file, it will recurse the folder's subdirectories for other matching files. But if it doesn't find one in the root directory that matches the filemask, then it won't look for more. I've spent hours trying to figure out how to remedy that, but to no avail. If anyone has a good idea, I'll update the script.
Spoiler
Here is a simple demo:Code: Select all
;Wednesday, July 10, 2013
;DynamicFileMenu.ahk
;by Brother Gabriel-Marie
;Version 1.2
;http://www.autohotkey.com/board/topic/95219-dynamicfilemenuahk/
;Include this script (see the demo) in your AutoHotkey project.
;Use it, abuse, do whatever you like with it. Just don't blame me if anything goes wrong.
;Just call the function and it will produce a menu of all the files and folders from the specified directory.
;There are two functions:
;Use menu_fromfiles if you want a menu that displays the folder and all its subfolders and the files within them. You can set the filemask parameter to limit what files get into the menu.
;Use menu_itempath to fetch the full path from the menu (put this in your sub that gets called as the menu's action).
;parameters (they are the same for both functions):
; submenuname - the code name you want to give your completed menu
; menutitle - the display name of the menu
; whatdir - the directory to load into the menu
; filemask="*" - mask for filtering what files to show. The default is just * - that is, all files.
; parentmenu="" - the name of the menu that you want to add this one to. Leave it blank if you want to add it manually. The default is blank. If you specify this parameter, the menu will get added in the position where the function was called.
; whatsub - the name of the sub that gets called when you click on the file entries in the menu (this is where you call menu_itempath)
menu_fromfiles(submenuname, menutitle, whatsub, whatdir, filemask="*", parentmenu="", folders=1){
menucount := 0
loop, %whatdir%\*, 1, 0
{
if(file_isfolder(A_LoopFileFullPath)){
if(folders){
menucount := menu_fromfiles(A_LoopFileFullPath, a_loopfilename, whatsub, A_LoopFileFullPath, filemask, submenuname, folders)
}
}else{
loop, %A_LoopFileDir%\%filemask%, 0, 0
{
menu, %submenuname%, add, %a_loopfilename%, %whatsub%
menucount++
}
}
}
if(parentmenu && menucount){
menu, %parentmenu%, add, %menutitle%, :%submenuname%
return menucount
}
}
;Use this function to fetch the full path from the menu
menu_itempath(whatmenu, whatdir){
if(a_thismenu = whatmenu){
endpath = %whatdir%\%a_thismenuitem%
return endpath
}else{
endpath = %a_thismenu%\%a_thismenuitem%
return endpath
}
}
;returns true if the item is a folder, false if is a file
;if you add a slash to the end of a filename, ahk will still recognize a folder, but not a file.
file_isfolder(whatfile){
lastchar := substr(whatfile, 0, 1) ;fetch the last character from the string
if(lastchar != "\")
whatfile := whatfile . "\"
if(fileexist(whatfile))
return true
}
Spoiler
Notes:Code: Select all
;Wednesday, July 10, 2013
;DynamicFileMenuDemo.ahk
;by Brother Gabriel-Marie
;Version 1.2
;http://www.autohotkey.com/board/topic/95219-dynamicfilemenuahk/
;Demo for DynamicFileMenu.ahk
;Use it, abuse, do whatever you like with it. Just don't blame me if anything goes wrong.
;Just call the function and it will produce a menu of all the files and folders from the specified directory.
;Please see the main script for the meaning of the parameters.
#SingleInstance force
#include DynamicFileMenu.ahk
;--------------DEMO-----------------------------------------------------
thisdir := "C:\temp"
if(!fileexist(thisdir)){
msgbox, Sorry, Man, that directory doesn't exist.
exit
}
;Display a folder with its immediate files that match the filemask
;if no files are found, no menu is created. If you have something else in the menu, then you don't need to test it before you show it.
RButton::
menu, thismenu, add, What's Up?, WHATSUP
menu_fromfiles("filelist", "File List", "SHOWPATH", thisdir, "*.txt", "thismenu", 0)
menu, thismenu, show
Return
;Display a folder with all its subfolders that have files matching the filemask
;if no other items are added to the menu, you need to test it first.
+RButton::
if(menu_fromfiles("filelist", "File List", "SHOWPATH", thisdir, "*.txt", "thismenu", 1))
menu, thismenu, show
else
msgbox, No files were found, George.
Return
;You want a way to exit the script, since you can't right-click in the sytem tray? Really?
^RButton::
exitapp
Return
SHOWPATH:
curpath := menu_itempath("filelist", thisdir)
msgbox, %curpath%
RETURN
WHATSUP:
msgbox, Why, Hello there!
RETURN
It's pretty good but not perfect. Everything works fine as long as the script discovers a file matching the filemask. If it finds such a file, it will recurse the folder's subdirectories for other matching files. But if it doesn't find one in the root directory that matches the filemask, then it won't look for more. I've spent hours trying to figure out how to remedy that, but to no avail. If anyone has a good idea, I'll update the script.