2

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
1
  • If I am not mistaken, the theList variable holds all the paragraphs (lines) of the original file as a list, so writing it to the file inside the loop will write the entire contents of the file as many times as you have lines. Perhaps it would make more sense to write to file after the end repeat? Commented Apr 11, 2012 at 7:58

1 Answer 1

3

I suppose your theList has 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 a text in order to write it to the file, set the text item delimiters to the newline character:

set AppleScript's text item delimiters to "\n"
set theData to {"1,2,3", "A,B,C", "X,Y,Z"} as text

theData has now the value

"1,2,3
A,B,C
X,Y,Z"

which is exactly what you'll find in the output file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.