Rather Than Increasing the Length of a System Tray Menu, Add Submenus—Plus, How to Use Menu Names (A_ThisMenu) for Conditional Actions
When I decided to add two more recipes to the
Jack Stuffed Cheeseburger script, I ran into the problem of adding too many items (one for each step in each recipe) to the System Tray right-click menu. In the original, Cheeseburger.ahk script, I only included four steps in the menu (Ingredients, Prepare, Cook, and Serve). However, after inserting the Animal-Style Cheeseburger and the Jack Stuffed Cheeseburger (Animal Style) into the script, the number of recipe increments jumped from four to 18—plus an additional Print Recipe option for each burger. Also, since many of the steps use the same (or similar names), I needed a method for identifying each step with the proper recipe. When increasing the number of AutoHotkey menu options, cumbersome lists of items commonly crop up. You can fix these bloated menus by using submenus.
The AutoHotkey Menu command offers the option to create submenus which pop-up when the mouse cursor hovers over the parent menu item. Rather than directly calling an action Label (subroutine), insert a colon (:) followed by the submenu name as shown in this example from the online AutoHotkey documentation:
Menu, MySubmenu, add, Item1 Menu, tray, add, This Menu Item Is A Submenu, :MySubmenu
Tip: To avoid a runtime error, the parent menu line of code which calls the submenu must appear in the script after at least one of the submenu items.
I pack all 21 of the recipe steps into three recipe menus by creating submenus:
Menu, Cheeseburger, add, Ingredients, Ingredients Menu, Cheeseburger, add, Prepare, Prepare Menu, Cheeseburger, add, Cook, Cook Menu, Cheeseburger, add, Serve, Serve Menu, Cheeseburger, add, Print Recipe, Print Menu, tray, add, Jack Stuffed Cheeseburger, :Cheeseburger Menu, Animal, add, Ingredients, Animal_Ingredients Menu, Animal, add, Onions, Animal_Onions Menu, Animal, add, Sauce, Animal_Sauce Menu, Animal, add, Prepare, Animal_Prepare Menu, Animal, add, Buns, Animal_Buns Menu, Animal, add, Cook, Animal_Cook Menu, Animal, add, Serve, Animal_Serve Menu, Animal, add, Print Recipe, Print Menu, tray, add, Animal Style Cheeseburger, :Animal Menu, StuffedAnimal, add, Ingredients, Stuffed_Animal_Ingredients Menu, StuffedAnimal, add, Onions, Animal_Onions Menu, StuffedAnimal, add, Sauce, Animal_Sauce Menu, StuffedAnimal, add, Prepare, Prepare Menu, StuffedAnimal, add, Buns, Animal_Buns Menu, StuffedAnimal, add, Cook,Stuffed_Animal_Cook Menu, StuffedAnimal, add, Serve, Stuffed_Animal_Serve Menu, StuffedAnimal, add, Print Recipe, Print Menu, tray, add, Stuffed Animal Cheeseburger, :StuffedAnimal

When AutoHotkey creates the new menu, all of the submenu steps appear next to the called parent menu item whenever the mouse cursor hovers over it. In AutoHotkey, using submenus is one of the easiest ways to pack numerous options into a small space. In fact, you can add submenus to submenus creating a pyramid of as many options as an application may demand. This technique also solves the problem of identifying steps with identical names which call the same Label subroutine—such as the Print Recipe option. Using the parent menu name (A_MenuName) tells AutoHotkey which recipes to print.
Using Menu Names for Conditional Actions (or How to Print the Right Recipe)
I always look for the built-in variables in an AutoHotkey command. Those hidden gems often make my scripting life much easy. The Menu command offers three such variables (as shown in the online AutoHotkey documentation):
A_ThisMenuItem — The name of the most recently selected custom menu item (blank if none).
A_ThisMenu—The name of the menu from which A_ThisMenuItem was selected.
A_ThisMenuItemPos—A number indicating the current position of A_ThisMenuItem within A_ThisMenu. The first item in the menu is 1, the second is 2, and so on. Menu separator lines are counted. This variable is blank if A_ThisMenuItem is blank or no longer exists within A_ThisMenu. It is also blank if A_ThisMenu itself no longer exists.”
Each of the three Print Recipe options calls the same Label name Print subroutine. By identifying the menu clicked, AutoHotkey knows which recipe to print:
Print: FileDelete, RecipePrint.txt If (A_ThisMenu = "Cheeseburger") { FileAppend , % "Jack Stuffed Cheeseburgers`r`r" . Ingredients . "`r`r" . Mix . "`r`r" . Cook . "`r`r" . Serve, RecipePrint.txt } If (A_ThisMenu = "Animal") { FileAppend , % "Animal Style Cheeseburgers`r`r" . Animal_Ingredients . "`r`r" . Animal_Onions . "`r`r" . Animal_Sauce . "`r`r" . Animal_Mix . "`r`r" . Animal_Buns . "`r`r" . Animal_Cook . "`r`r" . Animal_Serve, RecipePrint.txt } If (A_ThisMenu = "StuffedAnimal") { FileAppend , % "Jack Stuffed Cheeseburgers (Animal Style)`r`r" . Stuffed_Animal_Ingredients . "`r`r" . Animal_Onions . "`r`r" . Animal_Sauce . "`r`r" . Mix . "`r`r" . Animal_Buns . "`r`r" . Stuffed_Animal_Cook . "`r`r" . Stuffed_Animal_Serve , RecipePrint.txt } Run, Notepad /p RecipePrint.txt Return
Using the A_ThisMenu variable to identify the recipe reduces the amount of redundant code needed for the print routine.
* * *
Like anybody else, I have expenses and a need to make ends meet. As “Jack’s AutoHotkey Blog” increases in popularity, coding the test scripts and writing the blogs takes up more of my time. That means I’ve less time to pursue other income earning opportunities. I don’t plan to ever move “Jack’s AutoHotkey Blog” behind a paywall, but if you think my efforts are worth a bit of your hard-earned cash, then you can offer a token of your appreciation by purchasing some of my AutoHotkey books. You may not need the references yourself, but you might know someone who can benefit from one or two of them.
Thank you,