File 文件存在在applescript中不起作用

File 文件存在在applescript中不起作用,file,applescript,exists,File,Applescript,Exists,我正在尝试检测文件是否存在。但是,脚本停止并出错,而不是转到else语句 你知道怎么做吗 tell application "Finder" if exists file h then display dialog "file exists" else display dialog "file doesnt exist" end if end tell 干杯 Ke这里有一个小例子: set h to (choose fil

我正在尝试检测文件是否存在。但是,脚本停止并出错,而不是转到else语句

你知道怎么做吗

tell application "Finder" 

    if exists file h then 

        display dialog "file exists"

    else

        display dialog "file doesnt exist"

    end if

end tell
干杯

Ke

这里有一个小例子:

set h to (choose file with prompt "Pick a file…") as string

tell application "Finder"

    if exists file h then

        display dialog "file " & return & quoted form of h & return & " exists"

    else

        display dialog "file " & return & quoted form of h & return & " missing!"

    end if

end tell
或者不使用as字符串,这使得它更简单:

set h to (choose file with prompt "Pick a file…")

tell application "Finder"

    if exists h then

        display dialog "file " & return & quoted form of (h as text) & return & " exists"

    else

        display dialog "file " & return & quoted form of (h as text) & return & " missing!"

    end if

end tell
使用时,以下各项似乎按预期工作:

set h to "/mach_kernel"
if FileExists(h) then
    display dialog (h & " exists!")
else
    display dialog (h & " does not exist")
end if

set h to "/foo"
if FileExists(h) then
    display dialog (" exists!")
else
    display dialog (h & " does not exist")
end if

on FileExists(theFile) -- (String) as Boolean
    tell application "System Events"
        if exists file theFile then
            return true
        else
            return false
        end if
    end tell
end FileExists

你应该展示你脚本的代码,或者至少是演示问题所需要的代码。好的,我已经做了,但这是一个非常直截了当的问题。不确定为什么需要脚本…在我的代码上下文中它不是重复的。我已经读过了,但它没有回答我的问题嘿,克,你能在你的示例代码中设置变量h吗?如果你的代码出错,那是因为变量h。您的代码是这样编写的:h是一个字符串。如果h不是字符串、文件引用、别名等,则代码将出错。所以你需要在你的例子中说明h是什么。或者至少从if语句中删除word文件,然后重试。