Scripting iTunes Preferences

Does anyone else find it annoying how difficult it is in iTunes to switch between Shuffle by Song and Shuffle by Album? It requires a trip to the Preferences window. Couldn’t Apple have made the Shuffle button a 3-way toggle, like the Repeat button? Ah well.

So I had the brilliant idea of creating an AppleScript to do the job. Not so easy, as it turns out. First of all, Preferences aren’t scriptable in iTunes (as of version 4.5). Second of all, even using Apple’s new GUI Scripting (System Events), I can’t seem to get this to work.

The first problem is that the iTunes Preferences window is divided into 7 screens, and the option I want to get at is in the Advanced screen. But the window doesn’t use standard tabs to switch between screens. Using the UIElementInspector, I see that the row of icons across the top of the window is one UI element. I suppose I could do a “click at”, but I haven’t been able to get that to work.

But assuming I could, or assuming that the Preferences window always defaults to the Advanced screen, I still can’t get the radio button in quetion (“Album”) to be selected. Here’s my script so far:

tell application "iTunes" to activate

tell application "System Events"
	tell process "iTunes"
		click menu item "Preferences…" of menu "iTunes" of menu bar 1
		delay 1
		tell window "Advanced"
			click radio button "Album"
		end tell
	end tell
end tell

Everything works up to the “click radio button” command. At that point, I can actually witness the radio button flash a bit (as if you clicked but didn’t release the mouse in the radio button), but it does not get selected.

Any help is greatly appreciated.



The programmer has a choice of different window types. I forget the different choices, but anyway. I think, iTunes full focus is on the preference window after it is opened. So if you try to send it something, then it won’t respond until the window is closed. There may be a workaround to this.

Edit: sorry my mistake. I had the preferences window already open so disregard.



I couldn’t click the radio button either nor find a workaround. Doing some sideways thinking, maybe you can make a smart playlist that selects albums or songs randomly. I think this is a feature but havn’t tested it yet. Another way maybe to use an AppleScript to create a playlist based on random songs or albums. Come to think of it, you could make an AppleScript that creates a smart playlist maybe, that plays… I’ll try testing out some options tonight.


Ok, I just created 2 smart playlists that selects 6 hours worth of songs by album and song name respectively. Next step is how can you reset the smart playlists to select another batch of random songs or albums.