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

17 September 20095. Yeggeps:

Awesome! Just what I was looking for! Big thanks!

2 February 20106. Peter:

Thank you, very helpful! I modified it slightly for the Mac Pro to toggle between internal speaker and digital out (as there are three outputs, not two) and it works beautifully. The download link IS broken, but it works if you just use the end part, here: http://blog.plasticmind.com/assets/Toggle_Sound_Output.zip

2 February 20107. Peter:

Just copy paste the link above, don't click, as the site auto-breaks it :)

2 February 20108. Jesse Gardner:

Peter: Thanks for posting the link. It seems as though the JS I was using to track downloads was breaking it. It's since been resolved, clicking the download link should work now.

12 April 20109. Mariusz:

Hi, I have OSX 10.4.11 and there is only headphone out audio option on my quicksilver 2002 Powermac. Ay guesses as to why ?

I've been trying to resolve it for weeks now with no use.

Also can not get audigy 2 zs pci to work with kx audio drivers for tiger...bad luck :P

email if somebody knows anything would be very appreciated


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 dopplr Find me on facebook Find me on lastfm Find me on linkedin Find me on livejournal Find me on msn Find me on pownce Find me on skype Find me on technorati Find me on twitter Find me on vox Find me on yahoo Find me on youtube