Skip to content

Instantly share code, notes, and snippets.

@itsrobli
Created July 29, 2021 23:35
Show Gist options
  • Save itsrobli/4cfe115b6a422676485e2ecde2934916 to your computer and use it in GitHub Desktop.
Save itsrobli/4cfe115b6a422676485e2ecde2934916 to your computer and use it in GitHub Desktop.
Connects and disconnects from AirPods in macOS Big Sur
# Connects and disconnects from AirPods in macOS Big Sur
# Credit: https://www.reddit.com/r/MacOS/comments/i4czgu/big_sur_airpods_script/gck3gz3/
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