PDA

View Full Version : Applescript with Itunes not retaining track info after selection




tdill13
Jun 22, 2011, 11:12 PM
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.

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



kainjow
Jun 23, 2011, 10:15 PM
Here's the problem:
set myresult to (choose from list trackNames)
repeat with myresult in search_results
Do you know why this is wrong?

tdill13
Jun 23, 2011, 10:24 PM
Please let me know what the problem is as I do not know. Thanks!

kainjow
Jun 23, 2011, 11:06 PM
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:

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
Jun 24, 2011, 12:34 PM
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.