Is it better to use “on error” or “exists” commands. Lets say I want to create a folder on the desktop.
-
I can tell “Finder” to check whether the folder exists. If it doesn’t exist, then create a new folder.
-
I can set the path to the folder as an alias. (A file/folder must exist to be referenced by an alias) If the folder does not exist, then an error is generated. If the error is triggered, then I tell “Finder” to create a new folder.
Here are applescript examples.
Which is of the following code is more efficient.
Using the exists command
property folder_name : "the_folder"
set the_folder to (path to desktop folder as string) & folder_name as string
tell application "Finder"
exists the_folder
if the result is false then
make new folder at desktop with properties {name:folder_name}
end if
end tell
Using the error trigger
property folder_name : "the_folder"
try
set the_folder to (path to desktop folder as string) & folder_name as alias
on error error_message number error_number
tell application "Finder"
make new folder at desktop with properties {name:folder_name}
end tell
end try
In my experience, the “error” trigger is slightly faster. What are your thoughts and experiences?
Model: MacBook Pro
AppleScript: 2.3 (118)
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)