I have an applescript that reads data from a CSV (successfully!). After my script is done editing the data, I want it to write the modified text back to that file.
When my text is written back to the CSV file it has lost all of its carriage returns, so it reads as one line. What am I doing wrong?
set theFile to choose file with prompt "Select a text file:"
set theFileContents to read theFile
...
set theList to paragraphs of theFileContents
repeat with i from 2 to count theList
set AppleScript's text item delimiters to ","
set theLine to theList's item i
theLine
set clinic_id to first text item of theLine
....
set AppleScript's text item delimiters to ""
...
open for access theFile with write permission
set theData to theList as text
write the [theData] to theFile as text
close access theFile
display dialog the [theLine]
end if
end repeat
Any suggestions? The output of this code is:
1,2,3,A,B,C,X,Y,Z
when what I want is
1,2,3
A,B,C
X,Y,Z
I suppose your
theListhas lines (each line a string with commas) as its items, so it looks like{"1,2,3", "A,B,C", "X,Y,Z"}, right? So, before converting the list to atextin order to write it to the file, set thetext item delimitersto the newline character:theDatahas now the valuewhich is exactly what you’ll find in the output file.