*Caveat, before 2 days ago, I had never written a single line of AppleScript. I learn quickly, but I may not understand everything you reply with right away. I will try to avoid asking questions about simple things I can learn from reading tutorials so we can focus on the nitty gritty.
I’m writing a script for work that takes values from a CSV file, finds those files in a specific folder, and then moves those files to a watched folder for Acrobat Distiller so that the resulting ads can be uploaded to the company website in PDF form. This post http://forums.quark.com/p/19276/77060.aspx helped me out a lot in learning to import and parse the data (and I used much of his code), and what I have works great as long as the data in the CSV file is something like “998786.eps,1001926.eps,1001927.eps”.
However, my problem lies in that the CSV file I get has extra values.
These are the first 3 lines of one of my sample files:
FILENAME,CATEGORY CODE,START DATE,END DATE,DISPLAY TYPE,HEADLINE,DESCRIPTION
1000097.eps,12310,7/23/10,7/30/10,thumb,LEROYS GREAT BEAR TIRE & AUTO,LEROYS GREAT BEAR TIRE & AUTO
999811.eps,12310,7/23/10,7/30/10,thumb,COLONIAL AMOCO,COLONIAL AMOCO
I was able to get it to ignore the first line by using “from 78” on my import, but I would like my script to ignore every field except the first one (ad number). How do I get it to skip the next 6 fields of each line, and then read in the next ad number?
Here is my code:
-- Choosing your 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 read file CSV_File from 78 using delimiter {return}
-- Parsing the information
try
set Line_Values to {}
repeat with ThisLine in CSV_Lines
--if there are spaces after your commas, make sure to add a space after the quoted comma on the next line
set end of Line_Values to GetTextItem(ThisLine as text, ",", 0)
end repeat
-- Targetting your desired information
if Line_Values is not {} then
set InfoCount to length of ((item 1 of Line_Values) as list)
set TargetInfo to ((item 1 of Line_Values) as list) as string
repeat with i from 1 to ((item 1 of Line_Values) as list)'s length
set ThisField to item i of ((item 1 of Line_Values) as list)
--move files to "Web" folder on Desktop
tell application "Finder"
duplicate file ThisField of folder "ARCHIVE:EPS ADS" to folder "Web" of desktop with replacing
end tell
if ThisField as text is TargetInfo then
exit repeat
end if
end repeat
set RequestedInfo to {}
repeat with ThisLine in Line_Values
set end of RequestedInfo to item i of ThisLine as list
end repeat
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" , "}
set RequestedInfo to RequestedInfo as text
set AppleScript's text item delimiters to OldDelims
else
error
end if
on error
tell application "Finder" to display dialog ThisField & " was not found"
end try
on GetTextItem(ThisString, ThisDelim, ThisItem)
-- ThisString -> String to look in
-- ThisDelim -> Text element that delimit the string
-- ThisItem -> Number of the element to return
copy the text item delimiters to OldDelims
set the text item delimiters to ThisDelim
if class of ThisItem is list then
set fromItem to (item 1 of ThisItem) as integer
set toitem to (item 2 of ThisItem) as integer
set arrItem to (text items fromItem thru toitem of ThisString) as alias
else
set arrItem to every text item of ThisString
end if
set the text item delimiters to OldDelims
if class of ThisItem is list then
return arrItem as text
else
if ThisItem is not equal to 0 then
return (item ThisItem of arrItem) as text
else
return arrItem -- return every item
end if
end if
end GetTextItem
Any help would be much appreciated.
-Adam
Model: PowerMac G5 Dual 2.0
AppleScript: 1.10.7
Browser: Firefox 3.6.6
Operating System: Mac OS X (10.4)