WARNING! TO RUN THIS YOU HAVE TO MAKE SURE TWO THINGS:
1. The file with the menu definitions MUST contain real tabs.
2. Versions prior to 1.0.36 of AHK have a subtle bug that makes this script unusable. Grab the current version if there's a problem
The code reads a file called Filemenu.txt, whose format is described further down. This file contains the menu definition(s); you can define as many menus as you want (or rather as you have hotkeys). Don't forget to separate menuname and hotkey with a real tab; the same's true for menu entries and command code and parameter: no tabs, no fun. (I know that's a rather poor choice but it makes the parsing of the menu file very straightforward. And it looks not half bad in an editor.)
If you use the Filemenu.txt as shown further down, have a window active that accepts more than one line of input before you press #1 and pick a menu option.
@Chris: I think you'll see why I wanted {wait} and {setkeydelay} as Send parameters: this would make it possible to use them in these menus. But I think I can figure out something in the sources as they're pretty well documented.
;Filemenu.ahk k=0 Loop, Read, Filemenu.txt { szLine=%A_LoopReadLine% If (szLine="") ; empty Continue If (InStr(szLine,";")=1) ; comment Continue If (InStr(szLine,"menu:")=1) ; start of new menu definition? { StringTrimLeft, szName, szLine, 5 ; get rid of menu: szMenus_%k%=%szName% ; store menu name and activation key in array szMenus_?, used later in ActivateMenu StringSplit, szEntry, szName, %A_Tab% szName=%szEntry1% ; stores the base name of the array for menu entries Hotkey %szEntry2%, ActivateMenu ; all filemenus go to ActivateMenu j=1 ; stores the index into the array for menu entries, 1 because we've got a new menu k:=k+1 } Else { ; a menu entry has 3 parts: what shows up in the menu, the command, and the paramenter szEntry3= StringSplit, szEntry, szLine, %A_Tab% If (szEntry1="-") ; separator Menu, %szName%, Add Else { Menu, %szName%, Add, %szEntry1%, FilemenuHandler ; add option to menu in szName ; store the command szCmd%szName%_%j%=%szEntry2% ; store the parameter If (szEntry3="") ; if empty use menu option itself szParm%szName%_%j%=%szEntry1% Else szParm%szName%_%j%=%szEntry3% } j:=j+1 } } Exit ; Handles a menu choice FilemenuHandler: szCmd:=szCmd%A_ThisMenu%_%A_ThisMenuItemPos% ; get command szParm:=szParm%A_ThisMenu%_%A_ThisMenuItemPos% ; get parameter If (szCmd="S") Send, %szParm% Else If (szCmd="R") Run, %szParm% Return ; Activates the filemenu linked to the incoming hotkey ActivateMenu: i=0 loop { If (i=k) Break StringSplit, szEntry, szMenus_%i%, %A_Tab% If (szEntry2=A_ThisHotkey) { Menu, %szEntry1%, Show Break } i:=i+1 } Return
Here is Filemenu.txt (the forum software eats the tabs, alas):
; comment ; A menu definition starts with "menu:" followed by the name (which is used ; internally and should contain no special characters) and the hotkey used ; to activate this specific menu. ; Important! Name and hotkey *have* to be separated by a literal tab (sorry ; for the poor choice but this makes the parsing code trivial) menu: TestSend #1 ; Next are the actual menu entries. Three parts, once again all separated by ; real tabs. The first part is the menu entry as it will appear when the menu ; is shown (& for shortcut is ok). ; The second part is a command code. Currently there only two codes: S and R, ; but more easily be defined. ; The third part is the parameter, the meat of the thing. If the command code ; is S, this string will be send to the active window via a Send command. ; If the command code is R, this string will be given to the Run command. ; If this is empty the menu entry will be used instead. test&1 S test1 is sent ; special strings like {enter} or {esc} are ok in the command parameter test&2 S here is test2{enter}and a new line ; separator - test&3 S here is test3{tab}and more test&4 S here is test4{BS 13}and by now it's getting boring ; empty parameter sends test5 test5 S menu: TestRun #2 ; web site &Go Google R http://www.google.com http://www.google.com R ; ditto with parameters &Search for AHK R http://www.google.com/search?q=AutoHotkey ; a program &Notepad R notepad.exe ; Explorer (or whatever handles directories) e&Xplore c:\ R c:\
Here is an identical version, without the comments. If you let your editor replace all instances of [[TAB]] with real tabs it should be a usable filemenu:
menu: TestSend[[TAB]]#1 test&1[[TAB]]S[[TAB]]test1 is sent test&2[[TAB]]S[[TAB]]here is test2{enter}and a new line - test&3[[TAB]]S[[TAB]]here is test3{tab}and more test&4[[TAB]]S[[TAB]]here is test4{BS 13}and by now it's getting boring test5[[TAB]]S menu: TestRun[[TAB]]#2 &Go Google[[TAB]]R[[TAB]]http://www.google.com http://www.google.com[[TAB]]R &Search for AHK[[TAB]]R[[TAB]]http://www.google.com/search?q=AutoHotkey &Notepad[[TAB]]R[[TAB]]notepad.exe e&Xplore c:\[[TAB]]R[[TAB]]c:\