Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript to switch audio output

I am having a little trouble with this applescript which is intended to present a dialog with some choices and switch the audio output accordingly. Any help would be greatly appreciated. I've started a gist too.


tell application "System Preferences" to activate

tell application "System Events"

get properties

set asrc to choose from list {"Internal Speakers", "Living Room", "Girl's Room", "Master Bedroom"} with title "Sound Picker" default items {"Internal Speakers"}

display dialog "src is: " & asrc



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 asrc then

set selected of aRow to true

exit repeat

end if

end repeat

end tell

end tell

tell application "System Preferences" to quit

Posted on Dec 25, 2012 6:24 PM

Reply
9 replies

May 3, 2017 5:01 PM in response to mbierman

Hello all.


I've made three nice little scripts to switch between audio outputs (and inputs with two of them, as per example below). Largely copied from an example found on a thread at macworld (linked at bottom), but to get it to work properly I needed to add the delay lines. Simply replace the output name in the select line with your output (or input) as shown in the System Preferences, Sound window. If all you want to do is change outputs, discard the second half of the script. Add a Quit "System Preferences" line at the bottom if you want to close the window when finished.

tell application "System Preferences"


activate

reveal anchor "Output" of pane "Sound"

end tell

delay 1

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 "HDSPe RayDat (Slot-2)")

end tell

end tell

tell application "System Preferences"


activate

reveal anchor "Input" of pane "Sound"

end tell

delay 1

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 "HDSPe RayDat (Slot-2)")

end tell

end tell


After preparing the three scripts in Script Editor I saved them as apps and put them on the Desktop for easy use. Good luck with these. Rick.

tel

http://hints.macworld.com/article.php?story=20050614171126634

Dec 26, 2012 5:31 AM in response to mbierman

Hi,


The following script, tested under OS X 10.8.2, should do the trick.



set asrc to (choose from list {"Internal Speakers", "Living Room", "Girl's Room", "Master Bedroom"} with title "Sound Picker" default items {"Internal Speakers"}) as text

if result is "false" then return


display dialog "src is: " & asrc


tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

activate


tell application "System Events"

tell process "System Preferences"

select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is asrc)

end tell

end tell


quit

end tell


To have the script run in the background, just comment out the “activate” line above.


You might also have a look at this web page.



Message was edited by: Pierre L. (fixed the web link)

Jan 17, 2013 4:04 PM in response to mbierman

I'm having a similar, but slightly different issue.


I'd like a script to run that automatically picks "display audio" for the speakers and "display audio" for the mic when I connect my Macbook Pro to my Thunderbolt display. It doesn't seem to do this on its own. So frustrating.


Can we make scripts run automatically upon detecting something like a Thunderbolt monitor?


If not, I already have a program called ControlPane that does detect things like the monitor. It can also run scripts upon detecting, so I would just need two scripts to activate/deactivate these prefs (i.e. one to change both mic and output to "display audio" when Thunderbolt connected, and one to change both back to "Internal speakers/mic" when I disconnect the monitor).


Can somebody help with the script portion at least? Thanks!

Feb 22, 2014 3:13 PM in response to Pierre L.

Pierre,

As you seem the guy to speak to in regards to Apple script, do you have any idea why the second part of this script does not work? The goal of the script is to be able to switch between 2 different input/output , one being the Internal and the other being an USB headset.

The non working part is after --USB

Any help would be greatly appreciated

Merci d'avance




property cancel : "Cancel"

tell application "System Preferences" to activate

activate --making dialogue box frontmost


set answer to button returned of (display dialog "

Select Sound Input / Output

" with icon file ((path to me) & "Contents:Resources:icn:SoundPrefcopy.icns" as string) with title "Select Sound Input/Output " buttons {"iMac", "USB", "Cancel"} default button 2)


-- Internal


if answer is equal to "iMac" then

tell application "System Preferences"

reveal anchor "input" of pane id "com.apple.preference.sound"

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 microphone")

end tell

end tell

end tell

tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

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

end tell

-- USB (Non working)

if answer is equal to "USB" then

tell application "System Preferences"

reveal anchor "input" of pane id "com.apple.preference.sound"

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 2 where value of text field 1 is "Microsoft LifeChat LX-3000")

end tell

end tell

end tell

tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

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 2 where value of text field 1 is "Microsoft LifeChat LX-3000")

end tell

end tell

end tell

--Cancel

if answer is equal to "Cancel" then

cancel

end if

end if

end if

tell application "System Preferences" to quit

Mar 5, 2016 1:38 PM in response to Pierre L.

I'd like to modify this script not to choose from a list but to choose a default usb audio device.

So far I Have:

set USB_Audio to "USB Audio Device"

launch application "System Preferences"

tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

tell application "System Events"

tell process "System Preferences"

select (row 1 of table 1 of scroll area 1 of tab group 1 of windowsound whose value of text field 1 is USB_Audio)

end tell

end tell

quit

end tell

and when run I get

error "System Events got an error: Can’t make sound into type integer." number -1700 from sound to integer

Mar 5, 2016 2:09 PM in response to lightninginajar

solved it with the following

set audio to ("USB Audio Device") as text

if result is "false" then return



tell application "System Preferences"

reveal anchor "output" of pane id "com.apple.preference.sound"

activate

tell application "System Events"

tell process "System Preferences"

select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is audio)

end tell

end tell

quit

end tell

applescript to switch audio output

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.