1

我正在使用以下脚本编写文件,但有时会出现如下所述的错误,请建议..

set filepath to POSIX path of "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
try
    tell application "System Events"
        if file filepath exists then
            set myFile to open for access file filepath with write permission
            set fileData to read myFile
            set eof myFile to 0
            write "blah blah" to myFile
            close access myFile
        else
            return "File Not Found"
        end if
    end tell
on error
    return false
end try

错误:

 "Network file permission error." number -5000 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"

有时我会收到此错误,无法关闭打开的文件

"File file Macintosh HD:Library:Application Support:Macromedia:mms.cfg is already open." number -49 from file "Macintosh HD:Library:Application Support:Macromedia:mms.cfg"

当我尝试关闭 offile 时,出现此错误:

on openAFile(filepath)
    try
        set fp to open for access filepath with write permission

    on error errstr number errNum
        if errNum = -49 then
            close access filepath
            set fp to open for access filepath with write permission
        else
            display dialog errstr
            return false
        end if
    end try

    return fp
end openAFile

set pointer to openAFile("Macintosh HD:Library:Application Support:Macromedia:mms.cfg"
set fileContents to read pointer

错误

"Can’t make \"Macintosh HD:Library:Application Support:Macromedia:mms.cfg\" into type file." number -1700 from "Macintosh HD:Library:Application Support:Macromedia:mms.cfg" to file
4

1 回答 1

0

我无法解释您收到的“网络文件权限错误”。

“文件文件 Macintosh HD:Library:Application Support:Macromedia:mms.cfg 已打开。” 当您在脚本编辑器中停止脚本而不关闭文件时会发生错误。如果有一些其他逻辑错误阻止 AppleScript 到达close access您的块中的命令on error(或者您在不合时宜的时间按下停止按钮),则会发生这种情况。当脚本停止时,Apple 的 AppleScript 编辑器不会为您关闭泄露的文件引用。

您在打开文件之前尝试关闭文件的解决方法可能有效,但您传递的是字符串而不是文件路径的fileor alias。使用open for access file filepathclose access file filepath

于 2012-12-20T01:54:29.620 回答