Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Toggle between two audio devices on macOS as a shell script. Set the devices on line 11 and 12. Requires Homebrew to be installed in the usual directory.
#! /bin/bash
# Author: James W Mann (jwmann)
# Dependencies: homebrew, SwitchAudioSource (https://github.com/deweller/switchaudio-osx)
# Notes: If SwitchAudioSource, the script will install it automatically
# See all audio devices: $> SwitchAudioSource -a
# Manually switch with: $> SwitchAudioSource -s device_name
toggle_audio() {
brew=/usr/local/bin/brew
if [ -f $brew ]; then
switchAudioSource=$($brew --prefix)/bin/SwitchAudioSource
if [ -f $switchAudioSource ]; then
currentOutput="$($switchAudioSource -c)"
audioOutputA="Vanatoo T0"
audioOutputB="Built-in Digital Output"
case $currentOutput in
$audioOutputA )
switchTo=$audioOutputB
;;
$audioOutputB )
switchTo=$audioOutputA
;;
esac
$switchAudioSource -s "$switchTo" > /dev/null 2>&1
osascript -e 'display notification "'"${switchTo//\"/\\\"}"'" with title "Audio Output"'
return 0
else
osascript -e 'display notification "SwitchAudioSource binary not found. Installing now." with title "Error Switching Audio"'
$brew install switchaudio-osx && osascript -e 'display notification "SwitchAudioSource binary successfully installed." with title "Audio Output"' && toggle_audio
return 0
fi
else
osascript -e 'display notification "Homebrew not found. Please install homebrew." with title "Error Switching Audio"'
return 1
fi
}
toggle_audio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment