Troubleshoot Sound Output Script

Hi folks.

I’m having problems with my scripts that choose sound outputs in System Preferences. This is one for my Logitech headset:

property speakers : "Line Out" --ext. speakers are connected to headphone output
property headset : "Headphones"
property Logitech : "Logitech H555 Headset"
set desired_output to Logitech

tell application "System Preferences" to activate
tell application "System Events"
	
	tell process "System Preferences"
		click menu item "Sound" of menu "View" of menu bar 1
		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"
		
		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

Here’s the problem. The System Preferences window comes up, but then the Sound Control Panel doesn’t get switched to. This is what error I am getting:

Any idea how I can make this happen?

Cheers

with a quick test I found that a small delay helped, probably not the best way to do it…

tell application "System Preferences" to activate
delay 1
tell application "System Events"

Hi there. I have a 2 second delay in another version (set sound to speakers) and I still get the problem. This error comes up:

System Events got an error: Can’t get window “sound” of process “System Preferences”.

Hi,

there’s an better way to open the specific tab of the Preference Pane


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

Excellent. This passes through this bottleneck. Thanks for the post.

Cheers