Phi*_*gan 23
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 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
Run Code Online (Sandbox Code Playgroud)
与它的接口可以通过以下方式清理......
my WriteLog("Once upon a time in Silicon Valley...")
on WriteLog(the_text)
set this_story to the_text
set this_file to (((path to desktop folder) as text) & "MY STORY")
my write_to_file(this_story, this_file, true)
end WriteLog
Run Code Online (Sandbox Code Playgroud)
纯AppleScript中的简短版本:
set myFile to open for access (choose file name) with write permission
write "hello world" to myFile
close access myFile
Run Code Online (Sandbox Code Playgroud)
似乎没有本机命令解决方案.相反,您必须打开并稍后关闭该文件.
我还了解到,如果只想将一些文本吐出到文件中,快速入侵是使用shell.
do shell script "echo TEXT > some_file.txt"
Run Code Online (Sandbox Code Playgroud)
@JuanANavarro.
使用shell时,您应该使用 TEXT和文件路径的引用形式.这将有助于阻止文件名中的空格和文本中的撇号(例如文本中的撇号)中的错误.
set someText to "I've also learned that a quick hack, if one only wants to spit a bit of text to a file, is to use the shell."
set textFile to "/Users/USERNAME/Desktop/foo.txt"
do shell script "echo " & quoted form of someText & " > " & quoted form of textFile
Run Code Online (Sandbox Code Playgroud)
上面的脚本运行正常.
如果我没有引用someText的形式
但相反,我有&someText 我会得到以下错误.
错误"sh:-c:第0行:在寻找匹配的'''时出现意外的EOF
sh:-c:第1行:语法错误:意外的文件结尾"第2位
" 我是"中的撇号被视为命令的一部分.
如果我有
将textFile设置为"/ Users/USERNAME/Desktop/some foo.txt"作为我的文件路径(注意空格.)并且没有 引用的textFile形式,而是我有 &textFile
然后当写出文件时,它将写入名为" some "而不是" some foo.txt "的文件
| 归档时间: |
|
| 查看次数: |
35297 次 |
| 最近记录: |