Search code examples
macosapplescriptautomator

Using AppleScript to select dropdown menu item containing string


The following code selects the menu item of a dropdown displaying the string Mac mini Speakers:

click (menu item "Mac mini Speakers") of menu 1 of popupButton

But I want to select the menu item by substring. In my use case I don't know the full name. So something like this (but this does not work):

click (menu item where value contains "Speakers") of menu 1 of popupButton

How can I specify a menu item by a substring of the content displayed in the menu item?


Solution

  • Testing under macOS Catalina in Script Editor with System Preferences > Sound > Sound Effects opened, the following example AppleScript code clicks the MacBook Pro Speakers menu item for the Play sound effects through: pop-up menu:

    tell application "System Events"
        tell application process "System Preferences"
            click pop up button 1 of tab group 1 of window 1
            delay 0.25
            tell tab group 1 of window 1
                click (menu items of menu 1 of pop up button 1 ¬
                    whose name contains "Speakers")
            end tell
        end tell
    end tell