Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript Question - Adding Tracks to Playlists

Seriously, I'm embarrassed to even be asking this.

I've got an Applescript that is supposed to build a playlist of a bunch of whole albums. Everything works fine, except for actually adding the tracks to the playlist. Here's the relevant code:

repeat with theAlbum in randAlbums
    set these_tracks to (tracks of library playlist 1 whose album is theAlbum)
    repeat with the_track in these_tracks
        add the_track to playlist thePlaylist  (* doesn't work *)
    end repeat
end repeat

The error I get is "iTunes got an error: A descriptor type mismatch occurred."

randAlbums is a list of unique album names, and thePlaylist is a playlist that is created earlier in the script.

I've been banging my head against this for what feels like a week and I haven't been able to figure it out. Thanks in advance for any assistance you can offer :)

like image 813
brettkelly Avatar asked Feb 10 '09 09:02

brettkelly


2 Answers

Duplicate is the command you want. Try this:

repeat with theAlbum in randAlbums
    duplicate (tracks of library playlist 1 whose album is theAlbum) to thePlaylist
end repeat

Within the iTunes interface add is used to add a new track to the iTunes library using a file system path, while duplicate is used to place a reference to an existing track in a playlist.

When the add command is used iTunes will eventually figure out that the track is already part of the library and do what you want, but not before it reads the file's metadata, schedules it for album art retrieval, etc. All of this amounts to a pretty slow operation so if you're using it within a loop for a large number of tracks iTunes will slow to a crawl.

Duplicate performs a native database lookup and adds the results to the playlist all at once so it is very fast.

like image 199
Matt Stevens Avatar answered Nov 03 '22 02:11

Matt Stevens


Try:

copy the_track to end of playlist thePlaylist

instead.

like image 40
iayork Avatar answered Nov 03 '22 02:11

iayork