Search code examples
applescriptitunes

How to set the tracks of a playlist in iTunes using AppleScript?


I'm trying to set the tracks of a user playlist using AppleScript. I don't want the tracks to be added, I want them to replace the current tracks. Before I do that, I need to look up the tracks by their persistent IDs, which is working fine, but the statement that sets the tracks isn't working. Code snippet below:

            set fixed indexing to true
        set myPlaylist to the first item of (every user playlist whose persistent ID is "5C768EFF306E3366")

        set tracksPIDs to {"66EB935073027EDD", "B6807694FEDD76B4"}
        set resolvedTracks to {}

        --resolve the tracks
        repeat with trackPID in tracksPIDs
            set myTrack to the first item of (every track whose persistent ID is equal to trackPID)
            set end of resolvedTracks to myTrack
        end repeat

        set (the tracks of my myPlaylist) to duplicate of (resolvedTracks)

Also, how can I remove all tracks from a playlist (i.e set it to empty list)?


Solution

  • To do what you want, you will need to do two steps, first removing the existing tracks from the playlist, then adding in the tracks that you want to be in the playlist. The first step would look like:

    delete every track of myPlaylist
    

    Then the addition would look like:

    duplicate resolvedTracks to myPlaylist