9

I'm trying to figure out a way to connect to my iPhone via Bluetooth with a shell script. I'm currently using an applescript which essentially does this through UIElements, but I'm wondering if this can be done with a command line utility, a.l.a. blueutil but with the ability to connect to the device not just turn on/off bluetooth? Thanks for the consideration.

-Afshin

asked Jul 8, 2013 at 5:41

8 Answers 8

11

This answer is very similar to @Wolph's answer; however after fighting other issues this is what I came up with. I like it better for two reasons: # It will not disconnect the device if it is already connected # Nice output from osascript

Just save it in a file and call osascript path/to/file.applescript

This was working on OSX Mavericks 10.9.2 as of 11-Apr-2014, although you may need to grant access to whatever method you use to run this in the security preferences panel. See this Apple KB: http://support.apple.com/kb/HT5914

All you should have to do is change the "LG HBS730" string to match the name of your device and you should be set. The returns are there so you get nice output from osascript.

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 LG HBS730 Item
 -- => Clicks on Connect Item
 set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
 tell btMenu
 click
 tell (menu item "LG HBS730" of menu 1)
 click
 if exists menu item "Connect" of menu 1
 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
answered Apr 11, 2014 at 17:23
Sign up to request clarification or add additional context in comments.

Comments

7

I had to change a bit to get Andrew Burns's answer to work for me in Yosemite; his code keeps giving me

connect-mouse.scpt:509:514: execution error: System Events got an error: Can’t get menu 1 of menu bar item 3 of menu bar 1 of application process "SystemUIServer". Invalid index. (-1719)

Seems like selecting the menu bar item that way lets you click on it, but not get to the menu, for some inscrutable applescript reason. This very similar code works for me:

tell application "System Events" to tell process "SystemUIServer"
 set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
 click bt
 tell (first menu item whose title is "The Device Name") of menu of bt
 click
 tell menu 1
 if exists menu item "Connect"
 click menu item "Connect"
 return "Connecting..."
 else
 click bt -- close main dropdown to clean up after ourselves
 return "No connect button; is it already connected?"
 end if
 end tell
 end tell
end tell

I don't know the difference between (first menu bar item whose description is "bluetooth") of menu bar 1 and (menu bar item 1 of menu bar 1 where description is "bluetooth"), but....

answered Apr 28, 2015 at 21:23

2 Comments

This does not work for me on High Sierra. The menu opens but it doesn't go into any of the devices.
One note, if your device name has an apostrophe in the name (for example, "Bob’s Airpods"), then you may need to use a non-standard character. It seems that devices that were configured on a phone are likely to use the U+2019 character, while our IDE's usually use U+0027. I copy/pasted the fancy apostrophe and then this worked fine. I'm using Alfred to trigger it and it only takes fractions of a second
5

Updating this for High Sierra 10.13.2, based on the answers provided by Andrew Burns and dougal.

set DeviceName to "LG HBS730"
tell application "System Events" to tell process "SystemUIServer"
 set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
 click bt
 if exists menu item DeviceName of menu of bt then
 tell (first menu item whose title is DeviceName) of menu of bt
 click
 tell menu 1
 if exists menu item "Connect" then
 click menu item "Connect"
 return "Connecting..."
 else
 key code 53 -- hit Escape to close BT menu
 return "No connect button; is it already connected?"
 end if
 end tell
 end tell
 else
 key code 53 -- hit Escape to close BT menu
 return "Cannot find that device, check the name"
 end if
end tell

Changes:

  • Clicking on the Bluetooth icon no longer closes the menu. Solved by hitting the Escape key, per this answer and confirmed on this page
  • Checking to see if the device is listed in Bluetooth menu, to detect incorrect names. (I spent quite a while debugging, only to realize ' isn't the same as ...)

Hope this helps others, not trying to steal karma!

answered Jan 1, 2018 at 2:29

Comments

4

After messing about a bit with Applescript I wrote this little bit of Applescript to connect:

Note that the "Show Bluetooth in menu bar" checkbox needs to be on for this as it's effectively just using that menu.

tell application "System Events"
 tell process "SystemUIServer"
 tell (menu bar item 1 of menu bar 1 whose description is "bluetooth")
 click
 -- You can use your phone name as well over here if you have multiple devices
 -- tell (menu item "YOUR_PHONE_NAME_HERE" of menu 1)
 tell (menu item 1 of menu 1)
 click
 tell (menu item 1 of menu 1)
 click
 end tell
 end tell
 end tell
 end tell
end tell
answered Feb 19, 2014 at 17:41

3 Comments

Was this in Mavericks? Does not work for me in 10.9.2. I get: error "System Events got an error: Application isn’t running." number -600
@AndrewBurns: yes, it is in Mavericks. But I should note that this script assumes you have the Bluetooth icon enabled in the menu bar.
It turns out that it was some other problem, a restart fixed the 600 error. I did however end up with a solution very similar to yours (see my answer).
2

This tool1 allowed me to connect to bluetooth headphones from command line.

answered Jan 4, 2018 at 9:58

3 Comments

I was about to post the same link (would have been a duplicate answer) because you didn't list the name of the tool. I answered a similar question here. I confirm that BluetoothConnector is also working for me.
Can confirm that BluetoothConnector is working for the intended task. My script (bound to a keyboard shortcut) first enables BT (if it's off) with blueutil, then connects to my Airpods using BluetoothConnector.
FYI blueutil is installable via Homebrew.
0

As far as I know you can only turn on/off bluetooth and check the status in command line (using blueutil or manipulating with launchctl). But you can use your applescript routines via osascript in shell.

answered Jul 8, 2013 at 6:17

Comments

0

Dougal's answered worked for me. You can also use Automator to create a service that can be run from anywhere and assign it a Keyboard Shortcut in Keyboard Shortcut Preferences for an even faster workflow.

answered Jul 8, 2015 at 21:09

Comments

0

I have multiple bluetooth audio device(s). So Andrew's script is very helpful. Here's my modification to his script. I'm not a programmer/IT person, just learned some bits from the internet.

set btchoice to BT_Choice()
on BT_Choice()
display dialog "Choose the device of your choice" with title "Selecting Device" buttons {"Bluedio", "Reconnect S-TS", "Anker A7721"} default button "Reconnect S-TS"
set Ndialogresult to the result
set DNameSel to button returned of Ndialogresult
display dialog "Whether to Connect or Disconnect the Device" with title "Handling Bluetooth" buttons {"Connect", "Disconnect", "Cancel"} default button "Connect"
set Bdialogresult to the result
set Bbuttonsel to button returned of Bdialogresult
activate application "SystemUIServer"
tell application "System Events"
 tell process "SystemUIServer"
 set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
 tell btMenu
 click
 tell (menu item DNameSel of menu 1)
 click
 if exists menu item Bbuttonsel of menu 1 then
 click menu item Bbuttonsel 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
end BT_Choice
answered Jan 8, 2017 at 13:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.