A while back I wrote a series of articles on using AutoHotkey for Windows program automation. They appeared in the old ComputorEdge Magazine. I used Windows 10 Paint as the example program demonstrating a number of approaches to program control. I temporarily put the articles in a makeshift PDF for artists and intend to include them in a future book—along with a couple of other unpublished Windows program automation articles. I extracted some of those columns from the original ComputorEdge issues and combined them into one free PDF download: AutoHotkey for Artist.pdf. The links may be obsolete, but the info is still good!
When I came upon the WinMenuSelectItem command, I thought I had found the panacea for AutoHotkey automation. Unfortunately, the limitations of this command make this blog more about what it can’t do rather than what it can do. You ask, “Why talk about something you can’t do?” Good question.
I spent a good deal of time experimenting with WinMenuSelectItem to determine its usefulness. Maybe my insights will save you some learning curve.
While ineffective with most of the programs I tested, the WinMenuSelectItem command worked great for a few old-style apps using the standard File | Edit | Format | View | Help type menu bar—and when it works, it’s a dream.
Newer programs using Ribbon menus or other hidden types of navigation do not respond to WinMenuSelectItem. Windows 10 Paint completely ignores the command, therefore I could not offer a follow-up on my previous articles.
Many programs which appear to use the old style standard menu (e.g. LibreOffice and Paint.Net) do not respond. Certainly, most modern software won’t benefit from this menu picking command.
However, I did find a few apps which do work with WinMenSelectItem: Notepad, Notepad++, and IrfanView graphics viewer and editor. For those programs, I certainly recommend the command for any type of automation. You’ll find menu selection crazy easy.
Using WinMenuSelectItem
Do some testing to determine if you can use the WinMenuSelectItem command with a particular software package. The command uses either text menu names or menu position numbers. The simple test below involves opening the File menu, then selecting an item—in this case, Save. I put together this example Hotkey which works on a test script in Windows Notepad:
^#!p:: WinMenuSelectItem, menucommandtest.ahk, ,File, Save Return
When open in a Notepad window (even if minimized), this command saves the file menucommandtest.ahk. It works from anywhere—not just in the active target window. If you want to apply the command only to any active window, then use A (for active) rather than the name of the file:
^#!p:: WinMenuSelectItem, A, ,File, Save Return
As an example of a practical application, if used in conjunction with the CopyRef.ahk script, the following Hotkey opens the Save dialogue for an Untitled – Notepad window without activating it:
^#!p:: WinMenuSelectItem, Untitled - Notepad, ,File, Save Return
This allows you to assign a file name to the collected data without needing to jump to the Notepad collection window. If you want this Notepad Save feature, you can add it to the CopyRef.ahk (or any other) script as an extra file saving feature.
Using Menu Numbers
You might find times when using text won’t work (e.g. the menu items use images rather than text). In that case, you replace the text with the menu position (e.g. 1&, 2&, 3&…). For example, the previous example for saving a file in Notepad converts to:
^#!p:: WinMenuSelectItem, Untitled - Notepad, ,1&, 3& Return
The number one followed by an ampersand (1&) designates the first menu (File) while the number three followed by an ampersand (3&) designates the third menu item (Save) in the File menu. A test with menu position numbers might work for some programs when menu text doesn’t.
However, as a caution, when determining menu positions, the divider bars each count as one position—as shown in the image below taken from the IrfanView image viewer File menu.
I’ve considered writing a script which opens an image file in IrfanView, then resizes the image or adds a border before saving it and closing the app. The WinMenuSelectItem command makes that easier—at least in IrfanView. I’ll put that on the list.
While limited by the number of Windows programs which respond to the WinMenuSelectItem command, when used with apps sporting the traditional menu type, the command makes automating those program much easier. But test the command with the program you want to automate before investing too much time in WinMenuSelectItem.