AppleScript:如果文件不存在

3 applescript

我需要一些方法来确定特定文件是否存在.如果存在则执行一个脚本,如果没有,则执行另一个脚本.这是我在applescript中的逻辑:

If exists "File:Path:To:theFile"

tell application "Finder"
open "File:Path:To:the:script"
end tell

else

tell application "Finder"
open "File:Path:To:the:Anotherscript"
end tell

end if
Run Code Online (Sandbox Code Playgroud)

唯一的问题是,有时当我使用上述逻辑时,脚本无法说找不到文件.我需要一个完整的证明,从来没有失败的方式来查看文件是否存在.我愿意使用终端或者AppleScript.我敢肯定有人之前遇到过这个问题,但我已经在网上寻找答案,但却找不到答案.

sti*_*tib 8

在您的原始代码中,您为exists函数提供了一个字符串,而不是一个文件,即使它是文件的路径.你必须明确地给它一个文件,或者它就像你试图做的那样对待它

exists "god"
Run Code Online (Sandbox Code Playgroud)

要么

exists "tooth fairy"
Run Code Online (Sandbox Code Playgroud)

exists命令不会知道你在说什么.你可以用

return exists alias "the:path:to:a:file" 
Run Code Online (Sandbox Code Playgroud)

但是除非文件实际存在,否则别名不起作用,因此不存在的文件将产生错误.您当然可以捕获错误并对其执行某些操作,但只是将exists函数赋予文件对象更简单.文件对象属于Finder应用程序,因此:

return exists file "the:path:to:a:file" of application "Finder"
Run Code Online (Sandbox Code Playgroud)

HTH