Auto Remove iTune Duplicate Applescript - Help

Hi,

I found this script that works great with removing iTunes duplicates automatically. Something I've been wanting for a long time.


This is an import script which helps me sync my music library from my home office with my nightclub iTunes library. The one problem that i have with this script is that it doesn't keep that last saved duplicate track. I want to keep the latest tracks because sometime I replace a track with a better quality audio or music video file. I'm hoping that someone with great apple scripting abilities can review the script and help me adjust it. I tried to contact Randolf who created the script, but he hasn't replied. Below is his wonderful script. I'm sure many people will find this useful. I hope someone can help me.


-Ron


_______________________________________________________________________________'


Remove ITunes Duplicates Script


(*

This little script finds and deletes duplicates in your iTunes library. Two tracks are considered duplicates if they share the same track name, artist and album title. Only if those 3 fields are the same, all of the tracks except one can be deleted. Additionally the play counts and the grouping entries are "merged" together.

You can also use the main handler to delete duplicates in any other playlist than the main library.


You can turn on or off logging of deleted tracks. The logging goes to the "logFile.txt" in your home folder.


Any comments to

randolf.altmeyer@gmail.com

*)



property doLogging : false

property textlog : missing value


on run {}

tell application "iTunes" to set plList to library playlist 1

main(plList)

end run


on main(plList)

-- set up logger

set textlog to makeFileLog(((path to home folder) as string) & "logFile.txt")

if doLogging then textlog's logImportant("Starting deleting duplicates...")

findDuplicates(plList)

if doLogging then textlog's logImportant("Finished deleting duplicates...")

end main


on findDuplicates(plList)

tell application "iTunes"

-- find all artists

set artistList to artist of every track of plList whose artist is not missing value

set actArtList to {}

repeat with a in artistList

if not (actArtList contains (a as string)) then copy (a as string) to end of actArtList

end repeat

-- find all albums for a given artist

repeat with a in actArtList

set albList to (album of every track of plList whose artist is a and album is not missing value)

set actAlbList to {}

repeat with al in albList

if not (actAlbList contains (al as string)) then copy (al as string) to end of actAlbList

end repeat

-- find all tracks with artist a and album al

repeat with al in actAlbList

set trList to (name of every track of plList whose artist is a and album is al and name is not missing value)

set actTrList to {}

repeat with tr in trList

if not (actTrList contains (tr as string)) then copy (tr as string) to end of actTrList

end repeat

-- handle those tracks

repeat with tr in actTrList

set tracksWithThisComb to (every track of plList whose artist is a and album is al and name is tr)

if length of tracksWithThisComb > 1 then

my mergeTracks(tracksWithThisComb)

repeat with i from 2 to length of tracksWithThisComb

set t to item i of tracksWithThisComb

if doLogging then

set str to (tr & " - " & al & " - " & a)

textlog's logMessage("Deleting " & str)

end if

delete t

end repeat

end if

end repeat

end repeat

end repeat

end tell

end findDuplicates



on mergeTracks(possDupl)

tell application "iTunes"

set mainTrack to item 1 of possDupl

set playCount to played count of mainTrack

set grOfTrack to grouping of mainTrack

set gr to ""

set compOfTrack to compilation of mainTrack

set comp to ""

-- collect all information in mainTrack

repeat with i from 2 to length of possDupl

set tr to item i of possDupl

set trPlCount to played count of tr

if trPlCount > playCount then set playCount to trPlCount

if (grOfTrack is equal to "") then

set trGroup to grouping of tr

if not (trGroup is equal to "") then set gr to trGroup

end if

if (compOfTrack is equal to "") then

set trComp to compilation of tr

if not (trComp is equal to "") then set comp to trComp

end if

end repeat

set played count of mainTrack to playCount

if (grOfTrack is equal to "") then set grouping of mainTrack to gr

if (compOfTrack is equal to "") then set compilation of mainTrack to comp

end tell

end mergeTracks



on makeFileLog(file_path)

script FileLog

property class : "file log"

property _linefeed : character id 10

-- Writes log messages to a UTF8-encoded text file

on logMessage(the_text)

set f to open for access file_path with write permission

try

write (the_text & my _linefeed) to f starting at eof as «class utf8»

on error error_message number error_number

close access f

error error_message number error_number

end try

close access f

end logMessage

on logImportant(the_text)

logMessage("****** " & the_text & " ******")

end logImportant

end script

end makeFileLog

iMac, Mac OS X (10.7.3)

Posted on Feb 29, 2016 7:09 PM

Reply

There are no replies.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Auto Remove iTune Duplicate Applescript - Help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.