gpt4 book ai didi

ios - Applescript:写入新文件时为 "File Already Open"

转载 作者:行者123 更新时间:2023-11-29 10:39:44 26 4
gpt4 key购买 nike

我正在创建一个文本文件,其文件名将由常量和变量字符串组成。无论出于何种原因,我实际上只是在创建它时收到一条错误消息,提示“[文件名] 已打开”。文件已创建,但我的内容均未放入文件中。

我尝试过的所有修复都以另一个错误“网络文件权限”结束。

另外,我应该提到我的新文件将进入与用于创建新文件的另一个文件相同的容器,这是 filePathAlias 的用武之地。

有什么想法吗?提前致谢!

这是脚本:

-- get the file --
set filePathAlias to (choose file with prompt "** Choose File **")
set filePath to filePathAlias as string

tell application "Finder"
set fileName to name of filePathAlias
set containerPath to (container of filePathAlias) as string
end tell

set filePath to filePathAlias as string

-- get file container --
tell application "Finder"
set containerPath to (container of filePathAlias) as string
end tell

-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"

-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try

最佳答案

您没有为 filePathAlias 或 locationName 提供示例路径。我无法重现文件已打开的错误。我可以重现网络文件权限错误...所以:

set filepathalias to ((path to desktop folder as string) & "test" as string) as alias
--alias of folder on desktop called test... massaged well to be an alias that can later be converted to string when making containerPath

set locationName to "stuff you left out" --Just a name I assume...

-- get file container --
tell application "Finder"
set containerPath to ((container of filepathalias) as string)
end tell

-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"

-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try

如果您要在桌面上工作,这非常有效。问题似乎在使路径正确的阶段。

如果没有对我在第一行中执行的文件路径别名进行所有按摩,我们就会收到网络文件错误。该文件正在尝试保存到您无法保存到的位置....

您需要验证 filepathalias、containerPath 和 finalFile 是否都包含您期望的信息。

在 finalFile 设置的正下方,在编辑器中试试这个:

return {filepathalias as string, containerPath as string, finalFile as string}

我从上面得到的结果:

{"mac:Users:lithodora:Desktop:test:", "mac:Users:lithodora:Desktop:", "stuff you left out_RMP_2014.txt"}

这与您应该期望的相似。

关于ios - Applescript:写入新文件时为 "File Already Open",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25125826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com