我是AppleScript小组,我离购买类型作家并进入山区不远。
请允许任何人向我解释为什么会失败:
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无法告诉我为什么这不起作用,所以我很好,真的失去了理智。
答案 0 :(得分:3)
忽略file
和POSIX file
个关键字:
set mah_file to "/Users/me/folder/fileinfo.txt"
...
set the open_target_file to open for access mah_file with write permission
答案 1 :(得分:1)
read
和write
也使用主要编码(如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»
答案 2 :(得分: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路径
希望这就是你在寻找的东西