Search in ITunes needs prompt to select 1 track from results

I have put together a script that will process the name of songs from a text file with comma seperated values, and add them to a playlist. It will also write the values it could not locate to a text file. This works well, but I would like to implement additional functionality. When the search produces a value that has more than one result I would like it to prompt me to see which one of the results I would like to add to the playlist. Any help would be greatly appreciated, and I have included a copy of the script below:


set myPrefsFile to (choose file with prompt “Select a file to read:”)
set notfound to ((path to desktop folder) as string) & “notfound.txt”
open for access notfound with write permission
open for access myPrefsFile
set AppleScript’s text item delimiters to {“,”}
set prefsContents to read myPrefsFile using delimiter {“,”}
close access myPrefsFile
set x to 0
set theplaylist to “Temp”
tell application “iTunes”
make new user playlist with properties {name:“Temp-1”}
end tell
repeat number of items in prefsContents times
set x to x + 1
set track_name to item x of prefsContents
tell application “iTunes”
get view of front window
tell library playlist 1
set search_results to (search for track_name)
copy search_results to theplaylist
end tell
repeat with a_track in search_results
duplicate a_track to playlist “Temp-1”
end repeat
end tell
if search_results = {} then write track_name to file notfound
end repeat
close access file notfound



Model: Macbook Pro
AppleScript: 2.1.2
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)

I have minor experience in iTunes so I think there is nicer code but here is how you can search for a file. If it is not found selectedTrackID will be null otherwise selectedTrackID will contain a track id reference. When the search result has foudn more than 1 item it let the user choose the item. There is only one minor bug in this script, when there are two tracks with the same name it will always choose the first one found if even you select the second one from the list.

tell application "iTunes"
	tell library playlist "Bibliotheek"
		set theTracks to search for "deadmau5"
		if theTracks's length > 1 then
			set trackNames to {}
			repeat with thisTrack in theTracks
				try
					set end of trackNames to (name of thisTrack)
				on error
					set end of trackNames to "<track name missing>"
				end try
			end repeat
			set myResult to (choose from list trackNames)
			if myResult = false then
				set selectedTrackID to null
			else if item 1 of myResult = "<track name missing>" then
				set selectedTrackID to null
			else
				set selectedTrack to item 1 of myResult
				set selectedTrackID to null
				repeat with x from 1 to count trackNames
					if selectedTrack is item x of trackNames then
						set selectedTrackID to item x of theTracks
					end if
				end repeat
			end if
		else if theTracks's length = 1 then
			set selectedTrackID to item 1 of theTracks
		else --it means nothing has been found
			set selectedTrackID to null
		end if
		
		return selectedTrackID
	end tell
end tell

I implemented a few pieces from your code DJ. I came up with a solution that works, however, I would like to know how to view the artist and the name (side by side) in the pop-up that requests which track I want to add. Do have know how to do that?

set myPrefsFile to (choose file with prompt "Select a file to read:")
set notfound to ((path to desktop folder) as string) & "notfound.txt"
open for access notfound with write permission
open for access myPrefsFile
set AppleScript's text item delimiters to {","}
set prefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
set x to 0
set theplaylist to "Temp"
tell application "iTunes"
	make new user playlist with properties {name:"Temp-1"}
end tell
repeat number of items in prefsContents times
	set x to x + 1
	set track_name to item x of prefsContents
	tell application "iTunes"
		get view of front window
		tell library playlist 1
			set search_results to (search for track_name)
			copy search_results to theplaylist
		end tell
		if search_results's length > 1 then
			set trackNames to {}
			set trackartist to {}
			repeat with thisTrack in search_results
				try
					set end of trackNames to {name of thisTrack}
				on error
					set end of trackNames to "<track name missing>"
				end try
			end repeat
			set myResult to (choose from list trackNames)
			repeat with myResult in search_results
				duplicate myResult to playlist "Temp-1"
				if 1 = 1 then exit repeat
			end repeat
		else
			repeat with myResult in search_results
				duplicate myResult to playlist "Temp-1"
				if 1 = 1 then exit repeat
			end repeat
		end if
	end tell
	if search_results = {} then write track_name to file notfound
end repeat
close access file notfound

I thought it worked fine, but the selection of the track doesn’t really do anything since it chooses the first on the list regardless of what choice you make. I would like to capture that selection, and add it to the playlist. However, I was uder the impression that the “choose from list” syntax was performing that functionality. I suppose I am missing something. Any help would be appreciated!

Do you mean the track you have to select have the same identical name, artist, album artist etc???

If track name isn’t enough you can simply change the line

replace:
set end of trackNames to (name of thisTrack as string)
with:
set end of trackNames to (artist of thisTrack & " - " & name of thisTrack as string)

In fact you can put any song information there because it important which item is chosen and not it’s value.