Question

I wrote a script that would identify duplicates in iTunes, find their instances in playlists, and other things which I don't think are relevant. In order to check if a track is a duplicate, I have the following code:

        set duplicateSongs to ¬
        get (every track of playlist 1 where ¬
            (name is equal to (name of currentTrackToCheck as text) and ¬
                database ID is not equal to (database ID of currentTrackToCheck as integer) and ¬
                time is equal to (time of currentTrackToCheck as text)))

I wrote and tested this against a test library with 1,000 songs and it was speedy. When I tested it against my iTunes library which is about 30,000 songs, then all hell broke loose.

That snippet of code takes 2 minutes to finish processing! Is there any way to make this faster code-wise? I've been reading a lot on AppleScript and I believe using where/whose is the fastest way to filter down results from a query.

Thanks! :-)

Was it helpful?

Solution 2

Just changed to using the search function and then doing a search within that and it takes less than a second - yay! :-)

on SearchForDuplicates(currentTrackToCheck, iTunesLibrary)
tell application "iTunes"
    set duplicateSongs to {}
    set preSearch to search iTunesLibrary for (name of currentTrackToCheck as text) only songs
    repeat with aTrack in preSearch
        tell aTrack
            if (name is equal to (name of currentTrackToCheck as text) and ¬
                database ID is not equal to (database ID of currentTrackToCheck as integer) and ¬
                time is equal to (time of currentTrackToCheck as text)) then
                set end of duplicateSongs to aTrack
            end if
        end tell
    end repeat
    return duplicateSongs
end tell

end SearchForDuplicates

OTHER TIPS

It won't affect speed, but I would write it like this...

tell application "iTunes"
    set {trackName, trackDatabaseID, trackTime} to {name, database ID, time} of current track
    set duplicateSongs to (get every track of playlist 1 whose name = trackName and time = trackTime and database ID ≠ trackDatabaseID)
end tell
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top