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?

This is gold! Thank you!

To @mvonb and to whoever finds it useful, I adapted it to cycle between 3 devices

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

set output1 to 4 --change this to the actual 'line number' of your first output speaker
set output2 to 5 --change this to the actual 'line number' of your second output speaker selection
set output3 to 1 --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 if (selected of row output2 of table 1 of scroll area 1) then
set selected of row output3 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"
1 Like

Adapted it so it works with Mac OS Monterey (June 2022), assuming you only have 2 outputs (Macbook Speakers & Airplay).

-- 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 3 --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"

`The Apple Shortcuts has a Set Audio Output shortcut now.

If the gui coding changes in the future, you could set up three Set Audio Output shortcuts and trigger them with BTT.

You might even use the new BTT action Cycle Through Multiple Actions (on repeated trigger) to put it all on one button.

Your script works better, i see that Shortcuts doesn't give access to all audio output options.