使用Applescript将数据写入文件时出错

时间:2012-12-19 13:06:55

标签: applescript

我使用下面的脚本来编写文件,但有时会出现如下所述的错误,请提示..

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"

当我试图关闭时,我得到了这个错误:

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

1 个答案:

答案 0 :(得分:0)

我无法解释"网络文件权限错误"你收到了。

"文件文件Macintosh HD:库:应用程序支持:Macromedia:mms.cfg已经打开。"如果脚本在脚本编辑器中停止而未关闭文件,则会发生错误。如果存在某些其他逻辑错误导致AppleScript无法访问close access块中的on error命令(或者您在不合适的时间按下“停止”按钮),则会发生这种情况。当脚本停止时,Apple的AppleScript编辑器不会为您关闭泄露的文件引用。

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

相关问题