AppleScript Audio Output Switcher

by David Battino

AppleScript speakers

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.


12 Comments

TjL
2006-07-16 05:17:41
Cool! I've been wanting to do exactly the same thing for exactly the same reason! Thanks!
ABN
2006-07-16 07:10:50
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

David Battino
2006-07-16 17:21:26
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.

ABN
2006-07-16 18:33:03
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

David Battino
2006-07-16 20:34:07
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


DJG
2006-07-16 23:36:34
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

David Battino
2006-07-21 14:06:54
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.

sjk
2006-07-31 22:51:13
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")'.

David Battino
2006-08-18 17:25:03
Thanks, SJK. I’ve updated the balance script.
MF Howe
2007-05-10 08:50:16
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.
Jeremy Weathers
2007-07-06 19:05:18
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.
freeka
2008-03-29 17:26:44
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