Skip to content

Instantly share code, notes, and snippets.

@jwmann
Last active September 18, 2020 19:26
Show Gist options
  • Select an option

  • Save jwmann/46f57cedfb5bcc0ff7d87e84abc3b306 to your computer and use it in GitHub Desktop.

Select an option

Save jwmann/46f57cedfb5bcc0ff7d87e84abc3b306 to your computer and use it in GitHub Desktop.
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
# Notes: If SwitchAudioSource, the script will install it automatically
toggle_audio() {
brew=/usr/local/bin/brew
if [ -f $brew ]; then
switchAudioSource=$($brew --prefix)/bin/SwitchAudioSource
if [ -f $switchAudioSource ]; then
currentOutput="$($switchAudioSource -c)"
audioOutputA="All Speakers"
audioOutputB="Yeti Stereo Microphone"
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