Toggle between two Audio Output Sources (Airplay Speakers)

I often plug my MacBook into a secondary display using HDMI (Samsung TV). While working, I like to popup a BTT Webview of a streaming video site, but the audio was always stuck to my MacBook and not playing through TV Speakers.
Screen Shot 2020-02-25 at 8.50.15 AM
I would then select the Output device from the volume menu bar to change audio to Samsung TV. I decided to write a toggle for the Touch Bar to complete this audio output selection for me. Enjoy!

-- Applescript to toggle between two sound outputs by Line number, as they appear in the System Prefs. Sound Control Panel.

set output1 to 1 --change this to the actual 'line number' of your first output speaker
set output2 to 2 --change this to the actual 'line number' of your second output speaker selection
--the rest of the script will use these vales to toggle between the two

tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
end tell

tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
if (selected of row output1 of table 1 of scroll area 1) then
set selected of row output2 of table 1 of scroll area 1 to true
else
set selected of row output1 of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
quit application "System Preferences"

Enjoying This Preset? PayPal.me/K2DesignLab

This is amazing, thank you!!

1 Like

This is amazing and I love it. Slight problem though, if I have my Mac plugged into a display with speakers it kind of ruins this by making:
output 1- Mac
output 2- Display
output 3- Headphones
Is there some fancy scripting magic to make this IF statement apply to 3 variables?

Imprint | Privacy Policy