Thursday, February 25, 2021

#1 2008-08-23 05:31:05 pm

wizboyx86
Member
Registered: 2008-05-20
Posts: 71

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.

Offline

 

#2 2008-08-23 05:48:39 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Check if path is a folder

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.

Applescript:

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

Offline

 

#3 2008-08-23 06:20:02 pm

wizboyx86
Member
Registered: 2008-05-20
Posts: 71

Re: Check if path is a folder

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?

Offline

 

#4 2008-08-23 06:25:20 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Check if path is a folder

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

Applescript:

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

Offline

 

#5 2008-08-23 06:52:25 pm

Adam Bell
Administrator
From:: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4666

Re: Check if path is a folder

Or something like this:

Applescript:

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


Mac mini running 10.14.6, 2011 27" iMac as display.

Offline

 

#6 2008-08-23 08:31:53 pm

wizboyx86
Member
Registered: 2008-05-20
Posts: 71

Re: Check if path is a folder

Thanks a lot! I will try that :-)

Offline

 

#7 2008-08-23 09:29:54 pm

wizboyx86
Member
Registered: 2008-05-20
Posts: 71

Re: Check if path is a folder

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.

Last edited by wizboyx86 (2008-08-23 09:32:35 pm)

Offline

 

#8 2008-08-24 02:28:45 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11736
Website

Re: Check if path is a folder

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

Applescript:


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

Last edited by StefanK (2008-08-24 02:29:42 am)


regards

Stefan

Offline

 

#9 2008-08-27 08:24:08 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Check if path is a folder

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

Applescript:

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

Cheers,

Craig

Offline

 

#10 2008-08-27 08:27:57 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11736
Website

Re: Check if path is a folder

Craig Williams wrote:

This seems to be pretty fast.


Nice smile

What's the 'echo' for?


regards

Stefan

Offline

 

#11 2008-08-27 08:33:28 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Check if path is a folder

I'm retarded! tongue

I'll fix it.

Cheers,

Craig

Offline

 

#12 2008-08-27 09:03:16 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11736
Website

Re: Check if path is a folder

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


regards

Stefan

Offline

 

#13 2008-08-27 12:51:47 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 999

Re: Check if path is a folder

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):

Applescript:

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


Filed under: Finder

Offline

 

#14 2008-08-27 01:06:48 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11736
Website

Re: Check if path is a folder

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

Last edited by StefanK (2008-08-27 01:07:25 pm)


regards

Stefan

Offline

 

#15 2008-08-28 05:24:07 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4610

Re: Check if path is a folder

Why not:

Applescript:

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)


Filed under: System

Offline

 

#16 2009-04-01 06:44:53 pm

47211
Member
Registered: 2008-01-04
Posts: 23

Re: Check if path is a folder

How do you get that value into a variable?

Offline

 

#17 2009-04-02 06:52:27 am

Matt-Boy
Member
Registered: 2005-10-21
Posts: 480

Re: Check if path is a folder

Applescript:

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)


Filed under: System

Offline

 

#18 2009-04-02 04:30:46 pm

47211
Member
Registered: 2008-01-04
Posts: 23

Re: Check if path is a folder

thnx big_smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)