Last active
October 26, 2022 08:24
-
-
Save maxnowack/c6c4a74f6d03969e1d534b69b7818e33 to your computer and use it in GitHub Desktop.
Shell script to connect or disconnect my airpods with my mac.
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
airpods="LuftSchoten" | |
toggle=$1 | |
osascript <<END | |
use framework "IOBluetooth" | |
use scripting additions | |
set AirPodsName to "$airpods" | |
set toggle to "$toggle" | |
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 | |
display notification "AirPods not found" with title "AirPods Connection" | |
end getFirstMatchingDevice | |
on toggleDevice(device) | |
set quotedDeviceName to quoted form of (device's nameOrAddress as string) | |
if not (device's isConnected as boolean) then | |
display notification "Connecting to AirPods …" with title "AirPods Connection" | |
device's openConnection() | |
end if | |
try | |
with timeout of 10 seconds | |
set deviceValueInAudioSource to "" | |
repeat while deviceValueInAudioSource = "" | |
set deviceValueInAudioSource to do shell script "/opt/homebrew/bin/SwitchAudioSource -a | grep -m1 " & quotedDeviceName | |
end repeat | |
do shell script "/opt/homebrew/bin/SwitchAudioSource -t input -s " & quotedDeviceName | |
do shell script "/opt/homebrew/bin/SwitchAudioSource -t output -s " & quotedDeviceName | |
display notification "AirPods connected" with title "AirPods Connection" | |
end timeout | |
on error | |
display notification "AirPods not found" with title "AirPods Connection" | |
end try | |
end toggleDevice | |
if toggle is "" then | |
toggleDevice(getFirstMatchingDevice(AirPodsName)) | |
else | |
do shell script "/opt/homebrew/bin/SwitchAudioSource -t input -s " & quoted form of "MacBook Pro Microphone" | |
do shell script "/opt/homebrew/bin/SwitchAudioSource -t output -s " & quoted form of "MacBook Pro Speakers" | |
display notification "AirPods disconnected" with title "AirPods Connection" | |
end if | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment