AutoHotkey Tip of the Week: WinMenuSelectItem for Windows Automation

While It Works Only for Classic Style Windows Menubars, the WinMenuSelectItem Command Makes Changing App Settings Easy

CoverImage200
A Multitude of AutoHotkey Tips and Tricks

In Chapter 8.1.7 “Control Windows Program with the WinMenuSelectItem Command…Maybe” of the book Jack’s Motley Assortment of AutoHotkey Tips, I discussed the use of the WinMenuSelectItem command for Windows automation. While it has its limitations (it doesn’t operate with programs using ribbon-style menus), its power makes possible simple setup changes in apps such as Notepad, Notepad++, IrfanView image viewer/editor, and, most importantly, AutoHotkey GUI (Graphical User Interface) pop-up apps with menubars—such as one of my favorites,  CodeQuickTester by GeekDude. If you work with one of the compatible programs, then the command offers a number of advantages.

AutoHotkey WinMenuSelectItem Command Advantages

In the right situation, you can change program settings on-the-fly without leaving your current work window. The WinMenuSelectItem command:

AHKcoursead1

  1. Works on a compatible program without first activating the app window—operating in the background—even for minimized windows.
  2. Selects a specific menu (or submenu) item without using the SendInput command for navigation.
  3. In many cases, a single line of code can activate a program feature by executing any item in the menubar tree.

Note: While I wish AutoHotkey offered a command similar to WinMenuSelectItem for the more modern ribbon-style menu, I cover a number of other automation techniques that do work in the “Windows Automation Section” of Jack’s Motley Assortment of AutoHotkey Tips.

Setting the Capture/Screenshot Mode in IrfanView

I use IrfanView image viewer to capture screen/window shots because I can immediately resize, crop, add borders, or color correct an image without opening other graphic tools. However, to initiate the feature, I must first load IrfanView and select the option from a menu (OptionsCapture/Screenshot…).

IrfanView Screen Capture

As long as an IrfanView process window exists and I’ve SetTitleMatchMode to 2, I can do the selection with one line of code:

WinMenuSelectItem, IrfanView, , Options , Capture

In this case, AutoHotkey accesses the first IrfanView window it encounters (based upon their order on the Windows Desktop) and opens the “Capture Setup” window.

Irfanview Capture Setup

Sometimes, the new “Capture Setup” opens not-on-top. To help ensure it comes into view add:

WinActivate, Capture Setup
WinWaitActive, Capture Setup

Note: The loading of both the main app (if needed) and the child “Capture Setup” window can take a good deal of time. I probably should add a SplashTextOn/Off commands to give a positive indication of activity—a topic for next time.

SetTitleMatchMode Command

For the command to work as shown, you need to SetTitleMatchMode to 2. When set to 2, the SetTitleMatchMode command matches any part (substring) of a title containing the keyword:

SetTitleMatchMode, 2

By default in TitleMode 1, the window title match requires that the beginning of the window title coincides with the keywords. However, since I don’t care which open process window I use to activate the screen capture, I switch to TitleMode 2, where AutoHotkey only needs to match a substring within the title.

SetTitleMatchMode, 2
^!#y::
  WinMenuSelectItem, IrfanView, , Options , Capture
Return

As long as at least one IrfanView process window exists, the CTRL+WIN+ALT+Y Hotkey combination opens the “Capture Setup” window. But, what if I have not loaded IrfanView?

Note: You may need to show a little caution when opening other windows. For example, if you happen to open a Web page containing the same keyword (i.e. IrfanView) in its title, it could present a problem. This Hotkey routine may attempt to access an open tab displaying the IrfanView Web page—depending upon where it sits in the open window stack.

Conditional Loading of Programs

If you want to start the IrfanView screen capture feature but no open process window exists, use the following trap to launch a new window using the Run command:

If !WinExist("IrfanView")
{
  Run, C:\Program Files\IrfanView\i_view64.exe
  WinWaitActive, IrfanView
}

The exclamation point (!) tells AutoHotkey to only execute the routine if an IrfanView window does NOT exist.

Note: The RunWait command would not work in place of the WinWaitActive command (or WinWait command) since it forces the script to wait until the new window closes.

Automating the Start of the “Capture Setup” Window

Often, the Hotkey may open the “Capture Setup” window in the background without activating it. Since in its current form, we need to at least click the Start button to set the screen capture on, we add two lines (mentioned above) to ensure the window’s availability:

WinMenuSelectItem, IrfanView, , Options , Capture
WinActivate, Capture Setup
WinWaitActive, Capture Setup 

The WinWait command allows the subroutine to complete after loading the “Capture Setup” window. You only need the WinActivate command if you plan to manually make changes rather than totally automate the process as follows but leaving it in will only cause the window to temporarily pop-up before closing.

If we want to continue without any changes or the need to close the “Capture Setup” window? We merely add the ControlClick command:

WinMenuSelectItem, IrfanView, , Options , Capture
WinWaitActive, Capture Setup
ControlClick, Button22, Capture Setup

Use the Window Spy app (right-click on a running AutoHotkey script icon in the System Tray) to determine the target button number.

The IrfanView Set Screen Capture Script

The following represents the entire screen capture script in context:

SetTitleMatchMode, 2
^#!y::
  If !WinExist("IrfanView")
  {
	Run, C:\Program Files\IrfanView\i_view64.exe
	WinWaitActive, IrfanView
  }
  WinMenuSelectItem, IrfanView, , Options , Capture
  WinActivate, Capture Setup
  WinWaitActive, Capture Setup
  ControlClick, Button22, Capture Setup
Return

You can easily modify this script to suit your situations.

For one more example, resetting an AutoHotkey GUI feature without using the menubar.

Toggling AutoComplete in CodeQuickTester

As I mentioned previously, one of my new favorite AutoHotkey apps,  CodeQuickTester by GeekDude, makes it easy to “Save Time When Testing and Modifying Scripts.” However, the AutoComplete feature often causes problems when editing text by randomly swapping letters—usually when I type too fast. (Hard to believe I could ever type too fast!) I can eliminate this problem by toggling off the built-in AutoComplete feature. The following one-line AutoHotkey command does the job for me:

^#!a::WinMenuSelectItem, CodeQuickTester, , Tools , AutoComplete

Use it again and it turns AutoComplete on again.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

One thought on “AutoHotkey Tip of the Week: WinMenuSelectItem for Windows Automation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s