Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[28,170 views]  

An AppleScript to change sound output device | 14 comments | Create New Account
Click here to return to the 'An AppleScript to change sound output device' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
SoundSource
Authored by: sinjin on Tue, Jun 28 2005 at 12:28PM PDT
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 Tue, Jun 28 2005 at 7:52PM PDT
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 Wed, Jun 29 2005 at 2:13AM PDT
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 Wed, Jun 29 2005 at 11:13AM PDT
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 Fri, Sep 23 2005 at 1:49PM PDT
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 Mon, Feb 18 2008 at 4:36PM PST
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 Mon, Feb 18 2008 at 5:15PM PST
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 Tue, Apr 8 2008 at 10:49AM PDT
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 Thu, Aug 14 2008 at 2:39PM PDT
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 Tue, Oct 7 2008 at 8:12PM PDT
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 Wed, Jan 21 2009 at 3:14AM PST
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 Fri, Jul 3 2009 at 12:05PM PDT
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 Thu, Sep 24 2009 at 8:05AM PDT
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 Mon, Feb 8 2010 at 4:50AM PST
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 | # ]