I was wondering if someone could tell me how to write a very simple if-then statement? I want to check for the existance of a folder on the desktop and if it does exist then I want the script to continue, if not I want it to display a dialog that says something to the effect that it does not exist. expl: “Sorry, the backup folder does not exist. This script will now stop.”
Specifically, the folder is called “backups” if it exists I want the script to continue to execute the rest of its commands.
tell application "Finder"
if folder "backups" of desktop exists then
--Put your script here
else
display dialog "Sorry, the backup folder does not exist. This script will now stop." butons {"OK} default button 1 with icon 0
end if
end tell
that does the trick. the buton part gave me an error but it is more important to me that the script works so i dropped that part and it works fine. of course the dialog box that pops up gives me a choice of cancel and ok and both stop the script but like i said, i just need it to work.
If you don’t want to have to ask the Finder (which can be slow), you could use the following handler:
if testPathExists((path to desktop as string) & "backups") then
-- DO STUFF
else
-- show the dialog
end if
on testPathExists(inputPath)
-- version 1.4
-- from Richard Morton, on applescript-users mailing list
-- gets somewhat slower as nested-depth level goes over 10 nested folders
if inputPath is not equal to "" then try
get alias inputPath as string
return true
end try
return false
end testPathExists