I am new and fully beginner to applescript. Can applescript parse a .CSV file, split each line removing the first column and save it using the first column which has been removed as a filename?
Ex:
CSV file is “library.csv” containing:
“travel”,“list of travel”
“notes”,“list of notes”
“quotes”,“list of quotes”
etc…
I would like to have as many individual files as the number of lines of “library.csv” splitted like this (and the double quotes removed):
set destFolder to choose folder with prompt "Please select the Destinationfolder"
set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File"))
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with i from 1 to count of myArray
set fName to text 2 thru -2 of (text item 1 of (item i of myArray))
set destPath to (destFolder as text) & fName & ".txt"
set myContent to text 2 thru -2 of (text item 2 of (item i of myArray))
my write2file(destPath, myContent)
end repeat
set TID to AppleScript's text item delimiters
on write2file(thisFile, thisText)
try
open for access file the thisFile with write permission
write (thisText) to file the thisFile starting at eof
close access file the thisFile
on error
try
close access file the thisFile
end try
end try
end write2file
Wouahhh thanks a lot that is very fast and really appreciated.
I just tested it and it works almost at 100%.
I jsut have two small problems:
my delimiter is “,” and not only ,
Then, if my line contains for example the following:
“quotes”,“list of quotes, citations, blalbla”
then the output is only: list of quotes
I may have french accentued characters into the first column and then the file name is strange like:
À l’oeil.txt
instead of
À l’oeil.txt
point 1 should be solved
point 2 depends on specific text file encoding (difficult without having original data to test with)
try this:
--tested with operating system 10.7.4
set destFolder to choose folder with prompt "Please select the Destinationfolder"
set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File") as «class utf8»)
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\",\""
repeat with i from 1 to count of myArray
set fName to text 2 thru -1 of (text item 1 of (item i of myArray))
set destPath to (destFolder as text) & fName & ".txt"
set myContent to text 1 thru -2 of (text item 2 of (item i of myArray))
my write2file(destPath, myContent)
end repeat
set TID to AppleScript's text item delimiters
on write2file(thisFile, thisText)
try
open for access file the thisFile with write permission
write (thisText) to file the thisFile starting at eof
close access file the thisFile
on error
try
close access file the thisFile
end try
end try
end write2file
StefanK showed me a lovely way to set AppleScript’s text item delimiters:-
set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
-- Process Text...
set AppleScript's text item delimiters to atid
Thought I’d post it in case it’s of use to anyone, I use this method pretty much all the time now.