Search code examples
macosbluetoothapplescriptmacos-monterey

AppleScript to Connect Bluetooth Device in Monterey (12.0.1)


I want to write a simple script to connect my MacBook to the bluetooth speaker in my living room, but I can't figure out the final click.

-- Turn Bluetooth on
tell application "System Preferences"
    activate
    reveal pane "com.apple.preferences.Bluetooth"
end tell

delay 1

tell application "System Events"
    tell process "System Preferences"
        if button "Turn Bluetooth On" of window "Bluetooth" exists then
            click button "Turn Bluetooth On" of window "Bluetooth"
        end if
    end tell
end tell

-- Connect Living Room speakers
tell application "System Events"
    tell process "ControlCenter"
        
        set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
        click BluetoothButton --This works and makes the dialog pop up
        
        delay 3
        
        set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"

        
        if TheCheckbox exists then return "Yay"
        
        if value of TheCheckbox is 0 then click TheCheckbox
    end tell
end tell

-- This is how to do it through the System Preferences
-- But this doesn't work because there's no way to double click the device in the bluetooth menu
(*set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"
tell application "System Preferences"
    activate
    reveal pane "com.apple.preferences.Bluetooth"
end tell

delay 1

tell application "System Events"
    tell process "System Preferences"
        if button "Turn Bluetooth On" of window "Bluetooth" exists then
            click button "Turn Bluetooth On" of window "Bluetooth"
        end if
        
        delay 1
        
        set LivingRoomButton to UI element "Living Room" of row 3 of table 1 of scroll area 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
        --if LivingRoomButton exists then
        click LivingRoomButton -- Needs to be a double click but that's impossible
        --end if
    end tell
end tell
*)

If I

return every UI element of window "Control Centre"

I get {} back, which makes me think window "Control Centre" isn't the right place to be looking for the button, but using an application called UI Browser, that definitely seems like where it lives. So I don't know what to do.

As it stands I get the error "System Events got an error: Can’t get scroll area 1 of window "Control Centre" of process "ControlCenter". Invalid index." on the line

set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"

Solution

  • This is an old question, but the 1st result in a search so I'll answer here. This works for me in 12.6:

    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
        
        repeat with uiElem in checkboxes of scroll area 1 of window 1 as list
            if name of uiElem = "Your_device_name_here" then
                click uiElem
            end if
        end repeat
        
        key code 53 -- # escape key to close the window
    end tell