' ############################################################################### ' # ' # itunes_remove_duplicates.vbs ' # ' # This script will remove duplicates from the user-specified playlist(s). ' # Duplicates are determined by exact file/path name. ' # ' # Copyright (C) 2007 Robert Jacobson ' # written by: Robert Jacobson (http://mysite.verizon.net/teridon/itunesscripts) ' # Last Updated: 30 July 2007 ' # Version 2.1 ' # ' # This script is GPL v2. see http://www.gnu.org/copyleft/gpl.html ' # ' # Change record: ' # 1.1 Updated to deal with multiple copies of the same data with similar file ' # names (e.g. foofile.mp3, foofile 1.mp3, foofile 2.mp3) ' # 2 Detect whether script is running via cscript or wscript. Create and ' # select a temporary playlist while the script is running ' # 2.1 Update loop logic to start at end of list and step backwards through items. ' # Hopefully this will avoid a possible race condition ' # ' ############################################################################### Option Explicit Dim iTunesApp ' iTunes.Application object used to access the iTunes application. Dim tracks ' The tracks collection object of the Library object. Dim FileList ' The list of files that have been copied. Dim i ' A counter variable. Dim Msg ' A string for a message. Dim f ' A file object. Dim sources Dim source Dim playlists Dim playlist Dim playlistName Dim j Dim m Dim songName Dim artist Dim result Dim listarray Dim num Dim k Dim track Dim numtracks Dim Hash Dim numRemoved Dim old_playlist Dim tmp_playlist ' Temporary playlist to select Dim rand Set Hash = CreateObject("Scripting.Dictionary") Set iTunesApp = CreateObject("iTunes.Application.1") Set sources = iTunesApp.Sources DIM use_windows Dim Playlist_list 'detect CSCRIPT context & use InputLine IF Instr(lcase(wscript.FullName), "cscript")>0 THEN use_windows = 0 ELSE 'detect WSCRIPT context & use native InputBox use_windows = 1 END IF For i = 1 to sources.Count Set source = sources.Item(i) IF source.Kind = 1 Then Set playlists = source.Playlists if use_windows = 0 Then For j = 1 to playlists.Count Set playlist = playlists.Item(j) playlistName = playlist.Name Wscript.Echo j & ": " & playlistName Next Else For j = 1 to playlists.Count Set playlist = playlists.Item(j) playlistName = playlist.Name Playlist_list = Playlist_list & j & ": " & playlistName & Chr(10) Next End if If use_windows = 0 Then Wscript.Echo "" Wscript.StdOut.Write "Enter comma-separated lists to process: " result = Wscript.StdIn.ReadLine Else result = InputBox(Playlist_list & Chr(10) & "Enter comma-separated lists to process: ", "Which Playlist(s)") End if ' generate a new seed for the Rnd() function Randomize ' Generate a random number between 0 and 1000 rand = Int(Rnd() * 1000) ' Store currently selected Playlist Set old_playlist = iTunesApp.BrowserWindow.SelectedPlaylist ' Create a temporary playlist and select it Set tmp_playlist = iTunesApp.CreatePlaylist("Temporary Playlist " & rand) iTunesApp.BrowserWindow.SelectedPlaylist = tmp_playlist ' If use_windows = 0 Then Wscript.Echo "***" & Chr(10) & "*** Leave the Temporary Playlist selected -- the script will run faster" & Chr(10) & "***" ' Else ' MsgBox "Leave the Temporary Playlist selected -- the script will run faster" ' End If listarray = split(result, ",") For k = 0 to UBound(listarray) num = listarray(k) Set playlist = playlists.Item(num) playlistName = playlist.Name If use_windows = 0 Then Wscript.Echo "" Wscript.Echo chr(9) & "Processing playlist " & num & ": " & playlistName End If Set tracks = playlist.Tracks numtracks = tracks.Count If use_windows = 0 Then Wscript.Echo chr(9) & "tracks: " & numtracks End If numRemoved = 0 For m = numtracks to 1 step -1 'If m > tracks.Count Then Exit For Set track = tracks.Item(m) 'Wscript.Echo "num: " & numtracks & " Count: " & tracks.Count & " m: " & m If track.Kind = 1 Then songName = track.Name artist = track.Artist FileList = track.Location if Hash.Exists(FileList) Then If use_windows = 0 Then Wscript.Echo chr(9) & "Removing Duplicate: " & m & ": " & songName & " by " & artist numRemoved = numRemoved + 1 End If track.Delete ' after delete, all tracks move up in the list 'm = m - 1 'numtracks = tracks.Count else Hash.Add FileList , 1 end if End If Next ' If use_windows = 0 Then Wscript.Echo "Finished processing playlist " & playlistName & ", deleted " & numRemoved & " tracks" ' Else ' MsgBox("Finished processing playlist " & playlistName & ", deleted " & numRemoved & " tracks") ' End If Next 'End If End If Next ' Delete the Temporary Playlist and return selection to user-selected playlist tmp_playlist.Delete iTunesApp.BrowserWindow.SelectedPlaylist = old_playlist