Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

DynamicFileMenu.ahk


  • Please log in to reply
1 reply to this topic
BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

DynamicFileMenu.ahk

Version 1.2

by BGM

updated: Wednesday, July 11, 2013

 

Include this script 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.

 

fg7r.png

 

There are three functions:

menu_fromfiles

menu_itempath

 

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.  This function will return the count for the number of files loaded 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
  • whatsub  - the name of the sub that gets called when you click on the file entries in the menu - this is where you put menu_itempath.
  • 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.

 

Demo:

;DynamicFileMenu.ahk DEMO
;by BGM
;http://www.autohotkey.com/board/topic/95219-dynamicfilemenuahk/

;Run the script, then right-click anywhere to display the menu
#SingleInstance force

thisdir := "C:\temp\"   ;for the demo, set this path to a folder that has other folders and text files within

;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

Here is the menu-making code:

;DynamicFileMenu.ahk
;by BGM
;http://www.autohotkey.com/board/topic/95219-dynamicfilemenuahk/

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
        }       
}

;fetches the correct 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
file_isfolder(whatfile){
    lastchar := substr(whatfile, 0, 1) ;fetch the last character from the string
    if(lastchar != "\")
        whatfile := whatfile . "\"
    if(fileexist(whatfile))
        return true
}

(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)


BGM
  • Members
  • 199 posts
  • Last active: Jun 05 2015 06:26 PM
  • Joined: 12 Jan 2011

I think the only thing lacking is a way to retrieve the correct path of the file.

The idea is that we don't need the entire path in the menu.

Any ideas?

 

...

okay, that is fixed now.


(AutoHotkey 1.1.15.00 on Windows 7 Pro x64)