Automating alternate menus with Squish for Mac

The menus on macOS allow for a menu item to have an alternate version: if you press the “Option” key with an open menu, you see the alternate versions and can select them. For example the Squish addressbook example app has a menu item “Bring All to Front” in the “Window” menu.

Normal "Window" menu with "Bring All to Front" menu item

The normal “Window” menu.

And if you press the “Option” key, then this changes to “Arrange in Front”.

The "Window" menu when "Option" key is pressed with "Arrange in Front" menu item

The alternate “Window” menu when the “Option” key is pressed.

Automating the activation of the “Arrange in Front” menu item is possible, but there is one small gotcha: when you record the activation of this menu by clicking the “Window” menu, pressing the “Option” key and then clicking on the “Arrange in Front” menu item, you end up with the following script:

# -*- coding: utf-8 -*-

def main():
    startApplication("SquishAddressBook")
    waitForObject(":Window_NSMenuItem")
    activateItem(":Window_NSMenuItem")
    waitForObject(":Arrange in Front_NSMenuItem")
    activateItem(":Arrange in Front_NSMenuItem")

If you play back this script, it fails with the error

LookupError: Object ':Arrange in Front_NSMenuItem' not found.

This is because Squish did not record the pressing of the “Option”. So let’s fix this by adding the necessary script statements for pressing and releasing the “Option” key (which is also labelled “Alt” and which is the name used in the Squish script):

# -*- coding: utf-8 -*-

def main():
    startApplication("SquishAddressBook")
    waitForObject(":Window_NSMenuItem")
    activateItem(":Window_NSMenuItem")
    keyPress("<Alt>")
    waitForObject(":Arrange in Front_NSMenuItem")
    activateItem(":Arrange in Front_NSMenuItem")
    keyRelease("<Alt>")

And if we play back this test now, Squish activates the alternate menu item as expected.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*