2013-06-21 46 views
0

我是AppleScript核心部分,我很快就會從購買打字機和搬進山區。AppleScript:試圖在提供POSIX路徑時寫入文件

任何人都可以,請給我解釋一下爲什麼這個失敗:

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" 
set mah_data to "some text!!!!!!" 

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean) 
    try 
     set the target_file to the target_file as text 
     set the open_target_file to ¬ 
      open for access file of target_file with write permission 
     if append_data is false then ¬ 
      set eof of the open_target_file to 0 
     write this_data to the open_target_file starting at eof 
     close access the open_target_file 
     return true 
    on error 
     try 
      close access file target_file 
     end try 
     return false 
    end try 
end write_to_file 

write_to_file(mah_data, mah_file, true) 

即使這種成功:

set mah_file to choose file 
set mah_data to "some text!!!" 

-- the rest is identical 

我已經試過:

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as file 

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as alias 

set mah_file to POSIX file "/Users/me/folder/fileinfo.txt" as text 

而且,由於AppleScript的乾脆就不告訴我爲什麼這是行不通的,我很好,真正失去了我的腦海裏。

回答

3

離開了filePOSIX file關鍵字:

set mah_file to "/Users/me/folder/fileinfo.txt" 
... 
set the open_target_file to open for access mah_file with write permission 
+0

哦,上帝是這個捂臉時刻。謝謝,不過。 – Jonline

1

readwrite也使用主編碼(如或的MacRoman MacJapanese)由缺省值。添加as «class utf8»以保留UTF-8文件中的非ASCII字符。

寫作:

set b to open for access "/tmp/1" with write permission 
set eof b to 0 
write "α" to b as «class utf8» 
close access b 

附加:

set b to open for access "/tmp/1" with write permission 
write "ア" to b as «class utf8» starting at eof 
close access b 

閱讀:

read "/usr/share/dict/connectives" as «class utf8» 
1

您可以用組合>>使用echo命令寫入文本文件

如果是紡織品Ë沒有按牛逼存在,如果文本文件已經存在,一個新的,將創建

你用>,你可以用相同的名字

您使用>>文字將覆蓋現有的紡織品添加到現有的文件

set mypath to "/Users/" & (short user name of (system info)) & "/Desktop/file.txt" as string 
set myText to "text to write in the text file" as string 

do shell script "echo " & "\"" & myText & "\"" & ">>" & mypath 

和mypath中是和POSIX路徑

希望那是你在尋找什麼

相關問題