(* CsvToListOfText 2008
with a lot of help from Kai
t = the text to parse
delimiterString = the string to delimit items in a row, usually a comma
quoteString = the string to wrap around items containing delimiters, usually a double quote. null if no quotes (faster).
newLineString = the string to delimit the end of each line, usually a linefeed
*)
on CsvToListOfText(t, delimiterString, quoteString, newLineString)
if t is in {"", null} then
return t
else
set oldDelimiters to text item delimiters
if quoteString is null then
set text item delimiters to newLineString
else -- if quoteString is not null then
set quoteSubstitute to "<Qsub>"
set quote2Substitute to "<Q2sub>"
set newLineSubstitute to "<NLsub>"
set delimiterSubstitute to "<Dsub>"
repeat with findSubstitutePair in {{delimiterString & quoteString, delimiterString & quoteSubstitute}, {newLineString & quoteString, newLineString & quoteSubstitute}, {quoteString & quoteString, quote2Substitute}, {quoteSubstitute, quoteString}}
set {findString, substituteString} to findSubstitutePair
set text item delimiters to findString
set t to text items in t
set text item delimiters to substituteString
set t to t as string
end repeat
set text item delimiters to quoteString
end if
script listHandler
property l : t's text items
end script
if quoteString is null then
set text item delimiters to delimiterString
else
repeat with i from 1 to count listHandler's l by 2
set text item delimiters to delimiterString
set t to text items of listHandler's l's item i
set text item delimiters to delimiterSubstitute
set t to t as string
set text item delimiters to newLineString
set t to t's text items
set text item delimiters to newLineSubstitute
set listHandler's l's item i to t as string
end repeat
set text item delimiters to ""
set t to listHandler's l as string
set text item delimiters to quote2Substitute
set listHandler's l to t's text items
set text item delimiters to quoteString
set t to listHandler's l as string
set text item delimiters to newLineSubstitute
set listHandler's l to t's text items
set text item delimiters to delimiterSubstitute
end if -- quoteString is not null
repeat with i from 1 to count listHandler's l
set listHandler's l's item i to text items of listHandler's l's item i
end repeat
set text item delimiters to oldDelimiters
return listHandler's l
end if -- t is not in {"",null}
end CsvToListOfText
or:
2. Use Regex (a regular _expression_) to parse CSV. Pure AppleScript doesn't do regex, but you can get it to call something that does, such as Hai's TextCommand's or Satimage's scrioting additions, or use do shell to call perl or something else to invoke it. Here's a regex that's supposed to work, though I haven't tested calling it from AppleScript:
("(?:[^"]|"")*"|[^",\r\n]*)(,|\r\n?|\n)?
and other places
or:
3. Script an app like Excel to open the CSV file and read the cell elements from the app.
or:
4. Call another faceless utility such as PHP or perl or ruby or python which may have a built in CSV handling routine. I only know of the "do shell script" method of invoking those, and have only tried with perl.
Tom
BareFeet (aka T&B)