if file exists...

How would i go about making a script that says

if file x exists then
–do this
else
–do this

tips?
thanks.

Monkey:

There are at least two ways that I know of. Either way, you have to have the entire path as your [x] variable. Here is one way, that I learned in this forum:

set main_path to path to desktop
set wf to "Reading"
set Write_path to main_path & wf as Unicode text
try
	alias Write_path
on error
	my FirstRun(Write_path)
end try

The first three lines build the path to a file named “Reading” on the desktop. By putting that into an alias inside of a try, it will return an error if the file does not exist. When that error is reached, a handler (FirstRun) is called, the variable is sent there, and (in my script, at least) a file is created with the appropriate data.

Here is another method, albeit somewhat crude:

set main_path to path to desktop
set wf to "Reading"
set Write_path to main_path & wf as Unicode text
tell application "Finder"
	if file Write_path exists then
		--Do something
	end if
end tell

Either will work to see if a file exists.

Craig Smith

you can also do

try
	do shell script "ls -a ~/desktop/test" --change this to the path to the file
	return true
on error
	return false
end try