Renaming tracks in iTunes, sorting

I am trying change the names of tracks that are selected and having problems when the tracks get renamed and the sort order changes.

in pseudo script, here’s roughly what I am doing:

set newTrackNameList to {“cool song 1”,“not so cool song 2”…}

tell itunes
repeat with thisTrack in selection
set name of thisTrack to item x of newTrackNameList
end
end

The problem is as soon as the song name changes it’s sort order, the script starts renaming the wrong tracks.

I think what I am looking for is something like this, but I don’t understand the iTunes data types well enough.

again in pseudoscript:

tell itunes
set renameTracks to database id of every selection (get absolute references to tracks)
repeat with x in renameTracks
set name of track with database id to item x of newTrackNameList
end

Is this clear? Basically I want to get an absolute list of tracks that doesn’t change even if their name changes, and rename those tracks based on the selection order BEFORE I rename them.

I’ve looked through many examples and can’t find one that renames tracks this way. There is an example Rename Tracks to Files, but since the files I am using are imported, the default name is already the filename, so that doesn’t help.

although i don’t know, i would suggest setting a variable such as “trackList” to the selected tracks BEFORE you tell it to sort the selected tracks, and then saying “repeat with all items in trackList”, so you won’t rename them in the order they appear in as it renames them, you’ll rename them in their previous order, which, unless i misunderstand you, is what you’re trying to do.
good luck!
:slight_smile:

Although I found a workaround for this issue (using a playlist – See dougs scripts “Rename as Filename” for tips), I don’t see a specific way to refer to tracks as absolutes. That is, I can build a list from the selection, but to iterate the list there is no object to refer to. I can’t seem to refer to tracks by database id, track name, number, or anything. Track X of playlist x is the only thing that seems to work, but there is a Play Order issue that has to be solved manually.