Forked from jaredmoody/Connect Airpods.applescript
Last active
December 18, 2025 02:36
-
-
Save ieatfood/814b065964492f71f728da59a47413bc 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Taken from https://www.reddit.com/r/MacOS/comments/i4czgu/big_sur_airpods_script/gck3gz3/ | |
| # by https://github.com/smithumble | |
| use framework "IOBluetooth" | |
| use scripting additions | |
| set AirPodsName to "AirPods" | |
| on getFirstMatchingDevice(deviceName) | |
| repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list) | |
| if (device's nameOrAddress as string) contains deviceName then return device | |
| end repeat | |
| end getFirstMatchingDevice | |
| on toggleDevice(device) | |
| if not (device's isConnected as boolean) then | |
| device's openConnection() | |
| return "Connecting " & (device's nameOrAddress as string) | |
| else | |
| device's closeConnection() | |
| return "Disconnecting " & (device's nameOrAddress as string) | |
| end if | |
| end toggleDevice | |
| return toggleDevice(getFirstMatchingDevice(AirPodsName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An issue I encountered is that if the AirPods are named, that nickname is not expressed in
deviceName. So, the bluetooth device is properly found, butSwitchAudioDeviceis unable to match against the bluetooth device name.As a more robust solution, you can have SwitchAudioDevice find the device via it's address string rather than pure name, which not only makes the script run much faster, but fixes the above problem!