Alfred, you're a Genius

Alfred is one of my personal favorite and most-used apps on my Mac, and with the PowerPack, it’s become my primary portal into iTunes as well. (Those of you with Macs but without Alfred are missing out.) However, there remains one big deficiency in Alfred as it ships: there’s no ability to generate a Genius playlist from the currently playing song.

After some digging, I discovered an AppleScript from user TingTingBen on GetSatisfaction that accomplishes this:

tell application "iTunes"
    set TheITunesWindowName to name of (first window whose its class is browser window or its class is playlist window)
    activate
    if selection is {} then reveal current track
end tell
tell application "System Events" to tell window TheITunesWindowName of process "iTunes"
    click (first button whose value of attribute "AXDescription" is "Genius")
end tell

(Note that this script completely discards the alfred_script(q) wrapper that Alfred traditionally puts around a new AppleScript.)

By adding this to Alfred’s extensions and binding it to the keyword genius, I can play a song with the iTunes Mini Player, then reopen Alfred and immediately Genius the song. I can also let a playlist or album start playing through, then turn it into a Genius playlist when I hit a favorite song.