setting sound input

Does anyone have a good way to set the “select a device for sound input” in the system preferences sound pane to the Internal microphone or a external microphone the user connects. In addition I wanted to set the “input volume” to maximum and click “use ambient noise reduction”. These parameters do not appear in the dictionary

thanks for any help

Kevin

Browser: Safari 600.3.18
Operating System: Mac OS X (10.10)

Hi Beeman,

It has been written. :wink: You must use uielement scripting. Not too long ago there was a post on this. If it is still written.

gl,
kel

I have searched but can’t find the thread you are referring to. Do you know what I should enter into the search field to find it

Searching for “sound input” I got :
http://macscripter.net/viewtopic.php?id=42335

Yvan KOENIG (VALLAURIS, France) mercredi 10 juin 2015 22:50:00

I reviewed the thread…

This script opens up the input pane:

tell application “System Preferences”
activate
reveal anchor “input” of pane id “com.apple.preference.sound”
end tell

However I don’t see anyway to set the “input volume” to maximum and make sure the “use ambient noise reduction” is checked

Anyone have an idea how to do that?

kevin

I think these commands still work:

set volume alert volume 75
set volume input volume 50
set volume output volume 50
set volume without output muted
get volume settings

(*
set volumeSettings to (get volume settings)
if (output muted of volumeSettings) then
	display dialog "Volume: Muted"
else
	set theVolume to (output volume of volumeSettings)
	display dialog (("Volume: " & theVolume) as string)
end if
*)

you were right

set volume input volume 50 sets the input volume.

How do I make sure the “use ambient noise reduction” is checked?

Here is the script so far

tell application “System Preferences”
activate
reveal anchor “input” of pane id “com.apple.preference.sound”
set volume input volume 100
(* input volume 1-100 with 100 highest*)

end tell

thanks for all the help

Kevin

I can not seem to figure out how to get the names of the input audio source choices and how to select them.

I have this so far

tell application “System Preferences” to activate
tell application “System Preferences”
reveal anchor “input” 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 “AT2020USB+”)

	--set InputSource to value of (record 1)
	--could use this to set recording options in program
	
end tell

end tell
–quit application “System Preferences”

this always errors as

error “System Events got an error: Can’t get record 1 of table 1 of scroll area 1 of tab group 1 of window 1 of process "System Preferences".” number -1728 from record 1 of table 1 of scroll area 1 of tab group 1 of window 1 of process “System Preferences”

The scroll area always has “Internal Microphone” as row 1 and when you connect an external microphone (such as “AT2020USB+”) it row 2.

I would like to get the name of the input sources into variables that I can ask the user to select. I can then select it for them and set it as a preference in the program I am using that calls this applescript.

I am stumped
anyone have answer for this

Model: macbook pro
AppleScript: 2.6.1
Browser: Safari 537.77.4
Operating System: Mac OS X (10.10)

Hey Beeman,

This works for me on OSX 10.10.4:


tell application "System Preferences"
	if not running then run # Works around 'activate' bug.
	activate
	reveal anchor "input" 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 (first row whose value of text field 1 is "Soundflower (2ch)")
	end tell
end tell

Here’s some ideas for doing forensics with System Events:


tell application "System Events" to tell process "System Preferences"
	tell table 1 of scroll area 1 of tab group 1 of window 1
		
		set tableAttributes to its attributes
		set tableAttributesProps to properties of its attributes
		
		set tableUiElements to UI elements
		set tableUiElementProps to properties of UI elements
		set tableUiElementNames to name of UI elements
		set tableUiElementValues to value of UI elements
		
		tell rows
			set rowUiElements to UI elements
			set rowUiElementProps to properties of UI elements
			set rowUiElementNames to name of UI elements
			set rowUiElementValues to value of UI elements
		end tell
		
		first row whose value of text field 1 is "Soundflower (2ch)"
		
	end tell
end tell

If you have Xcode installed then you have the Accessibility Inspector.app on your system. It’s very opaque but better than nothing.

Better still is Bill Cheeseman’s UI Browser utility. At $55.00 U.S. it is not inexpensive, but it has kept me from pulling my hair out so often that I think it worth the money ” even for non-professional scripters.

-Chris


MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.10.4

Thanks for all the help…
totally forgot that I have the UI Browser utility. I updated my computer and never put it back on. I will check that out and post my results here

Here is my solution that works. The scripting is not very elegant, so if anyone has suggestions to streamline the code it would be much appreciated

–get input sources available from Sound Pane of System Preferences

tell application “System Preferences”
reveal anchor “input” of pane id “com.apple.preference.sound”
end tell
tell application “System Events” to tell process “System Preferences”

set InputSource1 to (get value of text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound")
--Row 1 is always "Internal Microphone"
try
	set InputSource2 to (get value of text field 1 of row 2 of table 1 of scroll area 1 of tab group 1 of window "Sound")
	--Row 2, if it exists, is the just attached external microphone name
on error number -1719
	--trap for error when no USB microphone attached
	set InputSource2 to "External Mic not attached"
end try

end tell
quit application “System Preferences”

– get Users choice of audio input

set userCanceled to false
try
set dialogResult to display dialog ¬
“Please select your input source” buttons {InputSource1, InputSource2, “Cancel”} ¬
default button “Cancel” cancel button ¬
“Cancel”
on error number -128
set userCanceled to true
end try
if userCanceled then
set InputChoice to “User cancelled.”
– statements to execute when user cancels
display notification InputChoice with title “Alert”
setFilemakerpro(InputChoice)

else if button returned of dialogResult is InputSource1 then
set InputChoice to InputSource1
– statements to process input source is InputSource1
SetInputChoice(InputChoice)
setFilemakerpro(InputChoice)
else if button returned of dialogResult is InputSource2 then

set InputChoice to InputSource2
-- statements to process input source is InputSource2
if InputChoice is "External Mic not attached" then
	display notification "There is no external microphone connected. Please connect one and try again later." with title "Alert"
	setFilemakerpro(InputChoice)
else
	SetInputChoice(InputChoice)
	setFilemakerpro(InputChoice)
end if

end if

on SetInputChoice(InputChoice)

tell application "System Preferences"
	reveal anchor "input" of pane id "com.apple.preference.sound"
	
end tell
tell application "System Events" to tell process "System Preferences"
	
	if InputChoice is "Internal Microphone" then
		--Row 1 is always "Internal Microphone"
		select text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound"
		-- make sure ambient noise reduction is checked
		if (get value of checkbox "Use ambient noise reduction" of tab group 1 of window "Sound") = 0 then
			-- the value of the checkbox is 0 if unchecked; 1 if checked
			click checkbox "Use ambient noise reduction" of tab group 1 of window "Sound"
		end if
	else
		select text field 1 of row 2 of table 1 of scroll area 1 of tab group 1 of window "Sound"
		--Row 2 is the external microphone
	end if
end tell
tell application "System Preferences"
	if InputChoice is "Internal Microphone" then
		set volume input volume 100 -- set to maximum for interanl microphone
	else
		set volume input volume 75 -- set volume lower for external microphones
	end if
	
	(* other optional settings in sound pane

set volumeSettings to (get volume settings)
if (output muted of volumeSettings) then
display dialog “Volume: Muted”
else
set theVolume to (output volume of volumeSettings)
display dialog (("Volume: " & theVolume) as string)
end if

set volume alert volume 75
set volume input volume 50
set volume output volume 50
set volume without output muted
get volume settings
*)

end tell
quit application "System Preferences"

end SetInputChoice

on setFilemakerpro(InputChoice)

tell application "FileMaker Pro Advanced"
	tell table "Preferences"
		tell record 1
			set cell "recordoptions1" to InputChoice --let Filemaker pro know which input to use
		end tell
	end tell
	
end tell

end setFilemakerpro

Model: macbook pro
AppleScript: 2.6.1
Browser: Safari 537.77.4
Operating System: Mac OS X (10.10)

Hello

I made a few changes so that the code triggering the prefPane is no longer localisation dependent.
I’m unable to do the same for the code triggering FileMaker which I don’t own/use.


--get input sources available from Sound Pane of System Preferences


tell application "System Preferences"
	reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
	set frontmost to true
	set Sound_loc to my getLocalWindowName()
	set InputSource1 to (get value of text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window Sound_loc)
	--Row 1 is always "Internal Microphone"
	try
		set InputSource2 to (get value of text field 1 of row 2 of table 1 of scroll area 1 of tab group 1 of window Sound_loc)
		--Row 2, if it exists, is the just attached external microphone name
	on error number -1719
		--trap for error when no USB microphone attached
		set InputSource2 to "External Mic not attached"
	end try
	
	
end tell
quit application "System Preferences"


-- get Users choice of audio input

set userCanceled to false
try
	set dialogResult to display dialog ¬
		"Please select your input source" buttons {InputSource1, InputSource2, "Cancel"} ¬
		default button "Cancel" cancel button ¬
		"Cancel"
on error number -128
	set userCanceled to true
end try
if userCanceled then
	set InputChoice to "User cancelled."
	-- statements to execute when user cancels
	display notification InputChoice with title "Alert"
	setFilemakerpro(InputChoice)
	
else if button returned of dialogResult is InputSource1 then
	set InputChoice to InputSource1
	-- statements to process input source is InputSource1
	SetInputChoice(InputChoice)
	setFilemakerpro(InputChoice)
else if button returned of dialogResult is InputSource2 then
	
	set InputChoice to InputSource2
	-- statements to process input source is InputSource2
	if InputChoice is "External Mic not attached" then
		display notification "There is no external microphone connected. Please connect one and try again later." with title "Alert"
		setFilemakerpro(InputChoice)
	else
		SetInputChoice(InputChoice)
		setFilemakerpro(InputChoice)
	end if
end if

on SetInputChoice(InputChoice)
	
	
	tell application "System Preferences"
		reveal anchor "input" of pane id "com.apple.preference.sound"
	end tell
	tell application "System Events" to tell process "System Preferences"
		set frontmost to true
		set Sound_loc to my getLocalWindowName()
		set InputSource1 to (get value of text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window Sound_loc)
		--Row 1 is always "Internal Microphone"
		if InputChoice is InputSource1 then
			--Row 1 is always "Internal Microphone"
			select text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window Sound_loc
			-- make sure ambient noise reduction is checked
			if (get value of checkbox 1 of tab group 1 of window Sound_loc) = 0 then
				-- the value of the checkbox is 0 if unchecked; 1 if checked
				click checkbox 1 of tab group 1 of window Sound_loc
			end if
		else
			select text field 1 of row 2 of table 1 of scroll area 1 of tab group 1 of window Sound_loc
			--Row 2 is the external microphone
		end if
	end tell
	tell application "System Preferences"
		if InputChoice is InputSource1 then
			set volume input volume 100 -- set to maximum for interanl microphone
		else
			set volume input volume 75 -- set volume lower for external microphones
		end if
		
		(* other optional settings in sound pane
        
set volumeSettings to (get volume settings)
if (output muted of volumeSettings) then
    display dialog "Volume: Muted"
else
    set theVolume to (output volume of volumeSettings)
    display dialog (("Volume: " & theVolume) as string)
end if

set volume alert volume 75
set volume input volume 50
set volume output volume 50
set volume without output muted
get volume settings
*)
		
	end tell
	quit application "System Preferences"
	
end SetInputChoice

on setFilemakerpro(InputChoice)
	
	tell application "FileMaker Pro Advanced"
		tell table "Preferences"
			tell record 1
				set cell "recordoptions1" to InputChoice --let Filemaker pro know which input to use
			end tell
		end tell
		
	end tell
end setFilemakerpro

on getLocalWindowName()
	set p2pane to (path to library folder from system domain as text) & "PreferencePanes:Sound.prefPane:"
	return localized string "CFBundleName" from table "InfoPlist" in bundle (p2pane as «class furl»)
end getLocalWindowName

Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) jeudi 30 juillet 2015 10:00:31

thank you for your help.
I have a question, though.

Why is

“I made a few changes so that the code triggering the prefPane is no longer localisation dependent.”

this an important change. What error or possible complication are you trying to avoid. This is for my education.

thank you again

Kevin Miller

Browser: Safari 600.3.18
Operating System: Mac OS X (10.10)