Skip to content

Instantly share code, notes, and snippets.

@jaredmoody
Last active December 9, 2021 13:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaredmoody/fad52569fafe5c7d2597edbc18696547 to your computer and use it in GitHub Desktop.
Save jaredmoody/fad52569fafe5c7d2597edbc18696547 to your computer and use it in GitHub Desktop.
An Applescript to connect bluetooth devices, such as Airpods. Nice when paired with an alfred trigger.
# Don't use this script, use this one instead:
# https://gist.github.com/ieatfood/814b065964492f71f728da59a47413bc
tell application "System Events"
tell process "ControlCenter"
set bt to (first menu bar item whose title is "Bluetooth") of menu bar 1
click bt
set btCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title contains "AirPods Pro"
set btCheckboxValue to value of btCheckbox
tell btCheckbox to click
tell bt to click
end tell
end tell
@verheyenkoen
Copy link

Doesn't seem to work on Big Sur anymore. Do you have an update perhaps?

@jaredmoody
Copy link
Author

@verheyenkoen I find myself using this less on Big Sur since the menu itself is better, but updated above for you. Change "Airpods Pro" to just "Airpods" or to your device name as you require.

@verheyenkoen
Copy link

@jaredmoody Great thanks! The problem is that my iPhone regularly "steals" the AirPod connection from the Mac so I'd like an easy shortcut to reconnect. In the menu above you have to switch to MacBook speakers first and then Airpods again. Still a few very specific clicks that I'd like to eliminate.

@drsnn
Copy link

drsnn commented Jan 15, 2021

hi !

when i run script, I got error "System Events got an error: Can’t get menu bar item 1 of menu bar 1 of process \"ControlCenter\" whose title = \"Bluetooth\". Invalid index." number -1719erorr. Could you help me. Thanks

@siroger
Copy link

siroger commented Jan 16, 2021

Hi @drsnn, I can only guess it's because you don't have the bluetooth icon/menu enabled at the top right of your mac screen. There is probably some way to hide/display it in system preferences.

@jaredmoody
Copy link
Author

Hi @drsnn, @siroger is correct, under System Preferences > Bluetooth, check the "Show Bluetooth in menu bar" and then the script should work.

@suganth91
Copy link

Thanks for this gem! A tiny little thing, if you are from UK or Australia, then just change Control Center to Control Centre when using this line set btCheckbox to checkbox 1 of scroll area 1 of group 1 of window "Control Center" whose title contains "AirPods Pro"

@jaredmoody
Copy link
Author

My pleasure - thanks for the tip!

@ieatfood
Copy link

I found a more elegant script, using IOBluetooth framework instead of menu clicks, so it should work in other languages or if Apple updates the menus.

https://gist.github.com/ieatfood/814b065964492f71f728da59a47413bc

by https://github.com/smithumble

@jaredmoody
Copy link
Author

Yep, that one is better, thanks!

@verheyenkoen
Copy link

Brilliant! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment