GUI Scripting

August 16 2013 - 9:34 am

Toggle Stereo/Mono Audio Output

Correspondent Simon Crosbie has set up an amp and pair of speakers in his workshop which is connected to an Airport Express. Unfortunately, the speakers are some distance apart, so that he may be near one speaker or another at any time, and will only hear that channel’s output. Simon wanted to know if there’s a way to toggle between stereo and mono output so he can hear more than just half of a stereo recording.

Yes, there is a way. Go to System Preferences > Accessibility. Choose “Audio” in the left-hand list and in the panel that appears click the checkbox next to “Play stereo audio as mono”.

Goodnight everybody!

Wait a minute. I almost forgot I wrote this script to do it:

tell application "System Preferences"

reveal anchor "Hearing" of pane id ""

end tell

tell application "System Events"

tell application process "System Preferences"

set frontmost to true

tell window "Accessibility"

##–> pre 10.9 set monoStereoCheckbox to checkbox 2 of group 1

set monoStereoCheckbox to checkbox “Play stereo audio as mono”

if (get value of monoStereoCheckbox) as boolean is true then

set ddMessage to "Switch to STEREO output?"


set ddMessage to "Switch to MONO output?"

end if

if button returned of (display dialog ddMessage buttons {"No", "Yes"} default button 2) is "Yes" then

tell monoStereoCheckbox to click

end if

end tell

end tell

end tell

if application "System Preferences" is running then

tell application "System Preferences" to quit

end if

[UPDATE November 13, 2013: The original script has been updated for Mavericks. Note the commented line for pre-10.9 systems and the line that follows it. Set the monoStereoCheckbox variable using one or the other depending on the OS.]

[UPDATE October 17, 2014: Another change for compatibility with Yosemite. The line “set frontmost to true” is inserted right after the first tell application process "System Preferences" line near the beginning.]

I would save this script in the system wide Scripts menu (install it in ~/Library/Scripts/). Because the script uses (gulp) GUI scripting you must make sure that “Enable access for assitive devices” is checked in the Accessibility System Preferences panel.

Smarties among you can figure out how to switch mono/stereo when using AppleScript to change AirPlay speakers.

And bear in mind that while the script works with the current OS (and probably an OS that may be released this Fall), Apple may change the layout of the System Preferences panels in a future update, in which case the GUI scripting will have to be updated.

October 9 2012 - 12:27 pm

I Hate GUI Scripting Except When I Don’t

Sometimes the only way to script anything that isn’t explicitly in an app’s scripting definition is to use GUI Scripting. I always look for some other way before resorting to GUI Scripting since it can get pretty oogly and is dependent on the app’s GUI; if the developer changes the GUI then the script can be rendered useless. With that said, a Correspondent writes:

“In iTunes, I utilize checkboxes to determine what songs are synced to my iPhone. I do not utilize playlists because I want to keep complete playlists in my iTunes library while still syncing them to my iPhone, minus any unchecked songs. The problem is that iTunes will not play unchecked songs continuously, which annoys me. I recently discovered, however, a preference called “Show list checkboxes”. Unchecking this will hide the checkboxes column and allow me to play my songs uninterrupted, but when I want to sync my iPhone I have to show the checkboxes again so that it won’t try to sync my entire huge library.

I was wondering if there was any way that you could create a script that toggles this preference on & off, in order to reduce the number of clicks required to switch between playing all of my music and syncing my iPhone. This would be much appreciated.”

There’s no explicit command to toggle this behavior, but this bit of GUI Scripting’ll do it:

tell application "iTunes" to activate

tell application "System Events"

tell process "iTunes"

click menu item 3 of menu 2 of menu bar 1

click button 1 of UI element 5 of window 1

click UI element 25 of group 1 of window 1

click UI element 1 of window 1

end tell

end tell

You must have set up GUI Scripting using the instructions found here. And be warned that if the Preferences panel of iTunes changes in the future this script may no longer work.

As usual, works best with a keyboard shortcut.

September 28 2011 - 8:59 am

Column Browser Go Home

I’ve found that iTunes’ Column Browser feature is one of the best ways to navigate the Music library. But I’m often annoyed that I can’t easily restore the browser window to a full view of tracks after digging down to a particular set of tracks. To do so requires a lot of scrolling up and clicking. Correspondent Josh Rafofsky emailed me complaining of the same frustration and his solution was pretty good: Command-B to Hide the Column Browser, fn-Left Arrow to got to the top of the browser, and the Command-B again to Show the Column Browser. But even this gets tiresome and he asked if there might be a one-step scripting solution.

You know there is.

I put together this script which uses a combination of standard AppleScripting and GUI Scripting to emulate Josh’s shortcuts solution:

tell application "iTunes" to activate
tell application "System Events"
	key code 11 using command down
	tell application "iTunes" to reveal track 1 of (get view of front window)
	key code 0 using {command down, shift down}
	key code 11 using command down
end tell

Save this as whatever you like—I call it “Column Browser Go Home”—save it to the ~/Library/iTunes/Scripts folder, and assign it a shortcut (make sure you have enabled GUI Scripting, too, as outlined in this article on using key codes). When launched after you’ve Column Browsed to a discrete set of tracks it will restore the entire list of tracks and jump to the top of the selected playlist. In my case this is usually the Music library playlist, but it will work with any playlist that’s being viewed with the Column Browser.

[UPDATE] And after all that, @tonyhazeldine tweets: “The same can be done by clicking on the column titles at the top of the column browser.” Yes, but each column has to be clicked.

Site contents © 2001 - 2017 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Legal.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
One who says "it cannot be done" should not interrupt one doing it.