Sound Output Detection

I’m building a startup script, something that gets my mac just the way I like it whenever I start. I wanted to have it set the volume, but I wanted it to set different values if I had on headphones or if internal speakers were active. Is this possible? Thanks!

Model: iBook G4
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

if headphones and internal speakers use the same output device,
this means, if the headphones mute the internal speakers when connected,
there is AFAIK no chance to detect something.

If you change settings in the sound prefpane to switch to headphones, then you can use this script.
Because the .plist file includes the primary MAC address of the computer, it must first be detected.

set MACaddress to do shell script "ifconfig en0 | awk '/ether/ {print $2}'"
-- using Tiger there is a "shortcut"
-- set MACaddress to primary Ethernet address of (system info)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set MACaddress to text items of MACaddress
set AppleScript's text item delimiters to oldDelims
set MACaddress to text items of MACaddress as string
set DefAudioOut to do shell script "defaults read ~/Library/Preferences/ByHost/.GlobalPreferences." & MACaddress & " com.apple.audio.CoreAudio.DefaultOutputDevice"

Thanks for your reply,
So there is NO way to detect what it is set as?

the status in the prefpane changes to “Headphones” when connected,
but nothing happens in the .plist-file.
Maybe there is another place where the information is stored, but I have no idea where

I’m not sure how my dual-core G5 is wired, but it used to be the case that the change was entirely electromechanical - plugging in headphones disconnected the speaker(s) directly. While this may be indicated and displayed (I don’t have any headphones, but my system doesn’t indicate that I’ve got external speakers plugged in), there’s no need for a plist change - the user either plugs in the phones or he doesn’t and that’s all the system cares about.

So nothing??? I was hoping I could use System Events to read the “Radio Box”? Is that the right term for it?

In my box (details below), sound runs thruough a built-in BurrBrown PCM3052 card that handles Line Level Input, SPDIF Digital output, Headphones, Internal Speakers, and Line Level Output - the last being what I get when I plug in internal speakers. I can see all this in the System Profiler or by running this script which creates a file on your desktop with the same info:

tell application "Finder" to set tName to name of startup disk
do shell script "/usr/sbin/system_profiler > " & POSIX path of (path to desktop as text) & tName & ".profile.txt"

Clearly, the BB3052 ‘knows’ what’s plugged in, and ‘informs’ the Sound preference pane too, but I have been unable to discover how it does it - Plugging in my external speakers changes the Sound pane’s first line to “Line Out” immediately, unplugging it changes the reading to “Internal Speakers” and the controls available are switched (Internal Speakers doesn’t have a “Balance” control). Maybe a test for that…

After some fiddling, I discovered that I could get the ‘value’ of the text field that contains the ‘Line Out’, ‘Headphones’, or ‘Internal Speakers’ announcement with GUI scripting (although the test was performed with the Sound Pane Open, so you may have to do that first):

activate application "System Preferences"
tell application "System Events" to tell process "System Preferences" to set S to value of text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound"

so you could test to see what it said and act accordingly. Your mileage may vary.

EDIT: If System Preferences is NOT running (which it won’t be at startup), this works (but fails if SP is open to a pane so quit SP before you try it):

activate application "System Preferences"
tell application "System Events" to tell process "System Preferences"
	click button "Sound" of scroll area 1 of window "System Preferences"
	delay 1
	set S to value of text field 1 of row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound"
end tell

You would then test the value of S and set your output volume accordingly.

Thank you so much!!! That is EXACTLY what I needed, you are perfect!

You’re welcome - I was kind of interested myself. Here’s a better version by Kai (from another list):


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

It has the advantage of running whether the System Preferences.app is open or not, no matter what the sound pane was set to before. I strongly recommend changing to this version, visciousfishes3000.

That worked wonderfully for my app, but I’m trying something new. I often switch from speakers to headphones, and equally often I am jolted by full volume blowing out my eardrums! I was thinking I could either grow a brain, or build a little stay open script that would change the volume when the output changed. Has anyone had any luck looking in plists for changes when headphones are connected?

As I said up in post #7, the words Headphones, Line Out, etc. are not to be found in an extensive contents search of my Library folders. I don’t know where that pref is stored, only that it works. :frowning: