Launch Genius via AppleScript

applescriptitunes

I would like to launch a Genius playlist for the currently playing song in iTunes via an AppleScript.

A prerequisite would be for the script to work when iTunes is in full screen.

And ideally, it wouldn't :

  • bring iTunes to front
  • leave an error message when genius isn't available for that song
  • depend on the system language

I found this script, but it doesn't work for me when iTunes is in full screen. Still, it's a great start.

It would maybe be possible to use the protocol the iOS "Remote" app uses, but that seems to be overkill.

Any help would be appreciated.

(the goal is to launch genius from softwares like Keyboard Maestro or Alfred)

Best Answer

Try this :

tell application "iTunes" to try
    activate
    with timeout of 10 seconds
        reveal current track -- error if no current track
        set tName to name of (first window whose its class is browser window or its class is playlist window)
    end timeout
on error
    return "Can't find track!"
end try

tell application "System Events"
    tell application process "iTunes"
        try
            set b to (first button of window tName whose value of attribute "AXDescription" is "Genius")
            if not (enabled of b) then return "Genius button disabled" -- (film, video clip,...)
            perform action "AXPress" of b
        on error -- no Genius button (radio, podcast, .....) playlist
            return "Can't create genius list from that track!"
        end try
        delay 2
        tell front window to if value of attribute "AXSubRole" is "AXDialog" then -- dialog opened.
            perform action "AXPress" of button "OK" -- close the dialog
            return "Can't create genius list from that track!"
        end if
    end tell
end tell
return "Done"