Skip to content

Instantly share code, notes, and snippets.

@greglamb
Created September 22, 2018 04:49
Show Gist options
  • Save greglamb/63060e873e9d64bc3730b30ac52507f0 to your computer and use it in GitHub Desktop.
Save greglamb/63060e873e9d64bc3730b30ac52507f0 to your computer and use it in GitHub Desktop.
Connect to Bluetooth devices via the Terminal
#!/usr/bin/osascript
(*
Usage:
./connect-bluetooth.applescript listall
./connect-bluetooth.applescript "Greg’s AirPods"
*)
on run argv
set DeviceName to item 1 of argv
activate application "SystemUIServer"
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 DeviceName is equal to "listall" then
repeat with btdevice in (get menu items of menu 1 of bt)
log btdevice
end repeat
key code 53
else
if exists menu item DeviceName of menu 1 of bt then
tell menu item DeviceName of menu 1 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 if
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment