Tuesday, October 21, 2008

Create Genius Playlists in iTunes on the go using ooTunes ... thank you Applescript

I've been taking a break from the pain of getting a firewall-avoiding/live-video-playing ooTunes iPhone app finished to spend some time on the ooTunes Media Server.  I've fixed a number of longstanding bugs and added a few new features.

One major new feature is that (as promised) I've finally added Genius Playlist creation to ooTunes!  What this means is that when you're listening to a song on random in ooTunes, and you decide that you want to make a Genius playlist from it, it's as easy as dragging that track to the "Genius" playlist.  Well, I say it's "that easy", but that's not exactly correct (and it wasn't trivial to get working):

Caveat Emptor -- or six
  1. This is currently only working in a regular browser (not on the iPhone yet).
  2. This only works on Mac's (sorry PC, you've had a rough couple of years, I know).
  3. This only works on Mac's when you turn on "User interface scripting" also known as "Support for Assistive Devices"... 
  4. It requires iTunes to be brought to the front and may even have message boxes popup in iTunes on your ooTunes server computer.
  5. It may not work on non-english versions of iTunes. 
  6. You have to already have the track in your iTunes library, and "Genius" has to be updated since that track was added.

So why all the caveats?
1. Because I haven't come up with a clean way to do drag/drop on the iPhone like I can in a regular browser
2. I have to use AppleScript to create the Genius Playlist in iTunes, there's no equivalent commands for the windows COM interface to iTunes.
3 and 4. There is no menu command or shortcut key or AppleScript Library entry in iTunes to allow access to the "Start Genius" feature of iTunes... it's almost like... oh, I don't know... Apple DOESN'T WANT YOU TO BE ABLE TO DO THIS?!?... so it's fragile (could stop working with the next version of iTunes) and annoying (the Applescript has to simulate clicking a mouse on the button, which means iTunes has to be at the front, and a message box may appear if the track can't be used to create a Genius playlist).  
5. The button can't be found by anything but name so it's not going to work for other languages unless "Start Genius" is never translated to your language in iTunes.
6. Unfortunately, you can't create a genius playlist from any old song... I was hoping to make it so you could find music you DO own that's similar to music you DON'T own (ie, from pandora, last.fm, seeqpod, radio, etc.)  But you simply can't.  You must have the track in your iTunes library already, and you have to do the "Update Genius" after adding any new tracks, if you want them to work with the Genius Playlists.

So, there you go, I've made good on my promise, now try it out (although you can't in the demo cause I don't have enough legal tracks there for iTunes to make any suggestions... sorry!)

So there you have it, it'll only get better (hopefully).

Oh, and so no-one has to reinvent the wheel, here's a download of the applescript ooTunes uses (more or less).  Feel free to do what you like with it but, but please leave the comments in there and the copyright notice with the link to this post! 


(*

(C) Steven Woolley 2008, as part of ooTunes Media Server (see ootunes.com)

  

How to create an Genius Playlist in iTunes using applescript.  More info and issues discussed here:

http://ootunes.blogspot.com/2008/10/create-genius-playlists-in-itunes-on-go.html 

Feel free to use this code however you'd like, but please keep this comment intact so I hear feedback, etc., since I'd like to hear about updates, improvements, suggestions, etc. 

*)


set pid to "4B62C662B53FFB94" -- set this to your track's persistent id... or use some other method to get your "seed" track


set app_name to "iTunes"

tell application "iTunes"

try

with timeout of 10 seconds -- don't want to hang forever if, for instance, iTunes has a dialog box already open

set cur_track to (first track of playlist named "Music" whose persistent ID is pid)

reveal cur_track

end timeout

on error theError

return "Can't find track!"

end try

end tell


tell application "System Events"

tell process app_name -- this needs Assistive Devices support enabled see: System Preferences -> Universal Access -> "Enable access for assistive devices"

try

set b to first button of window app_name whose help is "Start Genius." -- that's about the only way to get the "Start Genius" button, but it will break on non-english versions (I'm guessing)

tell application "iTunes"

activate

end tell

click b

with timeout of 1 second -- this is a hack to "test" if the "can't create genius playlist..." dialog opened.  If it does, we'll timeout (hopefully) and then close the dialog so it doesn't freeze future applescript interaction

tell application "iTunes"

activate

end tell

end timeout

return ""

on error theError

if theError is "iTunes got an error: AppleEvent timed out." then

key code 36 -- close the dialog 

return "Can't create genius list from that track!"

end if

return theError

end try

end tell

return "Error creating genius playlist!"

end tell

No comments: