The following is an applescript that I use to change audio output devices:
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"
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row 2 of table 1 of scroll area 1) then
set selected of row 1 of table 1 of scroll area 1 to true
set deviceselected to "Headphones"
else
set selected of row 2 of table 1 of scroll area 1 to true
set deviceselected to "MX279"
end if
end tell
end tell
end tell
tell application "System Preferences" to quit
It worked on Yosemite, but when I updated to El Capitan it is giving me the following error:
"System Events got an error: Can't get tab group 1 of window \"Sound\" of application process \"System Preferences\". Invalid index"
I'm pretty unfamiliar with applescript, so any ideas why this might be happening will be much appreciated.