Error replacing track in iTunes

I am writing a script that will replace an older/outdated/lost/missing file/etc. track within a playlist with its updated version. I know what needs to be done, but I am having two different problems. First, in the final repeat loop’s line labeled “ERROR”, below, I cannot figure out what the duplicate command is expecting to have passed to it. Second, assuming that the duplicate command can be made to work, I cannot figure out how one would properly add the track tot he correct order within the playlist. So for example, if I have a playlist that has an order:
Song 1
Song 2 (file corrupted)
Song 3
Song 4

How do I not only copy the new version of Song 2 to the playlist, but also have the new Song 2 play in the correct order, not at the end.

Any help very much appreciated!

property myTitle : "Replace Track"
set UserCanceled to false

tell application "iTunes"
	set sel to selection
	if the (count of sel) is not equal to 2 then
		set UserCanceled to true
		set endmsg to "You must select some tracks first."
		set userConfirm to the button returned of (display dialog "This operation will replace the selected song with the new version." & return & return & "Are you sure you want to proceed?" buttons {"Proceed", "NO!"} default button 2 with icon 2 with title "Confirm" giving up after 15)
		if userConfirm = "NO!" then set UserCanceled to true
	end if
	if not UserCanceled then
		-- We already know that there are exactly 2 tracks selected, or we wouldn't be here.  
		set Track1 to the first item of sel
		set Track2 to the second item of sel
		-- Determine which track overwrites the other.  Can be expanded to look for additional information
		if Track1's date added is less than Track2's date added then
			set targetTrack to Track1
			set newTrack to Track2
			set targetTrack to Track2
			set newTrack to Track1
		end if
		-- set newTest to {Track1}
		set containingPlaylists to (every user playlist of targetTrack whose smart is false and special kind is none)
		repeat with thisPlayList in containingPlaylists
                        -- ERROR: "iTunes got an error: A descriptor type mismatch occurred."
			duplicate track newTrack to playlist thisPlayList's name
		end repeat
		-- More stuff to go here
	end if
	display dialog endmsg buttons {"OK"} default button 1 with icon 1 with title myTitle & " - Done" giving up after 10
end tell

Hi. You can relink files without duplicating or rearranging tracks.

tell application "iTunes" to tell playlist "whatever" to set track 2's location to (choose file with prompt "Select a replacement for the missing file.")

Only instead of playlist “whatever” use playlist 1. Replacing the location of a playlist track will add a duplicate to your Library.

Thank you both for the replies, however, this is not what I am looking for. There is no way for me to re-link the files, because I am replacing the old file with a new one. Sometimes, the old file is just “MIA”, but usually, the old file is damaged in some way (old MP3 encoding left some crappy stuff, file is corrupted, etc.). In some cases, i can just delete the file and then download a good copy from iTunes, but iTunes match does not work all the time, and it also frequently fails on classical tracks.

Right now I have two copies of the song already in the library: an old, broken or corrupted track that is part of various playlists, and a new one. The playlists I am referring to have a specific order for the songs (transitions are good, or the playlist tells a story). So what I need is to find the old track’s location in the playlist and then substitute the new track for the old one.

So if instead of the “choose file with prompt”, I instead wanted to use the location of the existing new file, what would that syntax look like? In the code below, targetIndex is the index of the target track in the playlist (so the 10th track in the playlist is what we are replacing, the following does not work:

tell application "iTunes" to tell thisPlayList to set track targetIndex to (location of newTrack as list)

as it throws an “unknown object type” error. The same happens if I pass the location without the “as list”

Thanks in advance.

Tell playlist 1
    set location of dirtyTrack to location of goodTrack
end tell

(or something like this, this is just aht uh mi hed)

Will magically have the proper file play in all playlists.

If you do this in a specific playlist it will only change that playlist and you end up with a duplicate in the library: two goodTrack.

You have to add tracks to playlists one by one and only to the end of the playlist, you can’t add a list, even if a list of one item. The location is an alias, so also not a list.

A location is a reference to the actual music file, and updating the link preserves your playlist(s) by fixing broken associations or circumventing a defective track. This be done by choosing a replacement sound file, by finding a file with the same name on your hard drive, or by swapping location info from a new source file in the library (and then deleting that source). You seem to prefer the last method, which could go something like this:

tell application "iTunes"
	set sourceInfo to selection's {name, artist, location, it}
	tell playlist "whatever" to set (track 1 whose name = sourceInfo's item 1 and artist = sourceInfo's item 2)'s location to sourceInfo's item 3 --adjust to your playlist
	delete sourceInfo's item 4's item 1 --source is no longer needed
end tell

Changing the location of a track”regardless of if you’re just targeting a playlist or the main library”updates all instances of the music file. It might appear to create a duplicate in the library, if you link a different track to a location for a file that’s already there.

So far, you guys have been great. However, both suggestions seem to fail due to a requirement for a posix path, rather than whatever “location” returns (error -1728, Error getting posix path).

However, I think that I was unclear about what I am trying to accomplish. I don’t think that it is sufficient for me to simply change the location of the song within the playlist, because at the same time that I am replacing the song, I am also trying to merge the metadata. Unless I am wrong, changing the location will not affect the metadata associated with the original file, right?

For example, I have a song in a mix that someone made for me that has no artwork, and the album is listed as “Billboard Top 100”. That song has a play count of 7. At some point, something lost or corrupted that file, and it is listed with an ! in iTunes. I own a pristine, fully meta-data updated version of that song, with lyrics added, correct album artwork, and a working file. What I am trying to do is to replace the broken track with the new one while keeping the new track’s metadata with the exception of the play count, which I want to keep from the old track (actually, it will be the sum of the two tracks, but that is quibbling).

Am I correct that writing code to copy all the properties from the new song to the old song (preserving play count) and then updating location will work? How do I deal with the Posix issue?

Thanks in advance.

!? Unless something changed in iTunes location is an alias. (I am obsolete)

Wrong. Metadata will change to whatever is in the new file.

Something like this, but as I said, do it in the main library, all playlists will be updated in their proper playing order:

display dialog "select the good track"

tell application "iTunes"
	set t to item 1 of selection
	set good to location of t
	set goodCount to played count of t
	delete t
end tell

(* If the good file is not yet in iTunes:
set goodCount to 0

set good to choose file

display dialog "select the bad track"

tell application "iTunes"
	set t to item 1 of selection
	set bothCount to (played count of t) + goodCount
	set location of t to good
	set played count of t to bothCount
end tell

Been away from this code for a while. Thank you all!