Using Applescript to Select Dropdown Menu Item Containing String

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?

1 Answer

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.