11
votes

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
4
Well, I´d say the easiest solution is to hand POSIX paths over to the command line tool file, where e.g. file -b /Applications/Safari.app simply returns "directory". It should be easy enough to incorporate that in a do shell script loop.Asmus
@Asmus: You're probably right. After I wrote the question I realized that I could just do a quick shell command rather than a full-on shell script. Still, it just seems strange to have to go outside AppleScript to figure out something as basic as whether or not something's a directory. I'll leave the question up for a day or so, and in the meantime feel free to write you comment as an answer so you can get the points :)Flambino
@Flambino I can't upvote your good question because I used all my votes up! :(fireshadow52
@fireshadow52: Heh, well it's the thought that counts – thanks :)Flambino

4 Answers

7
votes

There is a simple applescript solution. You can check if something is a "package" using system events.

tell application "Finder" to set theItems to (selection) as alias list

set canCDItems to {}
tell application "System Events"
    repeat with anItem in theItems
        if anItem is package folder or kind of anItem is "Folder" or kind of anItem is "Volume" then
            set end of canCDItems to contents of anItem
        end if
    end repeat
end tell
return canCDItems
1
votes

If the variable is just a string, use this form:

on isDirectory(someItem) -- someItem is a string
    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 return true
    return false
end isDirectory
0
votes

I needed this today, as I was trying to plug a couple of diff tools into Finder for Mountain Lion and wanted to check if the selected item or items were folders or files. This worked for me:

tell application "Finder"
  set sel1 to the selection
  set class1 to class of (item 1 of sel1)
  set cs to (class1 as string)
  ...
  if cs = "folder" or cs contains "class cfol" then
    set choice to display alert "Which tool?" buttons {"opendiff", "diffmerge"}
    set tool to (button returned of choice)

I found I needed to test cs for both cases, depending on whether I was running in the AppleScript Editor or as a result of clicking a toolbar button on Finder. Far from the neatest but, shorter than the previous AppleScript-only solution, this would be my alternative proposal

on isDirectory(someItem)
  ((class of someItem) as string) contains "fol"
end isDirectory

Results of full testing in all circumstances welcome!

0
votes

Standard Additions offers this (no System Events necessary):

set isBundleType to package folder of (info for (choose file))