I recently bought a USB headset to make Skype calls, and wanted a quick way to switch the Mac’s audio output between the built-in line output (which feeds my speakers) and the headset. Rogue Amoeba’s free SoundSource provides a handy system-wide menu bar for doing that, but every time I switched to the headphones, they blasted my ears. So I cobbled together this AppleScript to switch between sound sources and adjust the volume with one click.
The code is based on a script I found at MacOS Hints.com. I substituted the name of my headset, added a line that changes the volume, and added a verbal confirmation of the change and some error checking. I also reformatted the dialog window and set it to disappear after three seconds.
To use this script, paste it into Script Editor, substitute the names of your output devices for mine, and then save it as an application or as a script. You can then double-click the application to run it, or assign the script to a keystroke with a program like Butler.
Note the line about enabling Assistive Devices. Because the script works by interacting with elements on the screen, it misbehaved when I unplugged the headset—there was no longer a headset item to select in the Output menu. I’m surprised that AppleScript doesn’t let you refer to screen elements by an ID, such as “Balance slider.” Instead, you have to use syntax like slider 1 of group 1 of tab group 1 of window 1. I couldn’t figure out where the volume slider was located in that hierarchy, so I ended up changing the volume through the Finder. Let me know if you suss it out.
(*
This script toggles between two audio outputs in the "Sound" pane in "System Preferences" and adjusts the volume.
Modified from a script at http://forums.macosxhints.com/showthread.php?t=45384
to add volume control and GUI scripting detection. --David Battino, www.batmosphere.com.
USES GUI SCRIPTING; "ENABLE ASSISTIVE DEVICES" OPTION MUST BE CHECKED IN THE "UNIVERSAL ACCESS" PREFERENCE PANE
*)
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
if UI elements enabled then
try
tell application process "System Preferences"
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row 3 of table 1 of scroll area 1) then --headset is selected
set selected of row 1 of table 1 of scroll area 1 to true
set deviceselected to "Line Out"
set verbal_description to "Line out."
tell application "Finder"
set volume 7
end tell
else
set selected of row 3 of table 1 of scroll area 1 to true
set deviceselected to "Logitech USB Headset"
set verbal_description to "Headset."
tell application "Finder"
set volume 2
end tell
end if
end tell
end tell
tell application "System Preferences" to quit
tell me to activate
say verbal_description using "Trinoids"
display dialog "Audio output is now..." & return & return & "* " & deviceselected buttons {"Rock on"} default button 1 giving up after 3
on error
tell me to activate
display dialog "Please plug in the headset." buttons {"Whoops!"} default button 1
end try
else --GUI scripting is disabled
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell
display dialog "Please check the box called \"Enable access for assistive devices.\"" buttons {"Okay"} with icon 1 default button 1
end if
end tell

Rogue Amoeba’s SoundSource offers a simple way to switch among inputs and outputs, but it doesn’t adjust levels.
Another thing I haven’t figured out is why the script will occasionally reverse the volume settings for the line out and the headset, or crank the latter all the way up. (The scale apparently goes from 0 to 7, though there may be a parameter to increase the resolution to 256 steps, as in the QuickTime Player.) Nevertheless, I find having one-key output switching is a treat.
Update, 2006-07-17: I inserted a backslash before the quotation marks in the GUI scripting dialog so the script will compile correctly. The O’Reilly blogging system changed the curly quotes I had in the original script to straight ones, breaking it.


Cool! I've been wanting to do exactly the same thing for exactly the same reason! Thanks!
Skype maintains its own preferences for audio input and output devices (see the Audio pane in Skype's preferences menu.) These settings are even easier to change using Applescript, since they don't require any GUI scripting. Applescript has access to the Skype API (documented at https://developer.skype.com/Docs/ApiDoc/FrontPage), which allows for things like:
tell application "Skype"
activate
send command "SET AUDIO_OUT AppleUSBAudioEngine:ÿ:Free-1 USB Phone:5b241000:1" script name "something"
send command "SET AUDIO_IN AppleUSBAudioEngine:ÿ:Free-1 USB Phone:5b241000:2" script name "something"
end
(You can get the name for a device by manually switching to it in Skype's preferences, then running the following Applescript.)
tell application "Skype"
activate
send command "GET AUDIO_IN" script name "something"
end tell
Thanks, ABN! I tried your second script and got the name of my line output, but don’t see how to set its volume. Can you clarify?
Having the global volume switcher could still be useful, though, if I want to use the headset to listen to music.
Applescript can access and modify system volume settings with the "get volume" and "set volume" settings in the Standard Additions dictionary. This allows for separate volume settings for the output device, the input device, the alert volume, and output muting. For instance:
set volume output volume 80 input volume 80 alert volume 0 without output muted
Today I plugged in another audio interface and noticed that it took the headset's spot on the list of possible outputs in the preference pane. Thanks to another Mac OS Hints script, here’s a modified version of the switcher script that now checks if the item behind Door #3 really is what we want. I also changed the volume command based on ABN’s tip.
(* This script toggles between two audio outputs in the "Sound" pane in "System Preferences" and adjusts the volume.
Modified from a script at http://forums.macosxhints.com/showthread.php?t=45384 to add volume control and GUI scripting detection. -David Battino, www.batmosphere.com.
USES GUI SCRIPTING; "ENABLE ASSISTIVE DEVICES" OPTION MUST BE CHECKED IN THE "UNIVERSAL ACCESS" PREFERENCE PANE *)
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
if UI elements enabled then
try
tell application process "System Preferences"
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row 3 of table 1 of scroll area 1) then --headset is selected
select row 1 of table 1 of scroll area 1 -- Line Out
set deviceselected to value of text field 1 of row 1 of table 1 of scroll area 1 as text -- Line Out
set new_volume to 100 --100 is max
set verbal_description to "Line out."
else
select row 3 of table 1 of scroll area 1 -- headset
set deviceselected to value of text field 1 of row 3 of table 1 of scroll area 1 as text --hopefully "Logitech USB Headset"
set new_volume to 30
if deviceselected is not equal to "Logitech USB Headset" then
select row 1 of table 1 of scroll area 1 -- Switch back to Line Out
error
end if
set verbal_description to "Headset."
end if
end tell
end tell
tell application "System Preferences" to quit
tell me to activate
say verbal_description using "Trinoids"
display dialog "Audio output is now..." & return & return & "• " & deviceselected buttons {"Rock on"} default button 1 giving up after 3
on error
tell me to activate
display dialog "Please plug in the headset." buttons {"Whoops!"} default button 1 with icon 2
end try
else --GUI scripting is disabled
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell
display dialog "Please check the box called \"Enable access for assistive devices.\"" buttons {"Okay"} with icon 1 default button 1
end if
set volume output volume new_volume without output muted
end tell
Even though the Standard Additions method is preferred the slider can be accessed through System Events through the following commands with the scale being from 0 to 1:
set value of slider 1 of group 1 of window "Sound" to 0.7
set value of slider 1 of group 1 of window "Sound" to 0.3
Incidentally, the command to center the stereo balance is
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
reveal (first anchor of current pane whose name is "output")
end tell
tell application "System Events"
launch
tell process "System Preferences" to tell slider 1 of group 1 of tab group 1 of window 1 to set value to 0 --center balance
tell process "System Preferences" to set value of slider 1 of group 1 of window "Sound" to 1 --maximize volume
end tell
Balance goes from -1 to 0 to 1.
Thanks for the center stereo balance script, David. Something's been causing the balance on my iMac G5 to drift again after a long period of remaining centered. Frustrating.
Btw, there's a missing double-quote on line 4 of the script; it should be 'reveal (... name is "output")'.
Thanks, SJK. I’ve updated the balance script.
Anyhone know how to use applescript to adjust mac sound output based on room ambient sound using the built in mic? For a museum kiosk setting.
I just downloaded SoundSource and it does adjust levels between internal speakers and USB headset on my 17" G4. This may have been a change made in the last year.
Hello,
i am looking for an applescript's master cause i am totally zero..
i'm using a bluetooth headset but i have no interest into the audio, what i need is to use the buttons of the headset..
Does anybody can help me to have a script or something that keystroke when i push my headset button (with different keystrokes for each buttons).
thanks for any help
freeka