- The Bigger Picture
- Getting Started with AppleScript—the Tools You Need
- The AppleScript Language
- Writing Your First AppleScript Program
- Who Is Working with AppleScript?
- Where Do You Go from Here?
- Tip Sheet
Writing Your First AppleScript Program
The following script interfaces with the Image Capture tool. What you are doing is writing a script that will scale an image to 25 percent of its original size.
Here is the script:
set this_file to choose file with prompt "Scale your image to 25%" try tell application "Image Capture Scripting" set this_image to open this_file scale this_image by factor 0.25 save this_image in this_file close this_image end tell on error error_message beep display dialog error_message buttons {"Process Failed"} default button 1 end try
The first line of the script provides the application with a name. In this case, it is called "Scale your image to 25%".
The next line is a TRY statement, which at first attempts to execute the first block of code wrapped in the TELL statement. If the TELL statement does not execute then the error block is executed. A BEEP will sound and an error dialog message will appear.
The TELL block of code is what you are looking to execute. The first line in the TELL block identifies the application AppleScript will be communicating with. In this case, it is the "Image Capture Scripting." The following four lines then allow you to define which image you will manipulate, how to scale the image, how to save the image, and how to close the image file.
The import command is SCALE, which is an exposed property of the Image Capture utility that enables you to change the physical size of an image. There are other properties that can be manipulated.
The END TRY statement closes the block of code. You have executed this program in only 12 lines of code.
You will need to write this script in the Script Editor. The script will run after you compile it. Try it out. This is your first fully programmable AppleScript program.