2011-11-15 108 views
0

我一直在試圖寫入一個文件xmlhttp GET請求的結果。我試過下面的代碼:在Applescript中寫入文件 - 不工作

set the_file to "/Users/xxxx/Documents/outputvals.txt" as file specification 

set the_data to costItems 

try 
    open for access the_file with write permission 
set eof of the_file to 0 
write (the_data) to the_file starting at eof as list 
close access the_file 
end try 

但是它似乎沒有寫入任何文件。理想情況下,我想將xml'costItems'寫入文本文件的末尾。

回答

0

通常,如果您想對文件或文件夾執行任何操作,它必須是完整的alias引用。只需將as file specification更改爲as POSIX file as alias,那部分就會起作用!並且,刪除as list並刪除行set eof of the_file to 0。假設文件已經包含了信息,你基本上告訴腳本文件的結尾是文件的開始。

我希望這有助於!

編輯:我不知道爲什麼你的代碼不能正常工作(它對我來說)。好吧,我想你可以試試下面的(有點哈克,雖然,但哦:P): 集the_file到「/用戶/尼克/文檔/ outputvals

tell application "Finder" 
    try 
     set the_file to "/Users/xxxx/Documents/outputvals.txt" as POSIX file as alias 
    on error --file doesn't exist yet, so create it 
     set the_file to (make new document file at ("/Users/xxxx/Documents/" as POSIX file as alias) with properties {name:"outputvals", text:""}) 
     --Normally, when you're creating documents in this fashion, you would put the text you want in the document after the 'text' property, but for your sake I will use the alternative :) 
    end try 
end tell 
set the_data to costItems 
try 
    open for access the_file with write permission 
    write the_data to file the_file starting at eof 
    close access the_file 
on error --you never know when errors will crop up (when it comes to reading and writing files); better to be safe than sorry 
    try 
     close access the_file 
    end try 
end try 
+1

所以此刻的香港專業教育學院得到這個.TXT」的POSIX文件作爲別名 集the_data到costItems 嘗試 \t打開具有寫權限 \t寫(the_data)訪問the_file到the_file開始EOF \t接近接入the_file 結束嘗試 但它仍然不會寫入文件,如果我刪除文件它不會重拍它,任何建議? – dojogeorge

+0

@ user1045280看我的編輯。 :) – fireshadow52

+0

我試過使用,但它仍然無法正常工作,繼承人我的代碼: http://pastebin.com/UfmgZqqC – dojogeorge

相關問題