Re: check if a folder exists
Re: check if a folder exists
- Subject: Re: check if a folder exists
- From: Paul Skinner <email@hidden>
- Date: Wed, 8 May 2002 15:28:42 -0400
OK, Maybe too much for a reply to a post with Newbie alert! at the top,
but here's one way.
--Concise version.
try
set fref to "Dalai:Users:paulskin:Desktop:DesiredFolder:"
tell application "Finder"
open fref as alias
end tell
on error
set previousDelimiters to AppleScript's text item delimiters
--Store this. You'll want it later.
set AppleScript's text item delimiters to ":" --We'll use this to
get the 'pieces' of the path that you supplied for the folder.
tell application "Finder"
set filePath to make new folder at ((text items 1 thru -3 of
fref) as text) & ":" as alias with properties {name:((text item -2 of
fref)) as text}
open filePath
end tell
set AppleScript's text item delimiters to previousDelimiters
end try
--Verbose version.
try
set filePath to "Dalai:Users:paulskin:Desktop:DesiredFolder:"
tell application "Finder"
open filePath as alias --test for the folder's existence.
end tell
on error --No folder!
set previousDelimiters to AppleScript's text item delimiters
--Store this. You'll want it later.
set AppleScript's text item delimiters to ":" --We'll use this to
get the 'pieces' of the path that you supplied for the folder.
--OK. Now that the delimiter is set to colon we can get the items
between the colons in your path.
--{"Dalai", "Users", "paulskin", "Desktop", "DesiredFolder", ""}
note that the trailing colon makes a null item at the end of the list.
--This next line of code gets the pieces of the path up to the
desired folder's container.
--it uses 'as text' to put the pieces back together with colons
between the pieces. the trailing colon makes it a folder path.
set the pathToTheFoldersContainer to ((text items 1 thru -3 of
filePath) & ":" as text) as alias
--This container must exist or else you'll need to do all this
again for the container folder.
--This line gets the next to last item in your path. That is the
folder's name.
set theFoldersDesiredName to ((text item -2 of filePath)) as text
tell application "Finder"
set filePath to make new folder at the
pathToTheFoldersContainer with properties {name:((text item -2 of
filePath)) as text}
open filePath --I just assumed you want the folder to open in
either case.
end tell
set AppleScript's text item delimiters to previousDelimiters --Now
you can set the delimiter back to whatever it was.
end try
On Wednesday, May 8, 2002, at 01:35 PM, Mark Oglesby wrote:
Newbie alert!
Can anyone give me an example of an if -then -else
statement that would check to see if a folder exists in
the finder.
If a folder exists I want the finder to simply open it else I
want it to create it.
SNIP
--
Paul Skinner
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.