AppleScript: How to check if something is a directory or a file

Viewed 8778

Update: Solution below


Say you have a list of selected items from Finder. Say there are some files, folders, various bundles, and even some applications included in the selection.

Now say you only want those items that are (in UNIX terms) directories. I.e. you only want items you can cd to in the terminal.

You can check each item's kind property and see if it equals "Folder", but that doesn't work for application bundles or other bundles/packages, though they are in fact "folders" (directories)

If the items are actual file objects (not aliases), you can check each item's class property... except that doesn't always work either, since bundles are now "document file" instances, and applications are "application file" instances.

It's worse if you only have a list of aliases rather than actual file objects, since you can't check the class property; it'll always just say "alias".

The only solutions I can think of, are to either get the POSIX path of each item, and see if it has a trailing forward slash, or to send its path to a script written in a different language, which can check if something's a directory or not.

Both ideas seem crazy to me. Checking for a trailing forward slash is extremely hacky and fragile, and feeding everything to a different script is complete overkill.


Update: What Asmus suggests below does seem to be the only a good solution, as it seems there's no way for AppleScript to figure it out on its own:

do shell script "file -b " & filePosixPath

That'll return the string "directory" for folder, bundles, applications, packages, etc. etc.
But note(!) that for disks, it returns "sticky directory".

Here's a generalized function that works pretty well

on isDirectory(someItem) -- someItem is a file reference
    set filePosixPath to quoted form of (POSIX path of (someItem as alias))
    set fileType to (do shell script "file -b " & filePosixPath)
    if fileType ends with "directory" then return true
    return false
end isDirectory
5 Answers

Im using an alfred workflow which run this script with a keyboard combination of keys (⎇+F2). The script opens a terminal application based on what I got selected on finder, if is a folder then open a terminal inside selected folder, if is a file then open a terminal on parent folder. worked flawless

set pathList to "~"
if frontmost of application "Finder" then
    tell application "Finder"
        set fileList to selection as alias list
        if fileList is not equal to {} then
            set someItem to (item 1 of fileList) as text
            set filePosixPath to quoted form of (POSIX path of someItem)
            set fileType to (do shell script "file -b " & filePosixPath)
            if fileType ends with "directory" then
                set pathList to filePosixPath
            else
                set pathList to (quoted form of POSIX path of (folder of the front window as alias))
            end if
        else
            set pathList to (quoted form of POSIX path of (folder of the front window as alias))
        end if
        
    end tell
end if
tell application "System Events"
    tell application "Terminal"
        activate
        if exists window 1 then
            do script ("cd " & pathList) in window 1
        else
            do script ("cd " & pathList)
        end if
    end tell
end tell
Related