Skip to content

Instantly share code, notes, and snippets.

@indec
Last active December 17, 2015 21:09
Show Gist options
  • Save indec/5672157 to your computer and use it in GitHub Desktop.
Save indec/5672157 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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