Cat logomenu

Run this AppleScript to Hear your Music in Glorious Mono

I often work while my son is sleeping or napping. My desk is right outside his room, so if I want to listen to music, I need to use headphones. But I’m also often the primary caregiver at the moment and need to keep one ear open so I can hear if he needs something, which means if I want to listen to music I need to use headphone. Fortunately, I use earbuds, so I can just put one in my ear and leave the other in its case.

Music is generally mixed in stereo. I don’t want to miss out on one whole channel, so I take advantage of a MacOS Accessibility setting that will change the audio output to mono. Annoyingly, that setting is buried behind four clicks, followed by a subsequent click or Cmd+Q to close the System Preferences app.

So I took my first dive into AppleScript, starting with this script by Doug Adams that does roughly what I want, which is:

  1. Open the Accessibility -> Hearing -> Audio preference pane
  2. Toggle the “Play stereo audio as mono” checkbox
  3. Close System Preferences

EXCEPT.

It does a couple things I don’t like and leaves out something I want.

Things I don’t like:

  • Presents a dialog to the user asking for confirmation of the action to be performed (disabling/enabling “Play stereo audio as mono”). WHO NEEDS THAT I AM A BUSY PROGRAMMER WHO EXPECTS MY COMMANDS TO BE OBEYED COMPUTER
  • Closes System Preferences even if it was open before running the script. Leave my existing state alone!

Thing I want that it leaves out:

  • A delay in closing System Preferences so I can actually see whether the checkbox is checked or unchecked. I often forget which state I’m in and want to make sure I’m performing the correct action. Yes, of course this would be unnecessary if I left in the confirmation dialog, but I want what I want.

Anyway, after some googling and tinkering, here’s what I came up with. I won’t annotate it, because AppleScript is so simple that I imagine anyone who actually cares what this does can follow it. I know this because I sent it to two non-programmer friends, and they figured out what the script did on their own.

if application "System Preferences" is running then
	set systemPreferencesWasOpen to true
else
	set systemPreferencesWasOpen to false
end if
tell application "System Preferences"
	reveal anchor "Hearing" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events"
	tell application process "System Preferences"
		set frontmost to true
		tell group 1 of window "Accessibility"
			# pre-Mojave - replace the line above with the one below
			# tell window "Accessibility"
			set monoStereoCheckbox to checkbox "Play stereo audio as mono"
			tell monoStereoCheckbox to click
		end tell
	end tell
end tell
if application "System Preferences" is running then
	if systemPreferencesWasOpen is equal to false then
		delay 1.5
		tell application "System Preferences" to quit
	end if
end if

May this be of use to all you Mac-user programmer parents who work within earshot of your sleeping children and want to use just one headphone to listen to stereo music. Say hi to me on Twitter; I want to meet all six of you.

Give us a share!