« Multiple Database Connections in Ruby on Rails | Main | The Last Hope »

Mac Pro Optical Digital Audio Output and the Mute button

One of the great things about the new Mac Pro is its full size optical digital output, which feeds pure and clean digital audio to my receiver and on to the speakers in the office or main room. The downside of this is that the implementation is such that there is no master volume or mute capability; the optical out is a direct hard line out of whatever your apps hand it. Controlling volume with iTunes is no big deal, but it's really really annoying when you open a website with blaring loud awful music and have no way to quickly mute the sound. The best way I know is to open the System Prefs and change the audio output. So I figured what better time to hack a little AppleScript to at least automate the output switch.

I've only touched AppleScript on one other occasion, and I must say it's a bit of a mystery to me. It's sort of like the developers tried so hard to make the code like English that it turns into a confusing mess of pseudocode, and I've yet to find any comprehensive reference of what's possible in quick glances at what seems to be a maze of documentation. (Any pointers to such a reference would be much appreciated, BTW) But, there's always Google. To be fair, I've only spent a few moments with it and was able to figure out what I needed to make my mute button work, albeit with a big chunk of copy and paste from the O'Reilly Mac Devcenter blog.

So, without further ado, here's the code. You'll need to use AppleScript Utility (in /Applications/Applescript) to "Enable GUI Scripting" for this to work (the script will help you out with this if you haven't already done so). Use the Applescript Editor (also in /Applications/Applescript) to save the code (it seems to do some compilation in the process).

The long lines are hard to read here on the site, so the code is also available here.

(*
Modified from the script found at http://www.oreillynet.com/mac/blog/2006/07/applescript_audio_output_switc.html to
switch between Digital output and Internal speakers, to act as a mute when using Digital out
*)

(*
This script toggles between two audio outputs in the "Sound" pane in "System Preferences" and adjusts the volume. 
Modified from a script at http://forums.macosxhints.com/showthread.php?t=45384 
to add volume control and GUI scripting detection. --David Battino, www.batmosphere.com.

USES GUI SCRIPTING; "ENABLE ASSISTIVE DEVICES" OPTION MUST BE CHECKED IN THE "UNIVERSAL ACCESS" PREFERENCE PANE
*)

tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
	if UI elements enabled then
		try
			tell application process "System Preferences"
				tell tab group 1 of window "Sound"
					click radio button "Output"
					if (selected of row 3 of table 1 of scroll area 1) then -- Digital out is selected
						set selected of row 1 of table 1 of scroll area 1 to true
						tell application "Finder"
							set volume 0
						end tell
					else
						set selected of row 3 of table 1 of scroll area 1 to true -- Internal speakers are selected
						tell application "Finder"
							set volume 2
						end tell
					end if
				end tell
			end tell
			tell application "System Preferences" to quit
		on error
			tell me to activate
			display dialog "Problem selecting audio device." buttons {"Whoops!"} default button 1
		end try
	else --GUI scripting is disabled
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
		end tell
		display dialog "Please check the box called \"Enable access for assistive devices.\"" buttons {"Okay"} with icon 1 default button 1
	end if
end tell

The script opens the System Preferences dialog, clicks on Sound, selects the Output tab, and toggles between outputs 1 and 3, which in my case are "Internal Speakers" and "Digital Out". When setting the output to the internal speakers it also sets the volume to 0 (mute). Perfect.

Now with a Quicksilver trigger, the script runs when I press F10 (in my case fn+F10, since I have the other special keys enabled) and mute is toggled. I also used the Quicksilver triggers and the iTunes plugin to map F11 and F12 to iTunes volume up and down. Not quite as nice as master volume + mute, but hey, it's digital to the amp — worth a little annoyance.

Music

Where am I?

Recent Photos

Powered by
Movable Type 3.35