Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tdill13

macrumors newbie
Original poster
Jun 22, 2011
3
0
I have put together a script that will process song information 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. I have added the ability to select a song when the search results are more than one. However, it doesn't matter what I select from the list. It always adds the song at the very beginning of the selection regardless of what is chosen. Any help would be greatly appreciated.

Code:
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 (artist of thisTrack & " - " & name of thisTrack as string)
				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
 
Last edited by a moderator:

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Here's the problem:
Code:
set myresult to (choose from list trackNames) 
repeat with myresult in search_results
Do you know why this is wrong?
 

tdill13

macrumors newbie
Original poster
Jun 22, 2011
3
0
No, I don't know please enlighten me.

Please let me know what the problem is as I do not know. Thanks!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Two things are wrong. One, the repeat loop is looping over search_results instead of myresult, and second, it's defining myresult again as the current item in search_results.

I'd suggest changing the loop to something like this:

Code:
set myresult to (choose from list trackNames)
repeat with a_result in myresult
    duplicate a_result to playlist "Temp-1"
end repeat

However the "duplicate" line is going to be wrong, as "a_result" is just a string of the artist and track name. You need to figure out a way to associate that value with the original track inside search_results.
 

tdill13

macrumors newbie
Original poster
Jun 22, 2011
3
0
Duplicate Line

Yes, the duplicate line is really the issue I am trying to find a solution for. The script works fine except for that issue. I need to know how to convert the string of the selection back to the track information, so that Itunes can add it to the playlist. Right now it just adds the first song from the list.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.