With a little experimentation on Arthur Hammer's script I discovered that the lines set deviceselected to "xyz"
appear to be just visual feedback whilst in Script Editor & don't seem to be part of the actual functionality.
The actual switching appears to be just between 2 chosen lines in the Control Panel; so, if you just need to switch between any 2 outputs, then you can just use the row numbers.
In this example I've lifted them out to be variables, so you don't need to delve in the script to change the required lines…
As you have more than 2 outputs, the first time you switch it will not necessarily go from/to the correct device, but will after that.
For example, if you wanted to switch between outputs 3 & 4, yet 2 was currently selected, the first change would be from "not 3" as opposed to "is 3" [hope you understand that convoluted explanation]
(*
Applescript to toggle between two sound outputs by Line number, ¬
as they appear in the Sound Control Panel. Based on code by ¬
Arthur Hammer http://apple.stackexchange.com/a/209434/85275
*)
set outputA to 3 --change this to the actual 'line number' of your first desired output
set outputB to 4 --change this to the actual 'line number' of your second desired output
--the rest of the script will use these vales as a switch
tell application "System Preferences"
activate
set current pane to pane "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"
click radio button "Output"
if (selected of row outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
--tell application "System Preferences" to quit
--remove the comment '--' tag above to make the control panel quit afterwards, leave for testing.
Maybe a little pictorial clarification - using the above script with outputA = 3 & outputB = 4, I can now switch between Digital Out & TonePort UX2.
One more note - the line numbers do not have to be consecutive, I could just as easily switch between Internal Speakers & TonePort by using lines 1 & 4.
