AppleScript to Connect Bluetooth Device in Monterey (12.0.1)

Viewed 593

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"
1 Answers

I don't have a Bluetooth speaker, but I use the following script to connect to my HomePod (Airplay). The HomePod shows up in the list under System Preferences -> Sound. If your Bluetooth speaker shows up in that list as well, this script should work on macOS 12 Monterey:

set outPutSrc to "Living Room"

tell application "System Preferences"
    
    reveal anchor "output" of pane id "com.apple.preference.sound"
    activate
    
    tell application "System Events"
        tell process "System Preferences"
            try
                delay 1
                select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is outPutSrc)
            end try
        end tell
    end tell
    
    quit
    
end tell
Related