Easy Audio Output Toggle Using AppleScript, Growl and Quicksilver

Growl Notification

Necessity is the mother of invention, and with Ethan crying frequently, I needed a quick and easy way to switch between my headphones and desktop speakers. So I put together this simple AppleScript that toggles between two audio output sources and assigned it to a Quicksilver hotkey. Here it is in a nutshell.

(If you’ve not used AppleScript before, the quick explanation is that you should copy and paste the code below into the Script Editor application. For a more detailed understanding of it, check out Apple’s developer guide for AppleScript.)

First, we activate the System Preferences and the sound preferences pane:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound"
end tell

This next part is a bit confusing, but it’s important to understand as you may want to customize it. First, we’re targeting the Output tab (make sure “Enable Assistive Devices” is checked in the “Universal Access” preference pane) then we do a simple conditional statement. If the second row on your sound output list (my Line Out) is currently selected, this tells your Mac to select the first row (my Headphones). If not, it selects the second row. You can easily customize this part to fit your specific needs.

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 "Line Out"
        end if
    end tell
    end tell
end tell

Then we close out the System Preferences window:

tell application "System Preferences" to quit

Next comes the Growl notification magic:

tell application "GrowlHelperApp"
    set the allNotificationsList to {"Sound Notification"}
    set the enabledNotificationsList to {"Sound Notification"}

    register as application "Toggle Sound Output" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"

    notify with name "Sound Notification" title "Audio Output" description deviceselected application name "Toggle Sound Output"

end tell

In this last bit we’re registering our AppleScript with Growl and then passing along a notification we built from the variable we set up in the conditional statement (deviceselected). And that’s it!

Download the full script here.

The last bit of magic is to save the script and assign it to a Quicksilver trigger. Now, every time I press Command-, my audio output toggles and I get a handy Growl notification telling me which is now active.

Assign the AppleScript to a Quicksilver trigger.

Looking for more handy Quicksilver triggers? Check out LifeHacker’s Nine Time-Saving Quicksilver Triggers How about sharpening your AppleScript-fu? Be sure to stop by http://macscripter.net/.