Automating Computer Tasks Through AppleScript

Eugen Buehler
University of Pittsburgh Medical Center


Repetition is an integral part of any research support facility. Certain tasks are repeated on a daily or weekly basis, and the efficiency of these tasks affects how well a core laboratory functions. Computers are used increasingly in all aspects of laboratory operation, including record keeping, equipment control, correspondence, and data analysis. The use of computers helps deal with repetition more efficiently but often the computers are not used at their full potential: personnel are still required to perform tasks that could be more automated. Repetition is what computers do best however, and automating common computer tasks with a scripting language such as AppleScript can increase laboratory productivity. This article describes the basic components of AppleScript and the process involved in writing a script to automate common, repetitive tasks. A rudimentary understanding of Macintosh computers and programming is all that is required to benefit from this article.

A "scripting" computer language interacts directly with applications on a computer, sending them commands and receiving information back from them. Apple has defined the way applications for the Macintosh should interact with scripts, and this standard is called the "Open Scripting Architecture" or "OSA". AppleScript is the scripting language that Apple has designed for interacting with OSA compliant applications and is included with System software version 7.5 and later, as well as being available as an addition to earlier versions of System software. While there are other, occasionally more powerful, scripting languages that can be used with OSA, these languages share the common disadvantage of being far less intuitive than AppleScript.

OSA and AppleScript are intuitive in part because of their object-oriented design. Each application defines objects that are appropriate to its purpose. In the case of a word processing program, the objects might be words, paragraphs, characters, and documents. In a sequence assembly program, the objects might be sequences, contigs, and individual bases. Objects have properties that can be referenced by a script. The properties of a sequence fragment might be its length, how it was obtained, and when it was last edited. In addition to having these properties, objects can also contain other objects. In our sequence assembly example, the contig object could contain sequence fragment objects, and those fragment objects could contain base objects. Of course, all these objects must be defined by the application programmers. An application that conforms to OSA standards for defining objects and commands is usually referred to as a "scriptable" application.

Let's take an example task to be automated by a script. Every morning when I arrive, the Sequence Analysis software for my ABI 377 has dutifully tracked the gel lanes and extracted the sample files for the gel. The only problem is about half the time it mislabels all the lanes, meaning that I have to throw away the sample files and re-analyze the gel. So I open the last run folder, select between 36 and 72 samples files and their associated .seq filesbeing careful not to select the gel, log, or run filesand then drag them to the trash. As I choose empty trash from the menu, I think to myself, "Wouldn't it be nice if you could do this with one command?" You can.

The first step to writing our script is opening Script Editor, a program that should come with your Macintosh system software. Choosing "Open Dictionary" from the File menu, we next select the application whose scripting dictionary we wish to examine. In this case, we wish to script "Finder", the pseudo -application that is always running as a part of the Macintosh operating system. The Finder allows us to do things like manipulate folders and throw away files. To read the Finder dictionary, we actually have to open the "Scriptable Finder Extension" in the extensions folder of the system folder. This is the exception to the rule, however, as with all other applications you will simply select the application itself to view its scripting dictionary.

Upon selecting the dictionary to open, we are presented with a list of commands (in plain text) and objects (italicized). Selecting a command shows the grammar that should be used with that command and what the command does. In this case, the actions we want to perform are "delete", to delete files by moving them to the Trash, and "empty", to empty the Trash when we are done. Selecting these commands, we see that "delete" should be followed by the object(s) to be deleted, and "empty" can be followed by "trash" but will perform the function by itself regardless. Now we examine the objects we want to manipulate, in this case "folder" and "file". Selecting "folder", we see a list of elements, which are objects that a folder can contain. Selecting "file" (from the Finder Suite), we see a list of properties that all files have. We will use two of these properties, "creator type" and "file type", to distinguish sample files from the other files in our run folder.

Another way we could have learned about the commands and objects we want to script is by using the Record button in Script Editor. When the Script Editor's Record button is pressed, it begins recording the user's actions in the AppleScript language. We could, for example, press Record, drag a file to the trash, empty the trash, and then press Script Editor's Stop button. The result should be a script that looks like the following:

         tell application "Finder"
               activate
               select file "Lil' Oil Slick" of startup disk
               delete selection
               empty trash
          end tell

When run, this script should have the exact same effect as the user's actions. While it is unlikely that we would want a script to throw away just one file with a particular name, this recording demonstrates two commands ("delete" and "empty") that we will use in our final script. Recording can often be used in this manner to aid in writing a script, but at this time there are many applications that do not support script recording.

Now that we know the commands we will be sending the Finder, we can begin writing the script (Figure 1 shows the entire script as viewed from Script Editor). (16k)

Figure 1: The entire example script, Trash Analysis v1.0, as viewed from script editor.

First, our script must determine which run folder is to have its analysis files trashed. There are a number of ways we could do this, but the easiest is to prompt the user to choose a folder. AppleScript has a special command for this named, aptly enough, "choose folder". This command displays a standard Macintosh dialog for choosing a folder and then returns the pathname of that folder. Thus the command:

        set theFolder to (choose folder)

would set the variable theFolder equal to the folder selected by the user.

The next step of writing our script is to define the application that will interact with the script. We do this by enclosing commands in a "tell" statement. In this case, it would look like this:

       tell application "Finder"
          list of commands to send to the Finder
       end tell

Now all we have to do is write statements, using commands and objects defined by the "Finder", that will delete only the analysis files in the chosen run folder.

         delete (every item of theFolder whose file type is "ABI1") 
         delete (every item of theFolder whose creator type is "ttxt")

These two statements are examples of "filtered references". A filtered reference takes the form "every (something) whose (test statement)". The reference then refers only to those objects for which the test statement is true, in the first case, whose file type is "ABI1". Now that these files have been deleted (moved to the trash), the remaining step is to empty the trash. Our final script then reads:

          set theFolder to (choose folder)
          tell application "Finder"
          delete (every item of theFolder whose file type is "ABI1")
          delete (every item of theFolder whose creator type is "ttxt")
               empty trash
          end tell

Of course, there are more complicated examples of AppleScript programming than the one above. Scripts can make use of most of the methods available in standard programming languages, such as local and global variables, subroutines, control structures like "if...then" statements, and event handlers. Still, most scripts bare a striking resemblance to spoken English, making writing and understanding scripts significantly simpler then with other programming languages.

Once a script has been written, it can be executed in a number of different ways. Scripts can be activated by double-clicking, just as with an ordinary application. Scripts can be made to perform an action when a file or folder is dropped onto them. They can even be made to stay open indefinitely, performing a given action periodically. Scripts function essentially as very small applications. The script's role as miniature application can be extended by using a graphic interface generator such as FaceSpan or HyperCard. These programs allow you to add menus, modal dialogs, and other graphic elements to your scripts that can make them virtually indistinguishable from an application written in C or Pascal.

Already, most major software packages are written with extensive scripting dictionaries, including Microsoft Word, Microsoft Excel, and FileMaker Pro. As the use of scripting becomes more popular, even small developers, including those that cater to scientists, will design their applications to be OSA compliant. ABI has announced that version 3.0 of Sequence Analysis will, at last, be scriptable.

In our laboratory, many daily functions are accomplished by the use of scripts. Sequence data files are sorted by the principal investigator's name and placed in E-mail messages automatically. Oligonucleotide order forms (submitted through the Internet) are converted to the ABI OligoNet file format for synthesis. A script is used for determining sequencing project statistics, such as average coverage and average sequence length, within Sequencher, the first scriptable sequence assembly program for the Macintosh. In each case, a small investment in programming time has yielded a great return in time saved on daily tasks. It is also worth noting that because the applications and tasks these scripts address are common to most core facilities, the opportunity for sharing resources among the community is great. The scripts mentioned in this article are available at my homepage at the URL

http://www.lm.com/~snafu

One of the easiest ways to learn about scripting is to take an existing script and make small modifications to it to suit your particular task. If you make an improvement on one of my scripts or write scripts of your own that would be of use to the general community, you can send them to me and I will add them to my homepage.

References

Inside Macintosh: Interapplication Communication . Cupertino: Apple Computer, Inc., 1993.

Goodman, Danny. Danny Goodman's AppleScript Handbook , 2nd ed. New York: Rando House Electronic Publishing, 1994.

The author may be contacted at the University of Pittsburgh, 300 Technology Dr., Suite 241, Pittsburgh, PA 15219.


Return to the The ABRF Home Page


Created: 1st June 1996
Last modified: 1st June 1996