Forked from jaredmoody/Connect Airpods.applescript
Last active
February 18, 2024 22:25
-
-
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 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)) |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes. Here is my code