title: AppleScript: How to check if something is a directory or a file
body: <p><strong>Update:</strong> Solution below</p>
  
  <hr>
  
  <p>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.</p>
  
  <p>Now say you only want those items that are (in UNIX terms) <em>directories</em>. I.e. you only want items you can <code>cd</code> to in the terminal.</p>
  
  <p>You can check each item's <code>kind</code> 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)</p>
  
  <p>If the items are actual file objects (not aliases), you can check each item's <code>class</code> property... except that doesn't always work either, since bundles are now "document file" instances, and applications are "application file" instances.</p>
  
  <p>It's worse if you only have a list of aliases rather than actual file objects, since you can't check the <code>class</code> property; it'll always just say "alias".</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <hr>
  
  <p><strong>Update:</strong> What Asmus suggests below does seem to be <strike>the only</strike> a good solution<strike>, as it seems there's no way for AppleScript to figure it out on its own</strike>:</p>
  
  <pre><code>do shell script "file -b " &amp; filePosixPath
  </code></pre>
  
  <p>That'll return the string "directory" for folder, bundles, applications, packages, etc. etc.<br>
  But note(!) that for disks, it returns "sticky directory".</p>
  
  <p>Here's a generalized function that works pretty well</p>
  
  <pre><code>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 " &amp; filePosixPath)
      if fileType ends with "directory" then return true
      return false
  end isDirectory
  </code></pre>