AppleScript programming – Introducing AppleScript

AppleScript is for Macintosh automation; so scripts you write can automate tasks you would otherwise have to perform with the mouse and keyboard.

So what would you want to automate? The answer to this question falls under two broad categories: small tasks and big tasks.

The main difference between small scripts and big scripts is the amount of though and planning involved.

  • When writing small scripts, you start out by … writing the script. As the scope progresses, you add more functions, you add more lines, and you go back to fix and debug until the script works.
  • Big scripts require planning. You have to carefully consider the users, the environment, and the different triggers for tasks the system will perform. You have to deal with where the data for the script comes from, small graphical user interfaces, and return on investment for your script or script project — all the issues that can kill your efforts just as you start to get excited.

Automating Small Tasks

Small scripts are the way one usually starts to become familiar with AppleScript. AppleScript lest you easily create a script, test it, save it as an application, and start using it. This quick turnaround for truly useful utilities is part of the winning recipe that makes AppleScript so great.

tell applicatio "QuarkXPress 4.01"
tell every page of document 1
set width of (every line box whose width < 0.25) to 0.25
end tell
end tell

Automating Big Tasks

Although AppleScript doesn’t sweat the small stuff, it can be the base for large-scale custom integrated system. As far as what to automate, you could automate any task that has repeatable logic, and if the cost of automation is less than the amount of money you save — and it usually is — the task is worth automating. Repeatable logic is logic that can be applied successfully to similar, but not identical, subjects and that produces a predictable outcome, based on the uniqueness of the subject.

For example, here is a process that is not so suitable for automation:

Here is a better candidate for automation:

When you get a job from a client, you search for the client in the database to locate the job information. If the job is marked “urgent,” you e-mail the imaging room to expect it within ten minutes. You open the Adobe InDesign document and verify that all the fonts were provided. You make sure the document contains the number of pages indicated in the electronic job ticket; if it doesn’t, you make a note in the database and e-mail the client. When done, you make a copy of the “job” folder in the “archive” folder on the server, and you send another copy to the “in” folder on the production server. You repeat these steps for all jobs that arrived last night.

Now this is a process for which AppleScript is perfect. Besides having programmatic elements such as a repeat loop for applying the process to all jobs and some branching, the process itself s clear, is logical, and can be applied to any client job that arrives.

If you look more closely at the previous process, you can see that quite a few of the steps happen in software applications. The operator has to know ho to use FileMaker Pro for searching and entering data; how to use InDesign for checking fonts, pages, and so on; and how to use the Finder or moving folders. Controlling these and many other applications is one of AppleScript’s main strengths. AppleScript can give applications commands and get information from applications, such as retrieving the data in a FileMaker Pro record or the number of pages in an InDesign document.

AppleScript is also ideal for performing repetitive tasks. For instance, how would you like to show up at work one morning and realize that your job for the day is go through 200 TIFF images in a folder, add a black frame to each one, and export each as JPEG? Then, you must create a PDF file containing all the images, with four-up (which means having four impressions of similar size on a single printed sheet) and with the image name under each image. Does this sound like hard work? Your reward for a job Weill done just might be another folder with 200 TIFF images the next day …

Although not sophisticated, this process is repetitive. A script can do it for you while you take a walk to the cafeteria.

Read More »