2012-04-10 28 views
2

CSVからデータを読み込むためのアプリスクリプトがあります(正常に!)。スクリプトのデータの編集が終わったら、変更したテキストをそのファイルに書き戻したい。 私のテキストがCSVファイルに書き戻されると、キャリッジリターンがすべて失われてしまい、1行で読み上げられます。私は間違って何をしていますか?Applescriptを使ってCSVにリストを書き込む

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 

お勧めはありますか?このコードの出力は次のようになります。私が欲しいのは

1,2,3 
A,B,C 
X,Y,Z 
+0

私が誤解していない場合、 'theList'変数は元のファイルのすべての段落(行)をリストとして保持しているため、ループ*内のファイル*に書き込むと、ファイルの*あなたが行を持っていることが何回もあります。おそらく、 'end repeat' *の後にファイル*に書き込むほうが理にかなっていますか? – fanaugen

答えて

3

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

あるとき、それは{"1,2,3", "A,B,C", "X,Y,Z"}のように見えるので、私は、あなたのtheListはその項目として(各ラインカンマで文字列)のラインを持っていると仮定し、右?

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

theData今まさに値

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

がありますので、ファイルに書き込むためにtextにリストを変換する前に、改行文字にtext item delimitersセット出力ファイルで何を見つけるか。

関連する問題