Search code examples
user-interfaceapplescriptimessage

Selecting Pop Up Menu Buttons in AppleScript


I want to automate clicking a specific pop down menu's item. For Example, I want to change the Value of "Message receive Sound" to something else. How can I do this with AppleScript? And how can I do this with other pop down menus in AppleScript?

(To open the iMessage Settings menu, shown in the image, type CMD COMMA, once you open iMessage)

Note: I have successfully done this Automator, I just want to do it in applescript.

enter image description here


Solution

  • It's called GUI scripting. You have to identify the reference to the UI element(s).

    GUI scripting strongly depends on the system version. If an update changes the UI structure the script will break.

    This selects the sound "Popcorn" in the sound popup menu. It's for El Capitan. In systems prior to 10.11 the UI elements may be different and the process name might be "iChat"

    tell application "System Events"
        tell process "Messages"
            set frontmost to true
            if not (exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")) then
                keystroke "," using command down
                repeat until exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
                    delay 0.1
                end repeat
            end if
            tell (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
                tell pop up button 4 of group 1
                    click
                    delay 0.2
                    click menu item "Popcorn" of menu 1
                end tell
            end tell
        end tell
    end tell