您如何在 Applescript 中正确使用 "write"?
How do you properly use "write" in Applescript?
我无法弄清楚如何在 applescript 中正确使用 "write"。我目前有这样的代码:
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" 放在什么地方。
感谢任何帮助,谢谢:)
可以在代码片段中找到写入文件的一个很好的示例 (CTRL-单击进入脚本以查看它们) Error Handlers
、Write Error to Log
.
要保存一个 array/list 你可以使用这样的东西:
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
如零所述,您需要在写入文件之前打开文件。写入后,您必须关闭访问权限。顺便说一句,一次调用读取处理程序就可以读取整个文件。
你问题的另一点是你想写一个列表。这当然是可能的,但您也需要阅读列表中的内容。将所有内容放在一起并从零的答案中提取要点,我们得到:
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
享受吧,迈克尔/汉堡
我无法弄清楚如何在 applescript 中正确使用 "write"。我目前有这样的代码:
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" 放在什么地方。
感谢任何帮助,谢谢:)
可以在代码片段中找到写入文件的一个很好的示例 (CTRL-单击进入脚本以查看它们) Error Handlers
、Write Error to Log
.
要保存一个 array/list 你可以使用这样的东西:
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
如零所述,您需要在写入文件之前打开文件。写入后,您必须关闭访问权限。顺便说一句,一次调用读取处理程序就可以读取整个文件。 你问题的另一点是你想写一个列表。这当然是可能的,但您也需要阅读列表中的内容。将所有内容放在一起并从零的答案中提取要点,我们得到:
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
享受吧,迈克尔/汉堡