I’m trying to write a script that takes a CSV file I create based on episode lists of TV shows from Wikipedia, and use it to automagically run through a list of selected files in iTunes and do all the tagging and renaming for me. The problem lies in that to parse the CSV file, I need to do a repeat loop through each line to store the data. But in order to progress through the list of selected files, I also need a repeat loop. What I’m trying to do, and maybe it’s just because my brain is tired that I’m having trouble, is make each new line from the CSV file correspond to the next file in the list of selections. Here’s what I have so far. I know it’s sloppy (it’s bits and pieces of a few previous scripts), and there’s stuff in the wrong spots, but it’s my starting point. I tend to create log files for troubleshooting purposes, just to make sure my variables are on the right track. I find it less intrusive than dialogs.
The way it is now, it will work, so long as I have one iTunes file selected and either only one line in the CSV file, or the iTunes file selected corresponds with the last line of the CSV file.
--creates log file
set log_file to (path to desktop as text) & "ShowLog.txt"
global log_file
WriteLog("Program Started....")
property dialog_timeout : 3 -- set the amount of time before dialogs auto-answer.
-- Choose a file
set CSVstr to "Please locate your CSV file..."
set CSV_File to (choose file with prompt CSVstr) as text
-- Reading your file to memory
set CSV_Lines to every paragraph of (read file CSV_File from 1)
set AppleScript's text item delimiters to {""}
try
set Line_Values to {}
set {tids, text item delimiters} to {text item delimiters, " "}
-- moves through the list one item at a time
repeat with i from 1 to (count CSV_Lines)
set savedTIDS to AppleScript's text item delimiters
set seasonNumber to text item 1 of item i of CSV_Lines
set episodeNumber to text item 2 of item i of CSV_Lines
set episodeID to text item 3 of item i of CSV_Lines
set episodeTitle to text item 4 of item i of CSV_Lines
set directedBy to text item 5 of item i of CSV_Lines
set writtenBy to text item 6 of item i of CSV_Lines
set airDate to text item 7 of item i of CSV_Lines
set desc to text item 8 of item i of CSV_Lines
set showName to text item 9 of item i of CSV_Lines
set allInfo to (showName & " S" & seasonNumber & "E" & episodeNumber & " - " & episodeTitle & "
" & "Directed by " & directedBy & " Written by " & writtenBy & " - Original air date: " & airDate & "
" & desc) as text
set trackComment to ("S" & seasonNumber & "E" & episodeNumber & "
" & desc) as text
set tid to AppleScript's text item delimiters
WriteLog(allInfo)
tell application "iTunes"
if selection is not {} then -- if tracks are selected...
set sel to selection
set numTracks to (length of sel)
set theFiles to (a reference to selection)
repeat with j in sel
tell j
set season number to seasonNumber
set episode number to episodeNumber
set episode ID to episodeID
set name to episodeTitle
set long description to desc
set description to desc
set show to showName
set artist to showName
set sort show to showName
set bookmarkable to false
set comment to trackComment
set lyrics to allInfo
set sort show to showName
set shufflable to false
set video kind to TV show
set track number to episodeNumber
end tell
end repeat
end if
end tell
end repeat
return allInfo
end try
set AppleScript's text item delimiters to savedTIDS
set AppleScript's text item delimiters to {""}
WriteLog("Program Ended...
to WriteLog(text4Log)
set right_Now to (current date) as text
set wri to open for access file log_file with write permission
write (right_Now & " " & text4Log & return) to wri starting at eof
close access wri
end WriteLog
Model: 2006 Mac Pro
AppleScript: 2.1.2
Browser: Firefox 3.6.8
Operating System: Mac OS X (10.6)