Search code examples
applescript

Applescript Click item in system menu bar after opening it


I can open the bluetooth dropdown menu item with this code but I don't know how to actually click on any of the items in the menu.

tell application "System Events" to tell process "ControlCenter"
    click menu bar item "Bluetooth" of menu bar 1
end tell

What would be a command to click on something in the open bluetooth menu?


Solution

  • This following AppleScript code should accomplish what you are trying to achieve. Just replace the "Mac Pro" part of the code with the name of the item you want to click.

    tell application "System Events" to tell process "ControlCenter"
        click menu bar item "Bluetooth" of menu bar 1
        repeat until exists of checkbox 1 of scroll area 1 of window 1
            delay 0.1
        end repeat
        click checkbox "Mac Pro" of scroll area 1 of window 1
        key code 53 -- Press escape key
    end tell
    

    This following AppleScript code will return the names of the checkboxes so you can easily know your options to use in the first code.

    tell application "System Events" to tell process "ControlCenter"
        click menu bar item "Bluetooth" of menu bar 1
        repeat until exists of checkbox 1 of scroll area 1 of window 1
            delay 0.1
        end repeat
        set checkBoxNames to name of checkboxes of scroll area 1 of window 1
    end tell