我无法弄清楚如何正确使用"写"在applescript中。我目前有这样的代码:
set randomArray to {"hello", "text", "file"}
set saveFile to (path to desktop as text) & "RandomFile.txt"
write randomArray to saveFile starting at eof as list
我知道这是不对的,但我似乎无法弄清楚在哪里放置正确的东西" saveFile"是的。
感谢任何帮助,谢谢:)
答案 0 :(得分:1)
可以在code-snippet (CTRL-点击进入脚本以查看它们)中找到写入文件的好例子 Error Handlers
,Write Error to Log
。< / p>
要保存数组/列表,您可以使用以下内容:
set randomArray to {"hello", "text", "file"}
set fullText to ""
repeat with i from 1 to number of items in randomArray
set thisItem to item i of randomArray
set fullText to fullText & thisItem
if i is not number of items in randomArray then set fullText to fullText & return
end repeat
my scriptLog(fullText)
on scriptLog(thisText)
set the logFilePath to ((path to desktop) as text) & "Script Log.txt"
try
open for access file the logFilePath with write permission
write (thisText & return) to file the logFilePath starting at eof
close access file the logFilePath
on error
try
close access file the logFilePath
end try
end try
end scriptLog
答案 1 :(得分:1)
如上所述,您需要在写入之前打开文件。写完后你必须关闭访问权限。通过读取处理程序的一次调用,BTW可以读取整个文件。 你问题的另一点是你想写一个清单。当然这是可能的,但您也需要将内容作为列表阅读。综合起来并从零的答案中得出要点,我们得到了这个:
set randomArray to {"hello", "text", "file"}
set saveFile to (path to desktop as text) & "RandomFile.txt"
-- write the file
set theFilehandle to open for access file saveFile with write permission
write (randomArray) to theFilehandle starting at eof as list
close access theFilehandle
-- read the file
read file saveFile as list
享受,迈克尔/汉堡