Error checking is minimal; functionality also: it's bare bones but it does the job. I would like to get rid of the ActivateMenu stuff, as that's a clumsy way of activating the correct menu; however I am not sure how. I would need to be able to connect a hotkey, not with a label, but with an action, in this case a Menu, ..., Show command. That would be a nice addition.
Comments are welcome, especially about getting the thing smaller or more efficient: I am new to AutoHotkey's script language, so if you spot something dreadful tell me. If OTOH you like the script, tell your neighbour.
Suggestions are welcome as well; perhaps this can be coaxed into doing more than just sending keys. I might put in a bit of work if it seems worthwhile.
The script 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 and also menu entries and keys to send: no tabs, no fun.
If you use the Filemenu.txt as it is shown further down, have a window active that accepts more than one line of input before you press #1 and pick a menu option.
--- cut here for Filemenu.ahk ---
;Filemenu.ahk k=0 Loop, Read, Filemenu.txt { szLine=%A_LoopReadLine% If (szLine="") Continue If (InStr(szLine,"menu:")=1) ; 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 k:=k+1 } Else { szEntry2= StringSplit, szEntry, szLine, %A_Tab% If (szEntry2="") szEntry2=%szEntry1% If (szEntry1="-") Menu, %szName%, Add Else { Menu, %szName%, Add, %szEntry1%, FilemenuHandler ; stores the keys to send in an array composed of the menu base name and the index of the entry sz%szName%_%j%=%szEntry2% } j:=j+1 } } Exit ; Handles a menu choice FilemenuHandler: szSend=sz%A_ThisMenu%_%A_ThisMenuItemPos% ; get keys to send Send, % %szSend% 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--- cut here ---
A filemenu file looks like this (\t is a literal tab):
menu:menuname\thotkey to activate
menustring1\tkeys to send1
menustring2\tkeys to send2
...
There can be many "menu:" sections in a filemenu file. Empty lines are ignored; a single dash (-) marks a separator.
The following would be a trivial example for a valid filemenu.txt (the tabs may get lost, so you have to put them back in)
--- cut here for Filemenu.txt---
menu: Testmenu #1 test1 here is test1 test2 here is test2{enter}and a new line - test3 here is test3{tab}and more test4 here is test4{BS 13}and by now it's getting boring--- cut here ---