How to check if folder exists


My second day with Applescript is going generally ok but I have hit some issues.

I am trying to determine if a folder exists before trying to create one but missing something in the process.

My attempt is below.

property homeFolder : "Macintosh HD:Users:Dayo:Documents:MyDocs:" as alias
tell application "Finder"
	set myPrefix to "ABC" as string
	set mySuffix to "000" as string
	set temp_name to myPrefix & "_" & mySuffix as string
	if (exists folder temp_name of homeFolder) is true then
		set temp_name to myPrefix & "_" & myNewSuffix as string
	end if
	set targetFolder to (make new folder at homeFolder with properties {name:temp_name})              
end tell

It seems the if statement is not registering.
I think it has to do with how I have set up temp_name as a string but not sure

Any pointers appreciated.


Found a answer on Page 5.

Applied the second one.

Nitpick: You should remove the unnecessary as string coercions.

well, I seem to be having a similar problem, I’m trying to determine if a file in ~/Library/Caches/ exists, and if it does delete it.

currently, I have

 set folder_to_delete to ((path to library as text) & "Caches:com.deleteme")

tell application "Finder"
   display dialog folder_to_delete
   if exists folder folder_to_delete then
      move folder_to_delete to trash 
   end if
end tell

However, “library” on line 1 refers to /Library/ instead of ~/Library/. I want this script to be portable, so hardcoding the path is a no-no.

Any idea as to what I’m doing wrong?

Model: MacBook Air 2.13GHz
Browser: Firefox 3.6.10
Operating System: Mac OS X (10.6)

I know I could do something like

   do shell script "rm -rf ~/Library/Caches/com.deleteme"
end try

But, I’d like to move this to the trash to avoid an, oh, wait, didn’t want to remove that permenantly

Actually the correct syntax is path to library folder.
The default setting is the library folder of the local domain (/Library).
Fortunately path to has the parameter from to specify the domain of the path

set folder_to_delete to ((path to library folder from user domain as text) & "Caches:com.deleteme")

@StefanK, thank you so much. I really appreciate it. I’ve spent 3 hours trying to figure that out before asking.

I knew trying to get the user’s library folder was easy, I just didn’t know what I was doing wrong.

Do shell script will usually out-perform applescripts, so you should use a shell command if you can.

If you do not want to delete the file, you can do:

do shell script “mv ~/Library/Caches/com.deleteme ~/.Trash/”
end try

This will move it to the user’s trash, and it should perform faster than equivalent applescript code.


A reply to the post #1:

Another way doing the same thing but without using the finder (script works when Finder is not running or hanging).

set homeFolder to path to home folder as string
set myPrefix to "ABC"
set mySuffix to "000"
set tempFolder to homeFolder & myPrefix & "_" & mySuffix as string
if not fileExists(POSIX path of tempFolder) then
	do shell script "mkdir " & quoted form of POSIX path of tempFolder
end if

on fileExists(posixPath)
	return ((do shell script "if test -e " & quoted form of posixPath & "; then
echo 1;
echo 0;
fi") as integer) as boolean
end fileExists

EDIT: the test -e checks if a file exists if you change this to test -d it will check if it’s a folder/directory too.

This is quite complicated. The -p switch of mkdir can create non-existing directories directly

set myPrefix to "ABC"
set mySuffix to "000"
do shell script "mkdir -p " & quoted form of (POSIX path of (path to home folder) & myPrefix & "_" & mySuffix)

Just if you’re interested:

Here is another interesting way to check if a file or folder exists (without tellling Finder). It takes Mac-like paths (as strings) as input:

--c--   CheckExistence(FileOrFolderToCheckString)
--d--   Check if a file or folder exists.
--a--   FileOrFolderToCheckString : string -- mac-like file or folder path
--r--   boolean -- true if exists
--x--   CheckExistence("Mac:Users:usrName:Desktop:test.txt") --> true or false
--u--   found somewhere on the internet
on CheckExistence(FileOrFolderToCheckString)
		alias FileOrFolderToCheckString
		return true
	on error
		return false
	end try
end CheckExistence

true but that’s why I refered back to post #1 because he asked to check first and then make the directory and so is my example :).

Also I should use ljr_nbg example but it’s a personal thing why I didn’t. I never use try catch code in my software with no special reason.