Connect to a Bluetooth device (iPhone) via command line on MacOSX

I am trying to figure out a way to connect to my iPhone via Bluetooth with a shell script. I am currently using applescript, which essentially does this via UIElements, but I wonder if this can be done using the command line utility, ala blueutil, but with the ability to connect to the device, is it not easy to turn on / off Bluetooth? Thanks for attention.

-Afshin

+7
shell iphone bluetooth macos
source share
8 answers

This answer is very similar to @Wolph answer; however, after struggling with other problems, this is what I ran into. I like this for two reasons: # It will not disconnect the device if it is already connected # Good output from osascript

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

This has been working on OSX Mavericks 10.9.2 since April 11, 2014, although you may need to provide access to any method that you use to run it in the security settings panel. See This is Apple KB: http://support.apple.com/kb/HT5914

All you have to do is change the line "LG HBS730" to the name of your device and you should be installed. Refunds there, so you get a good result 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 
+8
source share

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

connect-mouse.scpt: 509: 514: runtime error: system events received an error message: I can’t get menu 1 in the menu bar of item 3 of the menu bar of menu 1 of the SystemUIServer application process. Invalid index. (-1719)

It seems that you are choosing a menu item that allows you to click on it, but not get to the menu, for some incomprehensible 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 do not 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 ....

+6
source share

After messing with Applescript, I wrote this little Applescript to connect:

Please note that the β€œShow Bluetooth in menu bar” checkbox must be enabled for this, since it uses this menu effectively.

 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 
+4
source share

This parameter was updated 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 pressing Escape for this answer and confirmed to this page
  • Check for a device in the Bluetooth menu to detect invalid names (I spent quite a bit of time debugging, just to implement ' this is not the same as ' ...)

Hope this helps others without trying to steal karma!

+3
source share

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

0
source share

Dougal's answer answered me. You can also use Automator to create a service that can be launched from anywhere, and assign it a keyboard shortcut in the keyboard shortcut settings for an even faster workflow.

0
source share

I have several audio devices (bluetooth). Therefore, the Andrew script is very useful. Here is my modification of its script. I am not a programmer / IT professional, I 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 
0
source share

This tool 1 allowed me to connect to bluetooth headphones from the command line.

0
source share

All Articles