Skip to content

Instantly share code, notes, and snippets.

@jmarcos-cano
Last active April 17, 2020 23:27
Show Gist options
  • Save jmarcos-cano/8d0db9c904c3b022252245d2d3edf31e to your computer and use it in GitHub Desktop.
Save jmarcos-cano/8d0db9c904c3b022252245d2d3edf31e to your computer and use it in GitHub Desktop.
Change audio Source in my Mac, input and output

Change Audio Source Automation

If you have multiple Audio Inputs like me:

  • MacBook Pro Speakers
  • Astro A50 Game
  • Krisp Speaker
  • DisplayPort (my monitor speakers)

And you want to change audio source using your keyboard you can use these two scripts alongside with

Im pretty sure you can use Automator with Keyboard shortcut but have not tested

Steps

  • Save .change-audio-source.sh in your home
  • Create a new HotKey in Spark with the contents of invoke-change-source.applescript and map it to FnX (function key)
#!/bin/bash
AudioOutput=$(/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -a -t output |egrep --color=no -v "Voice|krisp" |sed "s/(output)//g" |sort )
AudioOutputLength=$(echo "$AudioOutput" |wc -l)
CurrentDevice=$(/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -c)
NewDevice=$CurrentDevice
function switchNextAudioDevice(){
index=1
found=0
while IFS= read -r line; do
if echo "$line" |grep --color=no -q "$CurrentDevice";then
found=1
index=$(( index + 1 ))
break
fi
index=$(( index + 1 ))
done <<< "${AudioOutput[@]}"
#echo -e "index: $index \n found: $found"
if [[ $found -eq 1 ]];then
if [[ $index -gt $AudioOutputLength ]];then
index=0
export NewDevice=$( echo "${AudioOutput}" |sed -n 1p )
echo $NewDevice
fi
export NewDevice=$( echo "${AudioOutput}" | sed -n $(( $index ))p )
echo $NewDevice
fi
}
echo "[$(date)]" > /tmp/change-audio.log
NewOutputDevice=$(switchNextAudioDevice)
input=$(echo $NewOutputDevice |rev | cut -d ' ' -f 2- |rev)
NewInput=$(/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -a -t input |grep -v Game |grep --color=no "$input" )
NewInput=$(echo $NewInput |sed "s=(input)==" | xargs)
#echo -e "INPUT: $NewInput - $input \n OUTPUT: $NewOutputDevice "
if echo $NewOutputDevice |grep -q --color=no "DisplayPort" ;then
#echo "Setting default"
/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t input -s "MacBook Pro Microphone"
else
/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t input -s "$NewInput"
fi
/usr/local/Cellar/switchaudio-osx/1.0.0/SwitchAudioSource -t output -s "$NewOutputDevice"
set the currentAudioSource to (do shell script "~/.change-audio-source.sh")
display notification currentAudioSource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment