Last Updated: December 16, 2021
· erebusbat

AppleScript to Connect BlueTooth Headphones

I use my bluetooth headphones, alot. I also pair/repair them depending on which device I am using at the moment. Although my headphones (SX-991) support dual pairing I have found it much more reliable to just re-pair them for the task at hand.

OSX actually handles this very well and I do not have to go through the entire BT pairing process each time on my mac. I just toggle pairing mode on my headphones and then attempt to connect to them from OSX and everything magically works.

This was working on macOS Sierra 10.12.3 as of Mar-2017, although you may need to grant access to whatever method you use to run this in the security preferences panel. Google it you can't miss it.

All you should have to do is change the "SX-991" string to match the name of your device and you should be set.

The only caveat I have found is that now in macOS Sierra it is damn near impossible to give osascript accessibility rights. To work around this I just export an app from Script Editor

activate application "SystemUIServer"
tell application "System Events"
  tell process "SystemUIServer"
    -- Working CONNECT Script.  Goes through the following:
    -- Clicks on Bluetooth Menu (OSX Top Menu Bar)
    --    => Clicks on SX-991 Item
    --      => Clicks on Connect Item
    set btMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
    tell btMenu
      tell (menu item "SX-991" of menu 1)
        if exists menu item "Connect" of menu 1 then
          click menu item "Connect" of menu 1
          return "Connecting..."
          click btMenu -- Close main BT drop down if Connect wasn't present
          return "Connect menu was not found, are you already connected?"
        end if
      end tell
    end tell
  end tell
end tell

9 Responses
Add your response

Sometimes this works, sometimes it doesn't! I often get:

error "System Events got an error: Can’t get menu 1 of menu bar item 5 of menu bar 1 of application process \"SystemUIServer\". Invalid index." number -1719 from menu 1 of menu bar item 5 of menu bar 1 of application process "SystemUIServer"

over 1 year ago ·

Doesn't work anymore in el capitan. Seems they removed menu bar from the properties of that app.

over 1 year ago ·

FYI I just updated this for macOS Sierra

over 1 year ago ·

This works great for normal headphones (👍🏻 thanks!) but it seems Apple's AirPods act differently (of course). When I run it with the Airpods' name, I get

System Events got an error: Can’t get menu item "Andy's AirPods" of menu 1 of menu bar item 5 of menu bar 1 of application process "SystemUIServer".

as if it doesn't exist. Any suggestions?

over 1 year ago ·

hahah it's the apostrophe. Apple uses

whereas, the apostrophe key on my keyboard uses


just in case anyone is interested.

over 1 year ago ·

Works well in High Sierra, with the exception of this:

click btMenu -- Close main BT drop down if Connect wasn't present

For whatever reason, it doesn't close the BT menu. However, there is another method that seems to work:

key code 53
over 1 year ago ·

This isn't working for me in El Capitan. For those that have it working, can you post your script and the version of OSX you're running? Thanks!

over 1 year ago ·

Looks like my issue was caused by running Bartender 1. It does some weird restructuring of the menu items. I ended up using for my Alfred Workflow.

over 1 year ago ·

If you run into this error: "Can’t get «class mbri» 1 of «class mbar» 1 of «class prcs» "SystemUIServer" of application "System Events" whose description contains "bluetooth". Invalid index.", open up bluetooth settings and check the box that says "Show Bluetooth in menu bar". This fixed the error for me.

over 1 year ago ·