Remove duplicates from iTunes playlist

UPDATE: Script could be found on BitBucket now –

If one adds music to iTunes playlist by dragging them files in (without moving to iTunes library), one might end up with a fair number of duplicates. And one might get frustrated about it.

ALso, one might even go and solve it, and then share for other suffering fellas – like this:

NOTE: playlist should be sorted by name. And it makes sense to export a playlist for backup reasons – for I give no warranties here.

Code is included to the image – but let it rest below as well. FTR to launch/edit it, run AppleScript Editor (use Spotlight) – it’s simple but nice tool to write AppleScript apps.


tell application "iTunes"
display dialog "Gimme playlist name" default answer ""
set plName to the text returned of the result
set curPl to user playlist plName -- current playlist
set dupes to {}
set prevTrack to {"", 0}
set trackNo to 1
repeat with aTrack in every track of curPl
set curTrack to {get name of aTrack, get time of aTrack}
if curTrack = prevTrack then
set the end of dupes to trackNo
end if
set trackNo to trackNo + 1
set prevTrack to curTrack
end repeat
set dupePos to 0
repeat with dupe in dupes
set rmTrack to dupe - dupePos
-- log rmTrack
delete track rmTrack of curPl
set dupePos to dupePos + 1
end repeat
set dialogLine to "Deleted " & (count dupes) & " dupes in " & plName & " playlist"
display dialog dialogLine buttons {"OK"}
end tell

Leave a Reply

Your email address will not be published. Required fields are marked *