checking if folder exists

Hi Folks-

below is a snippet of script I’m working on. It’s supposed to check to see if a subfolder already exists, and if not, create one. I am getting the error ‘Finder got an error: Can’t get folder “Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #95,000”.’

this is part of a larger script that partially works, so i’m working on it one bit at a time.

on run
set vactualfolder to “95,000” as string
set vfullpath to “Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #” & vactualfolder
tell application “Finder”
vfullpath as string
if exists folder vfullpath = true then --checks to see if folder exists
display dialog “folder already exists”
else
make new folder at “Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #:” with properties {name:vactualfolder} – if no folder exists, creates new one

	end if
end tell

end run

as i’m an applescript newbie i’m not quite sure what i’m doing wrong…
thanks!!

-Ralph

This works for me…

set some_folder to "95,000"
set full_path to "Firestar:Users:jnierodzik:Desktop:folder # " & some_folder

tell application "Finder"
	if exists full_path then
		display dialog "yep, it's there"
	else
		display dialog "Scotty, we have a problem"
	end if
end tell

Hi Ralph

this line doesn’t work:

if exists folder vfullpath = true then --checks to see if folder exists

it works with:

if exists folder vfullpath then --checks to see if folder exists

but I would do it like this

on run
    set vactualfolder to "95,000" as string
    set vfullpath to "Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #" & vactualfolder
    try
        vfullpath as alias -- throws an error if folder doesn't exist
    on error
        tell application "Finder" to make new folder at "Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #:" with properties {name:vactualfolder} -- if no folder exists, creates new one
    end try
end run

Hi Stefan, why would you do an as alias as oppossed to a if exists?

the result is the same, it’s a matter of taste :wink:
you can also use in this case just

try
	tell application "Finder" to make new folder at "Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #:" with properties {name:vactualfolder}
end try

Hello James

in your original code, the line

if exists folder full_path = true then --checks to see if folder exists

is seen by AppleScript as:

if exists folder (full_path = true) then --checks to see if folder exists

as full_path is a string, the value of (full_path = true) is always false and so the code always try to create the folder.
When the folder already exists it’s normal that with such a code you get an error.

Putting parentheses by yourself a different way:
if (exists folder full_path) = true then --checks to see if folder exists

would give a correct behaviour but, as StephanK wrote, it’s faster to code

full_path as alias.

this short piece of code checks that there is an alias named full_path.
If it exists, the result is true
If it doesn’t, the result is false

It’s exactly what you tried to get.

Yvan KOENIG (from FRANCE mardi 6 mars 2007 19:31:36)

This code worked for me:

if exists folder vfullpath then --checks to see if folder exists

thanks StefanK and all!

-Ralph

Bonsoir Yvan,

it’s not quite correct:
If it exists, the result is the alias
If it doesn’t, it throws an error

Semantically, try/error statements like this shouldn’t be used for state testing if you can help it (that is, if there’s a method specifically designed to do what you need to accomplish). Two reasons:

  1. It alters the readable logic of your code. You’re not attempting to perform an action and then catching problems with errors, you’re evaluating something . . . yet you’re writing code as though you were doing the former. Bad practice.

  2. To accomplish one task, you’re using a method not intended for that purpose. The underlying implementations of the two operations are different, and may yield performance or behavioural differences. Additionally, the underlying implementation of the differerently purposed method may change without notice in future API revisions. Since the only concern of the method is to perform its documented behaviour, you cannot be assured the performance tuning or behaviour you currently see, for the unintended purpose.

There’s a method for this that returns a boolean (the one in your first example, using “exists folder”). Use that instead.

Ok, Folks-

I think I need a negative conditional (create folder if none exists) to get it to jump to the duplicate (copy over) file section.

The script works if it doesn’t need to create a new folder. However, if I give it a file that doesn’t correspond to an existing folder, I get the following error: "File Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #99,000 wasn’t found.

to split(vsomeText, vdelimiter) --sets up delimiter

set AppleScript's text item delimiters to vdelimiter

set vsomeText to vsomeText's text items

set AppleScript's text item delimiters to {""}

return vsomeText

end split

on open of vdropList

repeat with vtheFile in vdropList --repeats with each item in the droplist
	
	set vchosenFile to last item of split(vtheFile as string, ":") --only returns file name from path
	set vfilenumber to first item of split(vchosenFile as string, "_") as number -- only returns digits from filename
	
	set vcompare to 10000 --starts with 10,000s folder
	repeat
		set vcompare to vcompare + 1000 -- increments by 1000
		if vcompare > vfilenumber then exit repeat -- gets out of comparision loop when comparison number is 1000 greater than file number
		
	end repeat
	set vfolder to ((vcompare as integer) - 1000) -- subtracts 1000 from comparison number to get correct folder number to save to
	set vtens to vfolder / 1000 as integer -- cuts off 3 zeros from folder number to prep number for comma addition
	set vactualfolder to (vtens as string) & "," & "000" --adds comma and three zeros to make folder number user friendly and compatible with our system
	set vfullpath to "Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #" & vactualfolder -- takes partial path and adds correct thousands folder to complete path
	vfullpath as alias -- makes an alias of the path, which is the correct format for paths
	set vpartialnewfoldername to "PDF Page Proofs #" -- sets up first part of folder name
	set vnewfoldername to vpartialnewfoldername & vactualfolder --creates full folder name
	vnewfoldername as string
	tell application "Finder"
		if exists folder vfullpath = false then --checks to see if folder exists
			make new folder at "Users:SHARED:_PDF Page Proofs (low res)" with properties {name:vnewfoldername} -- if no folder exists, creates new one
		end if
	end tell
	tell application "Finder"
		duplicate vtheFile to vfullpath with replacing --put file into correct folder, replacing any file that is already there. 
	end tell
end repeat

end open

thanks,

Ralph

Hello

StephanK is perfectly right.
I typed too fast and so I typed a mistake ;-((

Yvan KOENIG (from FRANCE mardi 6 mars 2007 20:34:17)

...
if not (exists folder vfullpath) then --checks to see if folder exists
	make new folder at "Users:SHARED:_PDF Page Proofs (low res)" with properties {name:vnewfoldername} -- if no folder exists, creates new one
end if
...

StefanK et al

That script does work for files where the folder exists. I still get the same error: “File Users:Shared:_PDF Page Proofs (low res):PDF Page Proofs #99,000 wasn’t found.” when I attempt to run a file through it that doesn’t correspond to an existing folder (for example: 97011_pp.pdf). I don’t get it- why does it think it’s looking for a file?

thanks,

Ralph

Hi Ralph,

you get an error in this line

vfullpath as alias -- makes an alias of the path, which is the correct format for paths *)

because the folder doesn’t exist. For coercing a string to an alias the file or folder must exist
otherwise you get an error (I used exact this error in my make new folder routine)

Edit: And the path to Shared is wrong.
AppleScript paths (separated by colons) start with the disk name,
a “shortcut” for the path string to the folder “Shared” is

(path to shared documents folder as Unicode text)

*) the correct format for a path is Unicode text :wink:

Hi,

Actually, using the error handler is faster and you might use it if you need to check for existence several times. To make the code more readable, you could place it in a subroutine.


set t to "Macintosh HD:hello:"
set r to ExistsItem(t)
--
on ExistsItem(p)
	try
		p as alias
		set f to true
	on error
		set f to false
	end try
	return f
end ExistsItem

Note that you don’t need the Finder also.

gl,

Speed was only one of the concerns I mentioned. Additionally, that is not “logically readable”, as I discussed. It’s just as improper as the other way it was written earlier in the thread.

Please keep in mind that faster in this case does not equal better. Try/error is not the “best” solution, though it may be fast.

Moving on:

Glad you pointed out that you don’t need the Finder. I didn’t even think about it (and I usually am the loudest complainer about using the Finder for this stuff).

Additional info, for the heck of it: If you’re developing an AppleScript Studio application, you can use “call method” with -[NSFileManager fileExistsAtPath:] or -[NSFileManager fileExistsAtPath:isDirectory:].

That was the problem- I understand now. I had two coercions to alias in my script. Removing the earlier one allows the script to run through to actually creating the folder.

thanks,

Ralph