首页 > 解决方案 > 在 AppleScript 中使用 POSIX 表示法检查项目是否作为文件夹存在

问题描述

我有一个字符串,它以 POSIX 表示法表示文件夹中的文件/文件夹:

/Users/surfacedetail/Desktop/Temp/Test1

我想检查 Test1 是否存在并且是一个文件夹。以下说明它存在:

if exists my POSIX file myFileOrFolder then
    beep
end if

这工作正常,但如何判断它是文件夹还是文件?

问题是这是一个庞大脚本的一部分,这是一个更好的测试,它使用系统事件,但它不起作用。我认为这与我如何构建路径字符串有关。

tell application "Finder"
    set myScriptFilePath to (path to me)
    set mySourceFolder to folder of myScriptFilePath
    set myFileAliasList to the entire contents of mySourceFolder

    set myPOSIXSourceFolder to URL of mySourceFolder
    set myPOSIXSourceFolder to characters 8 thru -1 of myPOSIXSourceFolder as string

    repeat with myFile in myFileAliasList
        set myFileName to name of myFile
        log "myFileName: " & myFileName

        if (myFileName ends with ".txt") then
            set AppleScript's text item delimiters to "."
            set myFolderName to first text item of myFileName
            set myFolderPath to myPOSIXSourceFolder & myFolderName as string
            log "myFolderPath: " & myFolderPath

            tell application "System Events"
                if not (exists folder myFolderPath) then
                    beep
                    display dialog "Could not find Folder for File: " & myFileName
                    return {}
                end if
            end tell

        end if
    end repeat
end tell

标签: applescript

解决方案


你可以在System Events没有任何类型转换的情况下做到这一点

set thePath to "/Users/surfacedetail/Desktop/Temp/Test1"

tell application "System Events"
    if exists folder thePath then
        beep
    end if      
end tell

推荐阅读