Monday, June 6, 2005

AppleScript frustrations

I spent a lot of time this past weekend trying to improve the AppleScript that I mentioned before.  I want to uses this script to automatically move all tracks that were added to iTunes, from Shrook, into a single playlist.

Since Shrook downloads all of the podcast tracks to ~/Library/Application Support/Shrook2/Attachments, all that would been needed to is to find all of the tracks in the iTunes library that are in that directory, and then make sure they were in the playlist.

The AppleScript that I wrote before does this job, but it takes a long time if the iTunes Library has a lot of tracks, as it had to iterate over each of them.  So I wanted too change the script to iterate over the files that Shrook had downloaded, and then check each one to see if it has been added.

This script sample should work:

tell application "iTunes"

if (exists (get some file track of library playlist 1 whose location is theFile)) then

set this_track to (get some file track of library playlist 1 whose location is (theFile as alias))

set dbid to this_track's database ID

if not (exists (some track of playlist podcastPlaylist whose database ID is dbid)) then

duplicate this_track to playlist podcastPlaylist

end if

end if

end tell

Unfortunately, there seems to be a bug implementation detail in iTunes AppleScript implementation.  In the script above, the line get some file track of library playlist 1 whose location is theFile is supposed to return only objects of type file track.  The file track class inherits from class track but also includes the location attribute.  And exception is thrown on this line when running this script.

It looks like the implementation is doing something like getting each item as class track, and then attempting to cast it to class file track.  This will fail in a lot of cases.

I will have to figure out how to work around this.

Technorati Tags: