How to ignore extra field values in a CSV file import?

*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)

Hello and welcome!

I hope I have understood your problem correctly in that what you wanted was the field value which is a number that comes straight after the eps file on each line. :slight_smile:


-- Choosing your file
-- 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 every paragraph of (read file CSV_File from 78) 
-- assuming there are no fixed number of fields on each line
set Line_Values to {}
set {tids, text item delimiters} to {text item delimiters, ","}
repeat with i from 1 to (count CSV_Lines)
	set end of Line_Values to text item 2 of item i of CSV_Lines
end repeat
set text item delimiters to tids
log Line_Values --> (*12310, 12310*)

Well that certainly outputs the correct values in the event log (I needed the .eps files, so I changed “text item” to 1 instead of 2), but if I add in my duplicate script into the repeat, my event log gives me:
tell application “Finder”
copy file {“1000097.eps”} of folder “ARCHIVE:EPS ADS” to folder “Web” of desktop
“Finder got an error: Can’t make some data into the expected type.”

I’m guessing it’s erroring because of the brackets around the filename. If I move the duplicate script outside the repeat after the log command, it gives me the same error, but lists all the EPS files I want to move instead of just one.

I’m not sure what the problem is. Any ideas?

Thanks,
-Adam

Hi,

there are two problems: First the braces, it coerces the file to a list, second the proper command to copy files in the Finder is duplicate. Copy is a command to assign a value to a variable (similar to set without preserving the referene(s))

tell application “Finder”
duplicate file “1000097.eps” of folder “ARCHIVE:EPS ADS” to folder “Web” of desktop
end tell

I’ve been using the duplicate command. My last post just contained the output of the event log, not any code. Here is the code that created said event log:

-- 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 every paragraph of (read file CSV_File from 78)

-- assuming there are no fixed number of fields on each line
set Line_Values to {}
set {tids, text item delimiters} to {text item delimiters, ","}
repeat with i from 1 to (count CSV_Lines)
	set end of Line_Values to text item 1 of item i of CSV_Lines
	--move files to "Web" folder on Desktop
	tell application "Finder"
		duplicate file Line_Values of folder "ARCHIVE:EPS ADS" to folder "Web" of desktop with replacing
	end tell
end repeat
set text item delimiters to tids
log Line_Values --> (*12310, 12310*)

My question was more, why am I getting brackets in my output? If I add “as text” or “as string” to the end of the “set end of Line_Values” line, I still get the same problem.

you get a list because you define Line_Values as an empty list and add items to it.
If you want to use only the file name try something like this


.
repeat with i from 1 to (count CSV_Lines)
	set fileName to text item 1 of item i of CSV_Lines
	set end of Line_Values to fileName
	--move files to "Web" folder on Desktop
	tell application "Finder"
		duplicate file fileName of folder "ARCHIVE:EPS ADS" to folder "Web" of desktop with replacing
	end tell
end repeat
.

Perfect! A little tweaking here and there and it works absolutely perfectly. It took me a while to figure out why my error message wouldn’t work anymore, until I realized the whole thing had to be inside a “try” block. Thanks for your help, guys.

-Adam