Check if path is a folder

Hi,

I need some AppleScript to check whether an inputted path is a folder or not. So for example if I do:

set somevariable to “/Applications”

It would have to check whether the POSIX path “somevariable” is a folder or not.

There is probably a better way but this works and it’s simple.
If you can’t change directories into it then it doesn’t exist.

set posix_file_path to "/Applications"

try
	do shell script "cd " & quoted form of posix_file_path
on error
	display dialog "Dir does not exist"
end try

Cheers,

Craig

Hmm, that’s good, but I dunno if its ideal. My app, when you drop a file/folder on it it has to determine whether its a folder, if its a folder, do so and so, and if not, do so and so. Would that work for this purpose?

Your post suggested that it needed to be a posix path.
If it does not then you can do this.

set thePath to "G5:Users:craig:Desktop:SD_file.scpt"

if kind of (info for alias thePath) is "folder" then
	display dialog "It's a folder"
end if

Or something like this:

set somePath to "/Users/bell/Library/Scripts/BinarySearch(SK).scpt"
if kind of (info for (POSIX file somePath)) is "folder" then
	return "It's a folder"
else
	return "It's a file"
end if

Thanks a lot! I will try that :slight_smile:

OK, tried both of those examples from Craig and Adam and I dunno if its something wrong with the computer, but it takes a really long time for it to return the right answer…So I tried Craig’s first example at the top, but the problem with that was it detected applications like “Chess.app” as folders (because they technically are), but that won’t work for my purpose.

info for can take much time because it calculates also the size for the folder. Without size avoids that
You can determine file, folder and package folder (like an application) with the folder and package folder properties of info for


set posix_file_path to "/Applications"

set HFS_alias to POSIX file posix_file_path as alias
set {folder:Fo, package folder:Pa} to info for HFS_alias without size
if Fo and not Pa then
	say "it's a folder"
else if Fo and Pa then
	say "it's a package folder"
else
	say "it's a file"
end if

This seems to be pretty fast.
Returns true or false and it returns false if it is an app.

set itemPath to quoted form of "/path/to/folder"
do shell script "/usr/bin/env ruby -e \"puts File.directory?(" & itemPath & ")\""

Cheers,

Craig

Nice :slight_smile:

What’s the ‘echo’ for?

I’m retarded! :stuck_out_tongue:

I’ll fix it.

Cheers,

Craig

Addendum:
On my machine the script returns true if the argument is an application package bundle.

I’d prefer the pure AppleScript solution if the shell script call is in a repeat loop with a hugh amount of iterations,
because shell script calls are quite expensive

Even without calculating size, is “info for”, which collects a larger set of properties, more efficient than letting Finder detect the class? I’m not quite sure how to test the timing of my example, but I’m curious and am posting my example for such a test, if anyone is so inclined.

Detect Folder (droplet):

on open (docList)
	repeat with anItem in docList
		tell application "Finder" to if ((anItem as alias) as reference)'s class = folder then beep
	end repeat
end open

Hi Marc,

your solution is pretty cool.
As the Finder could be busy for some reasons with other stuff,
one of my personal basic principles is: Keep things away from the Finder as much as possible :wink:

Why not:

set unObjet to "Macintosh HD:Users:yvan_koenig:Desktop:DateTimeFormats_orig:"

tell application "System Events" to get class of item (unObjet as text)

Yvan KOENIG (from FRANCE jeudi 28 août 2008 13:20:40)

How do you get that value into a variable?

tell application "System Events" to set theType to (get class of item (unObjet as text))
theType

Model: iMac Intel 10.5.5
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)

thnx :smiley: