Applescript:"文件已经打开"写入新文件时

时间:2014-08-04 19:10:32

标签: ios objective-c applescript applescript-objc applescript-excel

我正在创建一个文件名,其文件名由常量和变量字符串组成。无论出于何种原因,我收到的错误是" [文件名]已经打开"当我真的只是创造它。该文件已创建,但我的内容都没有进入该文件。

我尝试过的所有修补程序都以另一个错误说明"网络文件权限结束。"

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

2 个答案:

答案 0 :(得分: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的所有按摩,我们会收到网络文件错误。该文件正在尝试保存在您无法保存的位置....

您需要验证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"}

这与你应该期待的相似。

答案 1 :(得分: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:用户:jun:桌面:subject_lines.csv已经打开。"

如果删除"将文件设置为文件字符串",并进行以下更改:

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

然后是错误"网络文件权限错误。"