Skip to content

Instantly share code, notes, and snippets.

@kennonb
Last active January 13, 2017 03:10
Show Gist options
  • Save kennonb/92b2535dea224219a3737e42cc0b64fe to your computer and use it in GitHub Desktop.
Save kennonb/92b2535dea224219a3737e42cc0b64fe to your computer and use it in GitHub Desktop.
Toggle Bluetooth Devices
set btchoice to BT_Choice()
on BT_Choice()
-- Change the object listing of devices to the names of your personal Bluetooth devices
display dialog "Choose the device of your choice" with title "Selecting Device" buttons {"Keyboard", "Powerbeats 3", "Magic Trackpad"} default button "Powerbeats 3"
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
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 DNameSel) of menu of bt
click
tell menu 1
if exists menu item Bbuttonsel then
click menu item Bbuttonsel
return Bbuttonsel
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
end BT_Choice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment