-
-
Save ieatfood/814b065964492f71f728da59a47413bc to your computer and use it in GitHub Desktop.
| # 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)) |
I want to run the applescript code @penn201500 posted from the dock as a shortcut but I get an error:

Any idea how to fix this?
Here's a version that works in Sequoia:
use framework "IOBluetooth"
use scripting additions
set AirPodsName to "AirPods" -- Replace with the exact name of your AirPods
on getFirstMatchingDevice(deviceName)
set pairedDevices to current application's IOBluetoothDevice's pairedDevices() as list
repeat with device in pairedDevices
if (device's nameOrAddress as string) contains deviceName then
return device
end if
end repeat
return missing value
end getFirstMatchingDevice
on connectDevice(device)
if device's isConnected() as boolean then
return "Device is already connected: " & (device's nameOrAddress as string)
else
device's openConnection()
delay 2 -- Wait a bit for the connection to establish
if device's isConnected() as boolean then
return "Successfully connected to " & (device's nameOrAddress as string)
else
return "Failed to connect to " & (device's nameOrAddress as string)
end if
end if
end connectDevice
-- Main Execution
set matchingDevice to getFirstMatchingDevice(AirPodsName)
if matchingDevice is not equal to missing value then
set result to connectDevice(matchingDevice)
return result
else
return "Device not found. Ensure your AirPods are paired and the name matches."
end if
Connect + Change the audio output device to Airpords
There is a popular problem when AirPods: when they switch from MacBook to iPhone, sometimes they don't connect back. Even if they get reconnected, the audio output still does not get changed back to AirPods.
I've adjusted @ieatfood implementation:
- Always try to connect, never try to disconnect.
- Use switchaudio-osx (
brew install switchaudio-osxorport install switchaudio-osx) to change the audio output device to AirPods- Add to Automator as a Quick Action (see screenshot below) + Apple manual
- Add the Quick Actions button to the Touch Bar
Now I have a TouchBar button that instantly gives my sound back.
use framework "IOBluetooth" use scripting additions set AirPodsName to "AirPods Pro (Дмитрий)" 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) set quotedDeviceName to quoted form of (device's nameOrAddress as string) if not (device's isConnected as boolean) then device's openConnection() end if do shell script "/usr/local/bin/SwitchAudioSource -s " & quotedDeviceName return "Connecting " & (device's nameOrAddress as string) end toggleDevice return toggleDevice(getFirstMatchingDevice(AirPodsName))
Woowwww.....it's working! Thanks
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, but SwitchAudioDevice is 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!
use framework "IOBluetooth"
use scripting additions
set AirPodsName to "AirPods Pro"
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)
set deviceAddress to quoted form of (do shell script "echo" & (device's addressString as string) & " | tr [:lower:] [:upper:]")
if not (device's isConnected as boolean) then
device's openConnection()
end if
with timeout of 10 seconds
set successfulConnection to false
repeat while not successfulConnection
set output to do shell script "/opt/homebrew/bin/SwitchAudioSource -u " & deviceAddress
set successfulConnection to output contains "output audio device set to"
end repeat
return "Connected " & (device's nameOrAddress as string)
end timeout
end toggleDevice
try
return toggleDevice(getFirstMatchingDevice(AirPodsName))
on error e
return e
end try
Nice one, thanks!