Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to change sound output device System 10.4
The goal of this small AppleScript is to be able to change the sound output device quickly and easily. Without using Accessibility Controls, the process usually involves mousing to the System Preferences, mousing to and clicking on the Sound control, then mousing to and clicking on the desired output.

If you wire this script to a keyboard shortcut (using, for example, FastScripts from Red Sweater), you can choose your default source without mousing at all.

This first version gets the list of possible outputs, and presents a dialog with buttons enabling you to choose the output source. The return key triggers your default choice without touching the mouse (replace "SoundSticks" in the script with the name of your preferred default).
tell application "System Preferences" to activate
tell application "System Events"
  get properties
  tell process "System Preferences"
    click menu item "Sound" of menu "View" of menu bar 1
    delay 2
    set theRows to every row of table 1 of scroll area 1 of ¬
      tab group 1 of window "sound"
    set theOutputs to {} as list
    repeat with aRow in theRows
      copy (value of text field 1 of aRow as text) to the end of theOutputs
    end repeat
    tell application "Finder"
      activate
      set desiredOutput to display dialog ¬
        "Choose Sound Output: " buttons theOutputs default button "SoundSticks"
    end tell
    repeat with aRow in theRows
      if (value of text field 1 of aRow as text) is equal to ¬
        (button returned of desiredOutput as text) then
        set selected of aRow to true
        exit repeat
      end if
    end repeat
  end tell
end tell
tell application "System Preferences" to quit
And next, a streamlined version that switches to your desired default source without asking you for input. Change the text "SoundSticks" in the script to the name of your desired output source.
tell application "System Preferences" to activate
tell application "System Events"
  get properties
  tell process "System Preferences"
    click menu item "Sound" of menu "View" of menu bar 1
    delay 2
    set theRows to every row of table 1 of scroll area 1 of ¬
      tab group 1 of window "sound"
    set theOutputs to {} as list
    repeat with aRow in theRows
      if (value of text field 1 of aRow as text) ¬
        is equal to "SoundSticks" then
        set selected of aRow to true
        exit repeat
      end if
    end repeat
  end tell
end tell
tell application "System Preferences" to quit
This second version is useful if, for example, you are a laptop user who frequently plugs into and then disconnects from an external sound output like SoundSticks. Upon plugging in, this script allows you to select the external sound output with a single keyboard shortcut (again, assuming that you have a utility for launching scripts via user-defined keyboard shortcuts).
    •    
  • Currently 3.00 / 5
  You rated: 4 / 5 (5 votes cast)
 
[68,796 views]  

An AppleScript to change sound output device | 23 comments | Create New Account
Click here to return to the 'An AppleScript to change sound output device' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
SoundSource
Authored by: sinjin on Jun 28, '05 12:28:31PM
I wrote a similar Applescript to switch back and forth from my iMic connected headset. All that GUI scripting discovery was hard for this noob!

I ended up not using it because I found SoundSource almost immediately after completing the project. It allows you to switch between multiple input and output devices from the menubar. Don't think there are keyboard shortcuts like you can do with your script, though.

[ Reply to This | # ]

SoundSource
Authored by: DougAdams on Jun 28, '05 07:52:57PM

SoundSource rules! And it's free! Rave on, Rogue Amoeba!

---
Doug's AppleScripts for iTunes
http://www.dougscripts.com/itunes/



[ Reply to This | # ]
SoundSource
Authored by: sjk on Jun 29, '05 02:13:26AM

Indeed. And a more Tiger-compatible version 1.0.5 was just released.



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: xSmurf on Jun 29, '05 11:13:10AM

Thank you thank you thank you! I Have been looking for this for ages! I am no AppleScripter and need this since I use iCal alarms with AppleScript to control iTunes for a smooth morning wake up. The problem lies in the fact that I use my iMic for audio in my bed room. So I need to change the audio out before heading to bed, not a problem, except for the days I'm gonna have a hardtime waking up the next morning (AKA when I come back home totally drunk ;o). You are a life (well at least job) saver. I will never thank you enough! Kudos!

---
PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: xSmurf on Sep 23, '05 01:49:56PM
Hmmm I've had problems making this work after all. It was working when I had the prefpane already opened and had the sound Output tab opened, but not from the ground up like it normaly runs in the morning... As I rely heavily on this script I couldn't afford for it not to work, even if it meant to manually select the output. So I dropped it for a while. But also caused me problems as I tend forget to do the switch (I have a back up "buzzer" alarm, but it's usually not enough).
I discovered what was missing :
click radio button "Output" of tab group 1 of window "Sound"
delay 2

before "set theRows to every row of table 1 of scroll area 1 of ¬". Otherwise the script grabs the items from the Alert sound list instead of the Output list!

---
Free iPods, now in Canada to! Get yours : http://tinyurl.com/75yta

PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf

[ Reply to This | # ]

An AppleScript to change sound output device
Authored by: martin5211 on Feb 18, '08 04:36:22PM
This script is very useful... Thanks! I've added delay 1 before tell process "System Preferences" and removed the next delay, it worked better on my Mac. Also, I recommend a boolean check and an else statement to switch among two sound options automatically, try to insert the code below into the repeat loop and change the values between quotes:

if (value of text field 1 of aRow as text) ¬
	is equal to "Headphones" and ¬
	(selected of aRow as boolean) ¬
		is equal to false then
	set selected of aRow to true
	exit repeat
else if (value of text field 1 of aRow as text) ¬
	is equal to "Line Out" and ¬
	(selected of aRow as boolean) ¬
		is equal to false then
	set selected of aRow to true
	exit repeat
end if
With AppleScript Utility I can execute these scripts directly from menu bar too.

[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: bcometa on Feb 18, '08 05:15:52PM

thanks for this update, if/then works great!

only one problem for me, which I've always had when using this script, hopefully someone who knows a little about applescript can help:

the script works if the last time i closed system preferences, I WAS IN THE OUTPUT TAB.

but if, for example, i went into sound and changed my input, then closed out of system prefs, then ran this script, it wouldn't work. it won't automatically switch over to the output tab if the last tab i used was input or sound effects.

is there anyway to tell the script "switch to the 'output' tab if not already there"?

thanks all!
brian cometa



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: RobLewis on Apr 08, '08 10:49:52AM

Just (almost) what I was looking for. I want to toggle between 2 sound outputs with a keystroke. Also, removed an extraneous line and fixed the problem that the "Output" pane must be preselected for the script to work.

property speakers : "Headphones" --ext. speakers are connected to headphone output
property headset : "Logitech USB Headset"

tell application "System Preferences" to activate
tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Sound" of menu "View" of menu bar 1
delay 2
click radio button "Output" of tab group 1 of window "sound"
delay 1
set theRows to every row of table 1 of scroll area 1 of tab group 1 of window "sound"
repeat with aRow in theRows
if selected of aRow then
set curr_output to (value of text field 1 of aRow as text)
if curr_output is speakers then set desired_output to headset
if curr_output is headset then set desired_output to speakers
exit repeat
end if
end repeat
repeat with aRow in theRows
if (value of text field 1 of aRow as text) is desired_output then
set selected of aRow to true
exit repeat
end if
end repeat
end tell
end tell
tell application "System Preferences" to quit



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: simphax on Aug 14, '08 02:39:42PM
How can I make this script to run System Preferences hidden? I've tried
set visible of window "System Preferences" to false
but it will not work because System Preferences will not be in focus, and no menus will be accessible...

[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: deweller on Oct 07, '08 08:12:01PM
The applescript was too slow for me, so here is a command-line utility that I wrote to do this:

http://code.google.com/p/switchaudio-osx/downloads/list

(Requires OSX 10.5, I believe)

[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: lras on Jan 21, '09 03:14:41AM
Neat! Your c program works like a charm! I created a QuickSilver trigger that runs a shell script that toggles the audio device using your command. I put your program in my $HOME/bin catalog. I also have installed the growlnotify program that comes with growl. I activated quicksilver (my trigger is alt-space) and then pressed "." (dot) to enter text. I then pasted in the script below

audio=$HOME/bin/SwitchAudioSource; $audio -s "$(basename "$($audio -a -t output | egrep -v "$($audio -c)")" " (output)")" | /usr/local/bin/growlnotify -a "System Preferences" -t "Sound Device"
Then I pressed tab to choose action, and I chose "Run Command In Shell". This will run the commands that were entered into QS's text buffer. I activated QS again and pressed command-' (quote) to get the trigger configuration pane. Then I added a trigger by clicking on the plus icon down on the popup window. This lets you choose which command to run, but the default is the one you just ran, so just add it and pick a nice shortcut (I picked control-alt-command a) QS rocks!

[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: Whosawhatsis on Jul 03, '09 12:05:54PM
I also made a command-line utility, just a cli wrapper around some of SoundSource's code, which I talked the Rogue Amoeba guys into releasing. You can get it here: http://whoshacks.blogspot.com/2009/01/change-audio-devices-via-shell-script.html

---
I was offered a penny for my thoughts, so I gave my two cents... I got ripped off.

[ Reply to This | # ]

An AppleScript to change sound output device
Authored by: gbuell on Sep 24, '09 08:05:57AM

I have a pair of Bluetooth headphones that can act as both a mono and a stereo audio device. In the Sound settings, it's listed twice with the same device name, and under "Type", one listing says "Bluetooth Headset" and the other says "Bluetooth Headphones". I'd like to use this AppleScript to select the "Bluetooth Headphones" option, but both options have the exact same device name listed, so it seems this script won't be able to tell which one to choose. Any suggestions on how to get around this?



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: ziriguidum on Feb 08, '10 04:50:11AM

I get this error when I try both scripts:

System Events got an error:

"System Events got an error: Access for assistive devices is disabled."

and then it highlights this line:
click menu item "Sound" of menu "View" of menu bar 1

i'm using OS X 10.6.2

thanks!



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: bnaro on Feb 11, '10 07:48:06PM
The command-line SwitchAudioSource is great! Thanks! I wanted to alternate between Line-Out and Headphones with the middle click of my Mighty Mouse. So, I saved the following script as an application (it's messy, beware):

tell application "GrowlHelperApp"
	set the allNotificationsList to ¬
		{"Audio Output Source"}
	set the enabledNotificationsList to ¬
		{"Audio Output Source"}
	
	register as application ¬
		"Audio Output Source" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "Script Editor"
	
	set CURRENT to do shell script "SwitchAudioSource -c"
	if CURRENT is "Built-in Line Output" then
		do shell script "SwitchAudioSource -s 'Built-in Output'"
		--	Send a Notification...
		notify with name ¬
			"Audio Output Source" title ¬
			"Audio Output Source" description ¬
			"Headphones enabled" application name "Audio Output Source"
		
	else if CURRENT is "Built-in Output" then
		do shell script "SwitchAudioSource -s 'Built-in Line Output'"
		--	Send a Notification...
		notify with name ¬
			"Audio Output Source" title ¬
			"Audio Output Source" description ¬
			"Line-Out enabled" application name "Audio Output Source"
		
	end if
	
	
end tell
and then through system prefs assigned the middle click to launch the application. Hope it helps someone.
Edited on Feb 11, '10 07:49:20PM by bnaro


[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: bnaro on Feb 12, '10 06:24:41AM

Forgot to add: I'm using growl to notify the switch. So...you'll need that.



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: ba07872 on Aug 07, '11 01:47:11PM

French system example :

tell application "System Preferences" to activate
tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Son" of menu "Présentation" of menu bar 1
delay 2
set theRows to every row of table 1 of scroll area 1 of ¬
tab group 1 of window "son"
click radio button "Sortie" of tab group 1 of window "son"
set theOutputs to {} as list
repeat with aRow in theRows
if (value of text field 1 of aRow as text) ¬
is equal to "Soundflower (2ch)" then
set selected of aRow to true
exit repeat
end if
end repeat
end tell
end tell
tell application "System Preferences" to quit



[ Reply to This | # ]
Updated for Lion: Change Audio Output device
Authored by: idStar on Oct 29, '11 09:38:48AM

I've updated the original script to work with Lion, assuming English. This is a modification of the second script originally posted, that switches directly (without prompting) to the specified output audio device. In my example below, I have it switching to "Headphones". I have another script that is identical, but it switches to "Line Out". I saved these as applications and then just assigned hot-keys on my ergo keyboard to run these scripts. Very handy!



tell application "System Preferences" to activate
tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Sound" of menu "View" of menu bar 1
delay 2
click radio button "Output" of tab group 1 of window "sound"
set theRows to every row of table 1 of scroll area 1 of ¬
tab group 1 of window "sound"
set theOutputs to {} as list
repeat with aRow in theRows
if (value of text field 1 of aRow as text) ¬
is equal to "Headphones" then
set selected of aRow to true
exit repeat
end if
end repeat
end tell
end tell
tell application "System Preferences" to quit



[ Reply to This | # ]
Updated for Lion: Change Audio Output device
Authored by: nicklassa on Jan 08, '12 12:02:56PM

sorry for being such Mac noob but how did did you create the Automator script? I tried creating a new application, then run Apple Script by pasting in the script you posted. I ran a debug a couple of times and finally got it to complete without errors. Then I saved as an application as you said. Does not seem to work after editing my output source name. Am I missing something? TIA



[ Reply to This | # ]
Updated for Lion: Change Audio Output device
Authored by: idStar on Jan 09, '12 06:35:21AM

So, it looks like you got it working? Basically, I opened up the AppleScript editor app, pasted in the script originally posted, and then made my modifications. When you save it, you can save it various formats. By making it save as an "Application", I could target it from things like my Microsoft Keyboard's programmable keys app.

Now, as for changing the name, you must pick a name for the audio device exactly as it appears in your System Preferences. Alternatively, you can ALT-click on the sound icon in the menu bar to see the list of available sound devices you could target. If it's not in that list or you spell it wrong, then of course, you cannot use it.



[ Reply to This | # ]
Updated for Lion: Change Audio Output device
Authored by: nicklassa on Jan 10, '12 12:02:50PM

I ended up using AppleScript editior as you say. I gave up an automator. Its probably trying to make it easier, but the AppleScript editor is more simple in my opinion. Yeah I did have to double check the source name. And now it works!

Thanks



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: trevorep on Apr 20, '12 12:33:32PM

I had trouble getting the second script to select my USB sound card but it would select the built in speakers no problem. I opened up System Information and selected USB, turns out the name of my device had a couple spaces on the end of it. I selected the name, copied and pasted it into the script and it worked. Hopefully this helps anyone else having the same issue.



[ Reply to This | # ]
An AppleScript to change sound output device
Authored by: Lri on Sep 03, '12 09:25:09PM

The second script could be simplified a lot:

  • reveal anchor "" of pane id "" reveals a specific tab
  • You don't have to make System Preferences frontmost or visible
  • You could use a single expression to select the UI element to click
tell application "System Preferences"
    reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
    tell table 1 of scroll area 1 of tab group 1 of window 1
        select (row 1 where value of text field 1 is "Internal Speakers")
    end tell
end tell
quit application "System Preferences"


[ Reply to This | # ]