An AppleScript to change sound output device
Jun 28, '05 09:18:00AM • Contributed by: Miraglia
The goal of this small AppleScript is to be able to change the sound output device quickly and easily. Without using Accessibility Controls, the process usually involves mousing to the System Preferences, mousing to and clicking on the Sound control, then mousing to and clicking on the desired output.
If you wire this script to a keyboard shortcut (using, for example, FastScripts from Red Sweater), you can choose your default source without mousing at all.
This first version gets the list of possible outputs, and presents a dialog with buttons enabling you to choose the output source. The return key triggers your default choice without touching the mouse (replace "SoundSticks" in the script with the name of your preferred default).
tell application "System Preferences" to activate tell application "System Events" get properties tell process "System Preferences" click menu item "Sound" of menu "View" of menu bar 1 delay 2 set theRows to every row of table 1 of scroll area 1 of ¬ tab group 1 of window "sound" set theOutputs to {} as list repeat with aRow in theRows copy (value of text field 1 of aRow as text) to the end of theOutputs end repeat tell application "Finder" activate set desiredOutput to display dialog ¬ "Choose Sound Output: " buttons theOutputs default button "SoundSticks" end tell repeat with aRow in theRows if (value of text field 1 of aRow as text) is equal to ¬ (button returned of desiredOutput as text) then set selected of aRow to true exit repeat end if end repeat end tell end tell tell application "System Preferences" to quit
And next, a streamlined version that switches to your desired default source without asking you for input. Change the text "SoundSticks" in the script to the name of your desired output source.
tell application "System Preferences" to activate tell application "System Events" get properties tell process "System Preferences" click menu item "Sound" of menu "View" of menu bar 1 delay 2 set theRows to every row of table 1 of scroll area 1 of ¬ tab group 1 of window "sound" set theOutputs to {} as list repeat with aRow in theRows if (value of text field 1 of aRow as text) ¬ is equal to "SoundSticks" then set selected of aRow to true exit repeat end if end repeat end tell end tell tell application "System Preferences" to quit
This second version is useful if, for example, you are a laptop user who frequently plugs into and then disconnects from an external sound output like SoundSticks. Upon plugging in, this script allows you to select the external sound output with a single keyboard shortcut (again, assuming that you have a utility for launching scripts via user-defined keyboard shortcuts).