1

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

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

另外,我应该提一下,我的新文件将进入与另一个用于创建新文件的文件相同的容器中,这就是新文件的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
4

2 回答 2

1

您没有提供 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"}

这与您应该期望的相似。

于 2014-08-04T23:52:39.480 回答
1
-- Convert the file to a string
set desktop_path to POSIX path of (path to desktop)
set theFile to desktop_path & "subject_lines.csv" as POSIX file

set theFile to theFile as string

-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission

write "Hello Kitty" to theOpenedFile

-- Close the file
close access theOpenedFile

这将导致错误“文件文件 Mac:Users:jun:Desktop:subject_lines.csv 已打开。”

如果删除“set theFile to theFile as string”,并做如下修改:</p>

set theFile to desktop_path & "subject_lines.csv" as string

然后错误“网络文件权限错误”。

于 2017-08-03T12:30:55.050 回答