How To use applescript change output source ?

Just like this ? but this just can be control Music ?

this script can change,can only control 2 devices,not flexible。。

set output1 to 1 --change this to the actual 'line number' of your first output speaker

set output2 to 4 --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"Preformatted text