Join Scott Simpson for an in-depth discussion in this video Preventing and catching errors, part of Up and Running with AppleScript.
Out in the real world, your app might face conditions that it doesn't know how to handle. For example, maybe a file that you're looking for doesn't exist. Or maybe someone left a text file in the Images folder that our watermarking app uses. Instead of failing and breaking the execution of the program, I want to make the user experience a little more polished. One way of avoiding potential errors is to make sure the data that you're sending to part of your script is what you expect it to be. In this watermarking app, we know that we only want to send images to Photoshop and ignore other file types.
Over in the Exercise files, I have an unwanted text file. I'll copy that into the Images folder, and then I'll run the script from the previous chapter. You'll see it'll fail with an error. That's because Photoshop doesn't know what to do with the text file. One easy way to avoid this kind of error is to check for the kind of file we're working with, and if it doesn't match the string image, skip it. I'll dismiss this error sheet, and then here, near the top of the addWatermark function, I'll tell Finder to set a variable called fileType to the kind of property of theFile as alias.
That will read the kind property of the file item. Then, after the tell block, I'll write, if fileType does not contain image, then, and in this case, I won't do anything, but you could also notify the user. Else, and then I'll run the rest of my code. Down at the bottom, I'll put end if to close the if block. Now if I hit Run, the program completes successfully. I can see it working in the background. Before starting this movie, I replaced these images with the versions from the exercise files without a watermark.
So now, if I look at these, I can see these all have the watermark. But I also didn't get any errors because we skipped the text file. Now of course, something that passes that test might still get through. For example, an image that's somehow corrupted. I don't really have a way to defend Photoshop against that, so I might get an error when I open up what looks like a valid image file. I've got a file here called bad_apples, which is a copy of one of my apple photos that I've intentionally damaged so Photoshop can't open it. I'll get rid of my apple pictures and I'll replace them with the unwatermarked ones from the exercise files.
So now let's take a look using the Try command, which tries out a bit of code and if it fails, it gives me the opportunity to recover from an error, instead of breaking the execution of the program, and giving up. Let's take a look at a basic example first. I'll open up a new script. And I'll start with a basic program that displays a few dialogues. And then, up here, I'll introduce an error. When I run this program, we hit the error, and the program stops. But I can use an error handler to catch that error, notify the user, and move on instead. To do that, I'll write try before the line that I know is going to cause an error.
And then I'll write, on error, following it, with the variable that'll contain the text that returns from the error message. In this case, I'll call it error text. Then I'll display a notification, with the text of the error message, and then I'll write, end try, to end the try block. Now, I'll run this, and instead of the program breaking like it did before, the try block catches the error, I'm notified of the error, and the program continues through to the end. So, let's switch over to the app and add an error handler in case there's a problem opening a file in the folder we're targeting.
Inside the else condition of the file type matching, I'll start this chunk of code with try. I'll make sure to put my end try at the end of the block. And above that, I'll write on error. I'll give it a variable to store the error message, and then I'll display a notification if an error happens. Knowing that I have a text file and my bad apples file inside the Images folder, I'll run the script, and it finishes correctly. I had a little notification that Photoshop couldn't open one of the files, but it kept on running, finishing up the watermarking operation on the files that could open.
- What is AppleScript?
- Using comments, variables, text, and numbers
- Working with files and folders
- Using conditionals and creating loops
- Building functions
- Controlling other programs with AppleScript
- Preventing and catching errors
- Distributing your AppleScript application