使用 AppleScript 中的 POSIX 符号检查项目是否作为文件夹存在

Checking if an item exists as a folder using POSIX notation in AppleScript

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

/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

您可以使用 System Events 来完成,无需任何类型转换

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

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

已修复!这是因为我没有恢复文本分隔符,它需要这个:

set mySaveASDelimiters to AppleScript's text item delimiters

更改分隔符

set AppleScript's text item delimiters to mySaveASDelimiters