Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Applescript to cycle the audio input and output sources between two (or more) audio devices
on alfred_script(q)
set output_devices to {"Display Audio", "Generic USB Audio Device "}
set input_devices to {"Display Audio", "Generic USB Audio Device "}
set device_keywords to {"speakers", "headphones"}
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
tell application "System Events"
tell process "System Preferences"
set target_idx to 0
if q is not "" then
repeat with idx from 1 to count of device_keywords
if (item idx of device_keywords is q) then
set target_idx to idx
exit repeat
end if
end repeat
else
repeat with idx from 1 to count of output_devices
set device to (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is item idx of output_devices)
if (selected of device) then
if (idx is (count of output_devices)) then
set target_idx to 1
else
set target_idx to idx + 1
end if
exit repeat
end if
end repeat
end if
if target_idx is not 0 then
select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is item target_idx of output_devices)
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is item target_idx of input_devices)
set script_str to "/usr/local/bin/growlnotify --image /System/Library/PreferencePanes/Sound.prefPane/Contents//Resources/SoundPref.icns -name 'Audio switcher' -m 'Switched audio to " & item target_idx of device_keywords & "'"
do shell script script_str
end if
end tell
end tell
quit
end tell
end alfred_script
@indec

This comment has been minimized.

Owner

indec commented May 29, 2013

As written, it's designed to be used by Alfred. Given the optional parameter it switches to the specified audio source and input. Without the parameter, it picks the next set in the list (which works as a toggle if there are only two items in the list).

@indec

This comment has been minimized.

Owner

indec commented May 29, 2013

The devices in this example match my Thunderbolt Display and my USB sound card. Update the script to suit your own setup. The sound card I have does actually have three spaces at the end of its device name. I have no idea why.

@medoix

This comment has been minimized.

medoix commented Nov 28, 2013

Are you able to share the alfred workflow? i cannot seem to get it to work but could be related to Mavericks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment