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/.

  • posted on 30 December 2008
  • by Jesse

InterAction:

31 December 20081. Mark:

Nice! You got me all excited, then I (re-)discovered this is for a MacBook. Won't work with an iMac, since the headphone connection is a hardware-level switch. Nuts!

31 December 20082. Jesse Gardner:

Wow, Mark... seems like you're sunk. :\

7 April 20093. Chris Rowe:

Worked great on my Mac Pro though I could not use the download link at the end. I just copied each block of script in order and poof, it worked.

Only wish is that there was a way so the preference pain did not pop up.

28 June 20094. ulysses:

hmmz.. does not really work for me..

when i paste the whole thing in my applescript and edit the device names i get the error while running "Syntax error , expected end but found indentifier"


=\
Also the download link is broke..


YourThoughts?



(Minutia)

  • Author:
    Jesse
  • Published:
    Dec 30, 2008
  • Chapters:

GetUpdated

ElseWhere

Find me on aim Find me on delicious Find me on digg Find me on linkedin Find me on pownce Find me on twitter Find me on youtube Find me on skype Find me on facebook Find me on livejournal Find me on msn Find me on vox Find me on technorati Find me on yahoo Find me on dopplr Find me on lastfm

ActionStream

  • Jesse saved The Declaration of Independence as a favorite video Jesse saved The Declaration of Independence as a favorite video 2009-07-04T21:00:08Z 2009-07-04T21:00:08Z
  • Jesse said, "We hold these truths to be self-evident. http://tr.im/qT9K (via @nathansmith)" Jesse said, “We hold these truths to be self-evident. http://tr.im/qT9K (via @nathansmith)” 2009-07-04T20:36:56Z 2009-07-04T20:36:56Z
  • Jesse said, "It's funny how difficult Drudge's headlines are to read in Independence Day blue." Jesse said, “It’s funny how difficult Drudge’s headlines are to read in Independence Day blue.” 2009-07-04T17:09:35Z 2009-07-04T17:09:35Z
  • Jesse said, "@foolmother Hilton did the same thing to me as well." Jesse said, “@foolmother Hilton did the same thing to me as well.” 2009-07-04T13:40:20Z 2009-07-04T13:40:20Z
  • Jesse said, "The Adventures Of An Independent Contractor: Client: "We won't be in tomorrow." Me: "Why not?!" Client: "Uh—the holiday." Me: "Oh—riiight."" Jesse said, “The Adventures Of An Independent Contractor: Client: "We won’t be in tomorrow." Me: "Why not?!" Client: "Uh—the holiday." Me: "Oh—riiight."” 2009-07-04T13:22:02Z 2009-07-04T13:22:02Z