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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With