-
-
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)) |
This is dope! using it for my productivity
repo :) (scripting a keyboard shortcut to do this)
Hey guys ! Made a little update on the script as I faced some issues regarding the audio output which didn't want to be properly set with @SilverFire script.
For some reasons the AirPods were showing up after a few seconds in my output list and not directly after the connection therefore: /usr/local/bin/SwitchAudioSource was returning an error because it couldn't find the AirPods in the device list.
Just for info I'm using Ventura beta with AirPods Pro (2nd gen)
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
with timeout of 30 seconds
set deviceValueInAudioSource to ""
repeat while deviceValueInAudioSource = ""
set deviceValueInAudioSource to do shell script "/usr/local/bin/SwitchAudioSource -a | grep -m1 " & quotedDeviceName
end repeat
do shell script "/usr/local/bin/SwitchAudioSource -s " & quotedDeviceName
return "Connecting " & (device's nameOrAddress as string)
end timeout
end toggleDevice
return toggleDevice(getFirstMatchingDevice(AirPodsName))
@bryanrmq Any chance you could help me set this up? I've been trying for 2 days and can't figure out for the life of me what I'm doing wrong.
Hi there @here4dahelp, I simply made a Shortcut out of this script.
I can't share my shortcut unfortunately because GitHub prohibits that to be shared.
Note: I added a keyboard shortcut (fn-shift-f12) to run the shortcut whenever I would like my AirPods to be connected on the MacBook. It works almost instantly on my side.
Hope this helps you out. Good luck !
@bryanrmq Thank you so much!!! Cheers mate.
@bryanrmq This is awsome!
Is there a way to have the reverse happen? I.E. I want to create a shortcut on my mac to have my AirPods connect to my iPhone.
Hi @bphirsh !
I don't see a possibility to do that out of the blue... Maybe with the help of an app and some remote push or something which could trigger something on your iPhone... But beside that I'm sorry, can't see a better possibility as of now.
All the best, if you find anything, feel free to share it ;-)
Cheers !
@bryanrmq Thanks for the reply! Anyways most of the time the airpods switch back to my phone quickly, your original shortcut is fantastic and solves my main issue! Cheers!
It keeps giving me this error "The device parameter is missing for toggleDevice." What am I doing wrong?
It keeps giving me this error "The device parameter is missing for toggleDevice." What am I doing wrong?
Hi mate, have you properly set the AirpodsName variable with the correct name matching your AirPods ?
Great! I can connect to my specific Bluetooth headset. And to disconnect one specific Bluetooth device, you can use the script:
use framework "IOBluetooth"
use scripting additions
set AirPodsName to "HUAWEI FreeBuds 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 disconnectDevice(device)
set quotedDeviceName to quoted form of (device's nameOrAddress as string)
if (device's isConnected as boolean) then
with timeout of 30 seconds
set deviceValueInAudioSource to ""
repeat while deviceValueInAudioSource = ""
set deviceValueInAudioSource to do shell script "/usr/local/bin/SwitchAudioSource -a | grep -m1 " & quotedDeviceName
end repeat
set currentDeviceName to do shell script "/usr/local/bin/SwitchAudioSource -c"
if currentDeviceName contains quotedDeviceName then
do shell script "/usr/local/bin/SwitchAudioSource -n"
end if
device's closeConnection()
return "Disconnecting " & (device's nameOrAddress as string)
end timeout
end if
end disconnectDevice
set matchingDevice to getFirstMatchingDevice(AirPodsName)
if matchingDevice is not equal to missing value then
return disconnectDevice(matchingDevice)
else
return "Device not found"
end if
I've tested it on my Mac osx 13.3.1, it's working well.
What a beauty @penn201500 ! Thanks !
On Ventura 13.4, the installation folder of SwitchAudioSource
is /opt/homebrew/Cellar/switchaudio-osx/1.2.2/bin/SwitchAudioSource
, so adjust that path
Nice one, thanks!
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:
brew install switchaudio-osx
orport install switchaudio-osx
) to change the audio output device to AirPodsNow I have a TouchBar button that instantly gives my sound back.