fyfp0w
Last Updated: May 17, 2017
·
2.639K
· erebusbat
62c329f4dbd904bbff9ac34fbc0f53f6

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
      click
      tell (menu item "SX-991" of menu 1)
        click
        if exists menu item "Connect" of menu 1 then
          click menu item "Connect" of menu 1
          return "Connecting..."
        else
          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
Say Thanks
Respond

6 Responses
Add your response

18793
Default profile 6 normal

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 ·
24844
021drv6a normal

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

over 1 year ago ·
28751
62c329f4dbd904bbff9ac34fbc0f53f6

FYI I just updated this for macOS Sierra

8 months ago ·
28868

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?

7 months ago ·
28871

hahah it's the apostrophe. Apple uses

whereas, the apostrophe key on my keyboard uses

'

just in case anyone is interested.

7 months ago ·
28933

@agsmith87 you're a lifesaver.

7 months ago ·