2012-12-19 46 views
1

wrting数据转换成文件正在使用下面的脚本编写的文件,但有时会收到错误类似下面,请建议..错误而使用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" 

当我试图关闭OFILE正在此eror:

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 

回答

0

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

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

你的解决方法试图打开它可能工作之前关闭该文件,但你传递一个字符串,而不是一个filealias的文件路径。使用open for access file filepathclose access file filepath

+0

是他们解决网络文件权限错误的方法吗? –