Script sound output preferences?

I admit total ignorance. Is there a script to automatically change sound output preferences from internal speaker to my headset when I answer a SKYPE call? I can’t create an alias to get to the window quickly, so I miss the call. Please advise or re-direct. iMac G5 OS X 10.4.8

Model: iMac G5
AppleScript: 1.0
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Tell me if this works on your machine, and if it does, what is the list shown in S (its output)?


tell application "System Preferences" to reveal anchor "output" of pane id "com.apple.preference.sound"
tell application "System Events" to tell (table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences") to set S to value of text field 1 of rows

If you haven’t used this forum before, click the link > Open this Scriptlet in your Editor: in the AppleScript box above and it will download the script to your Script Editor (with your permission).

That was fun. I clicked the ‘open this script’ link, it appeared in the script editor and the sound output control panel opened right up to whatever I had left it on. The results window said {“Internal speakers”, “Logitech USB Headset”}

It shouldn’t matter where you left the System Preferences window the last time you used it - that script will always go to that pane.

Try this one, it should toggle between the two entries:


tell application "System Preferences" to reveal anchor "output" of pane id "com.apple.preference.sound"
tell application "System Events" to tell (table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences")
	set S to selected of rows
	if item 1 of S is false then
		select row 1
	else
		select row 2
	end if
end tell
tell application "System Preferences" to quit

[EDIT: added quit System Prefs]

The results window said row 2 of table 1 of scroll area 1 of tab group 1 of window “Sound” of application process “System Preferences” of application "System Even

That works perfectly! I was worried about changing the setting, not the position of the pane. You’re not obsessed. Just great! Now I can answer and hang up without a scramble between hearing the ring and using the headset! Hoorah!
I’m soooo embarrased to ask. I’ve been trying to understand the instructions. I saved it as an application and put it in my dock to click when I answer an incoming call. Is that the right way to use it?
And
Thank you, Thank you, Thank you, Thank you, Thank you! I could feel myself ageing as I tried to figure this out before your help! -NJ Anderson, Boston

Graphical User Interface (GUI) scripting of System Preferences (which isn’t otherwise scriptable) is always a nightmare of finding the right place to do something and then discovering what to do there (i.e. click, select, set something to true, etc.)

The first line (due to Kai Edwards):


tell application "System Preferences" to reveal anchor "output" of pane id "com.apple.preference.sound"

Opens the System Preferences window, then (the reveal anchor part) tunes in to the correct panel’s “Output” pane. You (or I) would never guess how to do that.

The next line (now that the right pane is open) tells System Events - the controller of all things GUI - to drill down through the elements of the Sound pane’s Output page to the rows of the text box. Another way to say the same thing would be:

tell application "System Events" to set S to selected (a keyword - one of the two rows is always selected, and selected takes values of true or false) of rows of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences"

But if we’d said it that way, we would have had to repeat the whole thing with select row 1 and select row 2, so we tell all the first part to set S, then to select a row depending on S.

Confusing, isn’t it? Takes quite a while to master it and it’s always “fiddly”.

Thank You! And thanks for the explanation too. With that, maybe I’ll be able to accomplish my next project of switching printers using the same method. Just as soon as I get the computer to work with either of them in the first place! --But that’s another day! NJ Anderson, Boston

Hi NJ,

switching printers is much easier

tell application "Printer Setup Utility" to set current printer to myprinter

Or if you have two and want to toggle between them:

tell application "Printer Setup Utility"
	activate
	set myPrinters to every printer
	set DefaultPrinter to current printer
	if item 1 of myPrinters is DefaultPrinter then
		set current printer to item 2 of myPrinters
	else
		set current printer to item 1 of myPrinters
	end if
	-- display dialog (name of current printer) as text default button 1 buttons {"OK"} giving up after 3 -- Optional
	quit
end tell

I LOVE this bulletin board! Thanks guys!

I’m a freelance artist/grahic designer/website designer/Mac repair and builder. My first computer was an Apple SE HD “Never trust a computer you can’t pick up” That’s probably why my laserwriter mostly sent back postscript errors, -it was too heavy to pick up.

But I never even thought about scripting until this week and it looks like you’ve got me covered!

Thanks again
NJ