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/.
- in the chapter, "Code"
- tagged with applescript, growl, quicksilver
(Minutia)
- Author:Jesse
- Published:Dec 30, 2008
- Chapters:
- Previous:
- Next:
GetUpdated
ElseWhere
AllChapters
- 2 articles in the chapter Accessibility
- 7 articles in the chapter Announcements
- 1 articles in the chapter App
- 2 articles in the chapter Apple
- 5 articles in the chapter Blogging
- 7 articles in the chapter CMS
- 1 articles in the chapter Code
- 3 articles in the chapter CSS
- 16 articles in the chapter Design
- 2 articles in the chapter DIY
- 4 articles in the chapter Downloads
- 2 articles in the chapter Freebies
- 2 articles in the chapter Gadgets
- 1 articles in the chapter Javascript
- 5 articles in the chapter Journeys
- 1 articles in the chapter Miscellany
- 1 articles in the chapter Mobile
- 59 articles in the chapter Movable Type
- 1 articles in the chapter Photography
- 2 articles in the chapter Plugins
- 1 articles in the chapter Print
- 3 articles in the chapter Projects
- 2 articles in the chapter Reviews
- 2 articles in the chapter SEO
- 10 articles in the chapter Social Networking
- 1 articles in the chapter Standards
- 4 articles in the chapter Writing
ReaderFavorites
- CSS Image Framing
- Tools of the Web Design Trade, Pt.1: Where to Begin
- Social Networking Is Killing My Ability To Write
- 10 Tips For Creating Website Mockups In Photoshop
- MobileMe vs. Dropbox
- 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


















