Page 1 of 1

how to read CSV file in applescript

PostPosted: 07 Aug 2008, 18:18
by poovili
Hi,
I need to open the quark file by reading the filename from the CSV file. I need help for reading the CSV file line by line...
Thanks in advance

how to read CSV file in applescript

PostPosted: 08 Aug 2008, 05:50
by Scripting_Ace
This is not typically a "Quark" but here you go anyway...

001[b]-- Choosing your file
002[b]set [b]CSVstr [b]to [b]"Please locate your CSV file...[b]"
003[b]set [b]CSV_File [b]to [b]([b]choose file [b]with prompt [b]CSVstr[b]) [b]as [b]text

004[b]-- Reading your file to memory
005[b]set [b]CSV_Lines [b]to [b]read [b]file [b]CSV_File [b]using delimiter [b]{[b]return[b]}

006[b]-- Parsing the information
007[b]try
008[b]set [b]Line_Values [b]to [b]{}
009[b]repeat [b]with [b]ThisLine [b]in [b]CSV_Lines
010[b]set [b]end [b]of [b]Line_Values [b]to [b]GetTextItem[b]([b]ThisLine [b]as [b]text[b], [b]",[b]", 0[b])
011[b]end [b]repeat

012[b]-- Targetting your desired information
013[b]if [b]Line_Values [b]is [b]not [b]{} [b]then
014[b]set [b]InfoCount [b]to [b]length [b]of [b](([b]item 1 [b]of [b]Line_Values[b]) [b]as [b]list[b])
015[b]set [b]InfoStr [b]to [b]"Your data appears to have [b]" [b]& [b]InfoCount [b]& [b]" field(s) per line(s), please choose the desired data from this example...[b]"
016[b]set [b]TargetInfo [b]to [b]([b]choose from list [b](([b]item 1 [b]of [b]Line_Values[b]) [b]as [b]list[b]) [b]with prompt InfoStr) as string017repeat with i from 1 to ((item 1 of Line_Values) as list)'s length018set ThisField to item i of ((item 1 of Line_Values) as list)019if ThisField as text is TargetInfo then020exit repeat021end if022end repeat023set RequestedInfo to {}024repeat with ThisLine in Line_Values025set end of RequestedInfo to item i of ThisLine as list026end repeat027set OldDelims to AppleScript's text item delimiters028set AppleScript's text item delimiters to {", "}029set RequestedInfo to RequestedInfo as text030set AppleScript's text item delimiters to OldDelims031-- Returning the requesting information032display dialog "Here's the list of the information your requested:" & return & return & RequestedInfo033else034error035end if036on error037display dialog "It does not appear as if your file is a valid CSV file or that it contains any information!"038end try039on GetTextItem(ThisString, ThisDelim, ThisItem)040-- ThisString -> String to look in041-- ThisDelim -> Text element that delimit the string042-- ThisItem -> Number of the element to return043copy the text item delimiters to OldDelims044set the text item delimiters to ThisDelim045if class of ThisItem is list then046set fromItem to (item 1 of ThisItem) as integer047set toitem to (item 2 of ThisItem) as integer048set arrItem to (text items fromItem thru toitem of ThisString)049else050set arrItem to every text item of ThisString051end if052set the text item delimiters to OldDelims053if class of ThisItem is list then054return arrItem as text055else056if ThisItem is not equal to 0 then057return (item ThisItem of arrItem) as text058else059return arrItem -- return every items060end if061end if062end GetTextItemNotes: The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip all of them.Hope this helps!

how to read CSV file in applescript

PostPosted: 09 May 2014, 12:14
by josekuruvilla
I was searching for something very similar. I want to read content of a CSV file. I have numbers (which is coming from Indesign). If that number exist in first column of the CSV I want to get the corresponding value from second column? Any help?