Trying to find similar song names in itunes before putting in library

Newbie here! After adding a folder item that is an mp3 file, I want to search for similar song titles in the iTunes main library and if there is a match, then delete the new file. Nobody sems to use the “Search” command and I can’t figure it out myself. Thanks


on adding folder items to this_folder after receiving added_items
	repeat with i from 1 to number of items in added_items
		tell application "Finder"
			try
				set this_file to (item i of added_items)
				if kind of this_file is "mp3 Audio File" then

					set song_name to name of this_file
					copy (items 1 through -5 in song_name) as string to truncated_name --
						
                                   --if any song title in my main library is the same as truncated_name  then delete this_file			
				else
					delete this_file
				end if
			end try
		end tell
	end repeat
end adding folder items to

Model: Powerbook G4
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Welcome! :slight_smile: Try something like this:

on adding folder items to thisFolder after receiving addedItems
	repeat with thisItem in addedItems
		try
			set {name:thisName, name extension:thisExtension} to (info for thisItem)
			
			if thisExtension is "mp3" then
				tell application "iTunes"
					launch
					get (tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))
				end tell
				
				if result is {} then
					tell application "iTunes"
						add thisItem to library playlist 1
						add thisItem to playlist "Tonight"
					end tell
				end if
			else
				tell application "Finder" to delete thisItem
			end if
		end try
	end repeat
end adding folder items to

WOW you saved me a days worth of work this forum is awsome! One other thing, if there is a match, how can I copy the result (the track that was found as a match already in iTunes) to a playlist called “tonight”?
Thanks for your help Bruce!! :smiley:

You’re welcome. :slight_smile: I edited the script above to include your playlist.

Bruce that actually didn’t comile for me… should

if result is {} then
                   tell application "iTunes"
                       add thisItem to library playlist 1
                       add thisItem to playlist "Tonight"
                   end tell
               else

have read like this?

if result is {} then
					tell application "iTunes"
						add thisItem to library playlist 1
						add thisItem to playlist "Tonight"
					end tell
				end if


On another note Im curious why you put multiple tell blocks for iTunes within the if statement? I haven’t acctually tested it, but this looks like it should work no?

if thisExtension is "mp3" then
	tell application "iTunes"
		launch
		get (tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))
		
		if result is {} then
			add thisItem to library playlist 1
			add thisItem to playlist "Tonight"
		end if
	end tell
else
	tell application "Finder" to delete thisItem
end if

Sorry Bruce, about your second version… I guess I wasn’t clear. I am trying to add that song that was found in the search to the playlist “Tonight”. So how do I take a “result” of the search and and take the name of the found song so that it must be added to the “Tonight” playlist. Im using this script as a sort of request Jupebox: If the song is already in the itunes library then just add it to the request folder called “Tonight”.
The first example was great.
Thanks Again!
Here is what Im working on right now;


on adding folder items to thisFolder after receiving addedItems
	repeat with thisItem in addedItems
		try
			set {name:thisName, name extension:thisExtension} to (info for thisItem)
			if thisExtension is "mp3" then
				tell application "iTunes"
					launch
					get (tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))
				end tell
				--set existingTrack to result
				if --existingTrack is {} then
					tell application "iTunes" to add thisItem to library playlist 1
					tell application "iTunes" to add thisItem to playlist "Tonight"
		              else
					--set foundSong to existingTrack
                                   --tell application "iTunes" to add a song with the name foundSong to playlist "Tonight"
					tell application "Finder" to delete thisItem
				end if
			else
				tell application "Finder" to delete thisItem
			end if
		end try
	end repeat
end adding folder items to


I removed one of the Finder lines by mistake.

if result is {} then
	tell application "iTunes"
		add thisItem to library playlist 1
		add thisItem to playlist "Tonight"
	end tell
else
	tell application "Finder" to delete thisItem
end if

Sorry, DJWaldo, I didn’t quite catch that earlier. Try something like this:

on adding folder items to thisFolder after receiving addedItems
	repeat with thisItem in addedItems
		try
			set {name:thisName, name extension:thisExtension} to (info for thisItem)
			
			if thisExtension is "mp3" then
				tell application "iTunes"
					launch
					set existingTracks to (tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))
					
					if existingTracks is {} then
						add thisItem to library playlist 1
						add result to playlist "Tonight"
					else
						add existingTracks to playlist "Tonight"
					end if
				end tell
			else
				tell application "Finder" to delete thisItem
			end if
		end try
	end repeat
end adding folder items to

This did not work for me

on adding folder items to thisFolder after receiving addedItems
   repeat with thisItem in addedItems
       try
           set {name:thisName, name extension:thisExtension} to (info for thisItem)
           
           if thisExtension is "mp3" then
               tell application "iTunes"
                   launch
                   set existingTracks to (tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))
                   
                   if existingTracks is {} then
                       add thisItem to library playlist 1
                       add result to playlist "Tonight"
                   else
                       add existingTracks to playlist "Tonight"
                   end if
               end tell
           else
               tell application "Finder" to delete thisItem
           end if
       end try
   end repeat
end adding folder items to

Isn’t this a list of tracks rather than a single track? And what is it referring to… The track name or track file location? Thanks in advance and for the previous help :slight_smile:

I fixed it! I just added “location of”

on adding folder items to thisFolder after receiving addedItems
repeat with thisItem in addedItems
try
set {name:thisName, name extension:thisExtension} to (info for thisItem)

if thisExtension is "mp3" then
tell application "iTunes"
launch
set existingTracks to (location of tracks of library playlist 1 whose name is (text 1 thru -5 of thisName))

if existingTracks is {} then
add thisItem to library playlist 1
add result to playlist "Tonight"
else
add existingTracks to playlist "Tonight"
end if
end tell
else
tell application "Finder" to delete thisItem
end if
end try
end repeat
end adding folder items to

Thanks Bruce!