title: AppleScript to Connect Bluetooth Device in Monterey (12.0.1)
body: <p>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.</p>
  <pre><code>-- Turn Bluetooth on
  tell application &quot;System Preferences&quot;
      activate
      reveal pane &quot;com.apple.preferences.Bluetooth&quot;
  end tell
  
  delay 1
  
  tell application &quot;System Events&quot;
      tell process &quot;System Preferences&quot;
          if button &quot;Turn Bluetooth On&quot; of window &quot;Bluetooth&quot; exists then
              click button &quot;Turn Bluetooth On&quot; of window &quot;Bluetooth&quot;
          end if
      end tell
  end tell
  
  -- Connect Living Room speakers
  tell application &quot;System Events&quot;
      tell process &quot;ControlCenter&quot;
          
          set BluetoothButton to menu bar item &quot;Bluetooth&quot; of menu bar 1
          click BluetoothButton --This works and makes the dialog pop up
          
          delay 3
          
          set TheCheckbox to button &quot;Living Room&quot; of scroll area 1 of window &quot;Control Centre&quot;
  
          
          if TheCheckbox exists then return &quot;Yay&quot;
          
          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) &amp; &quot;PreferencePanes:&quot;
  tell application &quot;System Preferences&quot;
      activate
      reveal pane &quot;com.apple.preferences.Bluetooth&quot;
  end tell
  
  delay 1
  
  tell application &quot;System Events&quot;
      tell process &quot;System Preferences&quot;
          if button &quot;Turn Bluetooth On&quot; of window &quot;Bluetooth&quot; exists then
              click button &quot;Turn Bluetooth On&quot; of window &quot;Bluetooth&quot;
          end if
          
          delay 1
          
          set LivingRoomButton to UI element &quot;Living Room&quot; of row 3 of table 1 of scroll area 1 of window &quot;Bluetooth&quot; of application process &quot;System Preferences&quot; of application &quot;System Events&quot;
          --if LivingRoomButton exists then
          click LivingRoomButton -- Needs to be a double click but that's impossible
          --end if
      end tell
  end tell
  *)
  </code></pre>
  <p>If I</p>
  <pre><code>return every UI element of window &quot;Control Centre&quot;
  </code></pre>
  <p>I get {} back, which makes me think window &quot;Control Centre&quot; 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.</p>
  <p>As it stands I get the error &quot;System Events got an error: Can’t get scroll area 1 of window &quot;Control Centre&quot; of process &quot;ControlCenter&quot;. Invalid index.&quot; on the line</p>
  <pre><code>set TheCheckbox to button &quot;Living Room&quot; of scroll area 1 of window &quot;Control Centre&quot;
  </code></pre>