Easy Audio Output Toggle Using AppleScript, Growl and Quicksilver

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.
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/.
- Comments (4)
- in the chapter, "Code"
- tagged with applescript, growl, quicksilver
InterAction:
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:
- Previous:
- Next:
GetUpdated
ElseWhere
AllChapters
- 2 articles in the chapter Accessibility
- 6 articles in the chapter Announcements
- 4 articles in the chapter Blogging
- 7 articles in the chapter CMS
- 1 articles in the chapter Code
- 3 articles in the chapter CSS
- 15 articles in the chapter Design
- 4 articles in the chapter Downloads
- 1 articles in the chapter Freebies
- 1 articles in the chapter Gadgets
- 4 articles in the chapter Journeys
- 1 articles in the chapter Miscellany
- 59 articles in the chapter Movable Type
- 2 articles in the chapter Plugins
- 1 articles in the chapter Print
- 1 articles in the chapter Projects
- 2 articles in the chapter SEO
- 6 articles in the chapter Social Networking
- 1 articles in the chapter Standards
- 1 articles in the chapter Writing
BiteSize.blog
ReaderFavorites
- CSS Image Framing
- Tools of the Web Design Trade, Pt.1: Where to Begin
- Tools of the Web Design Trade, Pt.2: Building Trust
- 10 Tips For Creating Website Mockups In Photoshop
- Movable Type As A Desktop App
- Redesign, Part 1: The Logo
- Redesign, Part 2: Stylesheet Philosophy
- The Social Graph in Plain Language
- Writing Your Own Autobiography: The New Persistence of Information
- Designing eComm 2008



















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!