How do I create a Genius playlist in iTunes 10 from a script?

2

I want to create a script that starts a new Genius playlist based on the currently-playing track to use in LaunchBar. Unfortunately iTunes doesn't expose any Genius-related functionality to AppleScript, which makes things difficult.

I've searched the web and found several solutions that involve manually finding the button in the iTunes UI and invoking a click event (example), but they don't work in iTunes 10 and my AppleScript-fu is nowhere near powerful enough to debug them.

Is there any way to start a Genius playlist from outside the iTunes application? Other methods like Automator workflows or shell scripts would be perfectly acceptable as well.

Brant Bobby

Posted 2010-09-08T00:32:03.173

Reputation: 2 186

Answers

3

After some experimentation, I think I found your solution.

tell application "System Events"
-- click the genius button on the currently playing track at top
    click button 14 of window "iTunes" of application process "iTunes"
end tell

With more experimentation, I discovered the following:

  • Buttons 1-3 are the "stoplights" top left
  • Buttons 4-7 are the buttons bottom left, add playlist, shuffle, repeat, close artwork.
  • Button 8 seems to be the genius button on the bottom right of the screen, (close!) and will work for the currently selected track. I presume you want to genius the currently playing track…
  • Button 9 is the right-side genius pane.
  • Button 10-12 are rewind, play/pause, forward.
  • Button 13 is the equalizer.
  • Button 14 is the genius button! (The one you want)
  • Button 15 is select (and go to) track.
  • Buttons 16-19 are the various "view" buttons
  • Button 20 is zero volume.
  • Button 21 is max volume.
  • Button 22 is error!

That's it. Thanks for bringing up this question, this insight will help with various scripting ideas. :)

Edit:

OK, a little more digging into dictionaries gave me this idea:

tell application "System Events"
    set button_count to count every button of window "iTunes" of application process "iTunes"
    repeat with i from 1 to button_count
        set button_description to accessibility description of button i of window "iTunes" of application process "iTunes"
        if button_description is "genius" then
            set x to i
        end if
    end repeat
    if enabled of button x of window "iTunes" of application process "iTunes" then
        click button x of window "iTunes" of application process "iTunes"
    end if
end tell

What it does is cycle through all the buttons of the iTunes window and check the accessibility description of the button. The genius buttons with have, unsurprisingly, a description of "genius."

In some views, there are two genius buttons, one for the currently playing song and one for the selected song. The script will always take the latest of the two buttons, which happens to be the currently playing song. So this AppleScript will check if the genius button is enabled, and then click it.

If there is no currently playing song, but a selected song, then there will be one genius button and it will be enabled and will be clicked. If no songs are playing or selected, the genius button will not be clicked.

I think this is the functionality you want! Let me know if it works for you.

ghoppe

Posted 2010-09-08T00:32:03.173

Reputation: 6 124

Nice find. However, it seems that the button assignments change depending on what sort of playlist is currently active. The Genius button is #16 for me in playlists and the Genius Mixes view, #18 in iTunes DJ or Genius view, and #17 in a saved genius playlist. (Also various other values in Store view, books, podcasts, etc... I stopped digging around for special cases after the first few.) I don't suppose there's any way to take this into account, at least for the more common cases I listed? – Brant Bobby – 2010-09-09T00:19:50.753

@Brant hmm… actually there is. I'll update my answer with more info. – ghoppe – 2010-09-09T23:07:32.340

I get an error message when I try to activate the script in iTunes: "Can't get window "iTunes" of <<class pcap>> "iTunes" of application "System Events".

When I run the script from the script editor it works fine.

What's up? – None – 2011-01-23T02:45:58.430