AppleScript Dynamic Debugging
Volume Number: 13 (1997)
Issue Number: 8
Column Tag: Scripting
Dynamic Debugging of AppleScript
by Cal Simone, Main Event
Special debugging techniques for a special language
Talking About AppleScript
AppleScript is the scripting language of the MacOS. Much of AppleScript's syntax resembles that of a human language -- in many cases, you can write grammatically correct, imperative sentences to control your computer. But the experience of debugging AppleScript scripts can differ significantly from that of debugging a regular application.
In this article, I'll discuss:
- Debugging programs in traditional programming languages.
- What's different about debugging AppleScript.
- What comprises dynamic debugging
- How to debug an AppleScript script using these techniques.
- Special topics, such as debugging handlers, messages that originate from outside a script application, and AppleScript CGIs.
- Some extra AppleScript debugging goodies.
Lather, Rinse, and Repeat (The Traditional Debugging Cycle)
In most traditional programming languages the debugging process goes something like this:
- Type, copy, and paste in an editor to create and modify your source code.
- Compile your program into executable 68000 or PowerPC machine code.
- Run your program.
- Discover a bug and figure out the problem.
- Repeat this cycle until there are no more bugs.
For traditional programming languages, this edit-compile-run-debug cycle is normally fine. When you start your C program over each time to run it, you'll usually start over executing your program with a clean slate -- everything's gone when you start anew.
Debugging in AppleScript
AppleScript differs from most traditional programming languages in a number of ways:
- AppleScript has a natural-language syntax. It often consists of human-readable constructs such as tell the application "WordPerfect" to print the second window. Because of this human-readability factor, AppleScript's audience includes more than hard-core software developers. Programmers possess the abilities to think logically, to pay very close attention to detail, and to work adeptly with abstract symbolic notation. But a much wider variety of experience and technical skills are found among scripters -- in fact, many people who write in AppleScript can't or don't program in regular programming languages; a great many don't even think of themselves as programmers. While they can think logically and do well with detail, they aren't comfortable translating more cryptic symbolic notation into a set of instructions. So, in addition to software developers, among scripters we find power users, system administrators, and the guy or gal who helps others in their department set up scripts to make their work a little easier. (Even the nomenclature is different in the AppleScript world -- we see terms such as "command", "handler", and "message", instead of words like "statement", "function", and "code".)
- AppleScript does interapplication communication. A great deal of AppleScript's power is drawn by leveraging other existing applications, known in the AppleScript world as target applications. A single line of AppleScript can send an interapplication message (an Apple event) to a target application that may cause a thousand lines of C code to be executed in that application. What's different in a scripting scenario is that, when you encounter a bug, fix it, and run the script again from the beginning, you're not just starting the script over -- one or more target applications may be part way through a scenario. For example, the script may cause target applications to create or delete documents, files, or database records, and data (and visual representation of data) may be rearranged or transformed. Further, some of these applications may be on other computers. Each time you start your script from the beginning, these target applications must be reset to their respective starting points. If you're ever going to get done debugging, starting the script over each time isn't really an acceptable solution.
- AppleScript is extensible. Because any application or scripting addition can extend the AppleScript language, the vocabulary and the meaning of that vocabulary can be different, depending on which application you're communicating with or controlling. So, while debugging, you must be able to probe the target applications as well as the script.
The Dynamic Debugging Experience
To me, the essence of dynamic debugging encompasses the ability to affect the scenario in which a program or script runs, while executing step by step, and the ability to affect the program or script's state when a bug is encountered, without the necessity of starting over. Some dynamic languages (and occasionally interpreted languages) do have dynamic debugging mechanisms. SmallTalk, LISP, and Dylan are examples of such dynamic languages.
Changing state
Nearly all development environments for compiled languages, whether dynamic or not, have facilities for watching the values of variables change as the program executes. Some also allow modification of data values in the middle of debugging, thereby affecting the program's state. To do proper debugging in AppleScript, you must be able to modify a script's variables.
Affecting the scenario
Some dynamic language environments provide the ability to execute individual statements on the fly while debugging a program, as if the statements were typed inline. Because of AppleScript's interapplication communication capabilities, there'll be times when you'll have to modify the scenario outside the script, including target applications that the script is communicating with or controlling.
Furthermore, depending on how you're using AppleScript, you may also have to handle messages that originate from sources outside the script. This is particularly necessary if a script is going to be used as a standalone application, such as an AppleScript CGI application for your Web server.
Because of the rich set of tools available, many of which were developed specifically for use with AppleScript, I'll be using Scripter to demonstrate the debugging techniques described in this article. (Information about Scripter is available from http://www.mainevent.com/.) When Scripter was designed, the goal was to make the experience of debugging reflect the way AppleScript works. Also, the process of debugging scripts had to be made easily accessible to people who have never used a debugger before, and yet provide the serious scripter with advanced tools for perfecting larger production systems based on AppleScript.
Tackling the beast
In the Scripter environment, while stopped at a particular command in the script, you can fiddle with your script's variables, evaluate expressions, and execute commands.
In the Observe window, you can watch the values of variables and expressions as your script executes each step. More importantly, you can define or modify the value of any variable while stopped at a step. Note that the values you see or change are applicable to the scope of the handler currently being executed or the top level. (A handler is a routine that is either called like a regular subroutine or executed upon receiving an Apple event message from another application. The top level consists of commands that aren't in any handler and executes when you run the script.)
While stopped at a step, you can type, paste, or drag any AppleScript commands into the Instant window and execute them as though you had typed them inline. Those commands can use and modify variables as well as execute handlers in the script. This means you can often repair problems when you encounter them. Sometimes, by fixing things as you go, you can reach the bottom of the script in a single debugging session, having found all your problems. As in the Observe window, commands executing in the Instant window apply to the scope of the current handler or top level.
When you're done debugging, you have to go back and fix up your script's source code. I don't know about you, but I usually figure I've only got one or two problems to fix -- they'll take only 10 minutes -- and the script will be perfect, right? A few hours later, having found all the bugs, you emerge bleary-eyed, but triumphant, at the bottom of your script, something that might have taken you two or three days if you had to modify the source code and start over each time! However, now you've got to fix up your script, which means you have to remember what you did at the beginning of your debugging session. And naturally you always write everything down -- NOT!
To help you figure out what you need to modify, there's a Log window that shows you not only the commands you stepped through (and the results generated by each of those commands), but also your errors and the fixes you applied. So what you end up with is a guide to fixing up your source code after the debugging session.
Let's Do One (A Debugging Session)
Consider the following simple, yet problematic script:
global a
to BumpAndSquare(x)
set temp to x + 1
set temp to temp * temp
return temp
end BumpAndSquare
set a to 1
set b to c
BumpAndSquare(b)
set b to the result
tell the application "Finder"
close the front window
end tell
Before you start, close all the windows in the Finder (or you can use another application that supports the object model). You can watch the values of a, b, and c in the Observe window. As you step through the script (using the Step or Auto Step menu command in the Script menu), first a is set to 1, then you encounter an error on the second line, because you can't use c before it's been given an value. Figure 1 shows the Observe window when this error is encountered.
Figure 1. Watching variables.
You can double-click the "undefined" value for c and enter a number (for example, 6) to give c a new value. Now the second line will work, and you can continue stepping through your script without having to start over.
Incidentally, if you step into a handler (by holding down the Shift key while stepping), then you can modify the values of variables that the handler has access to. In the example, while stepping inside the BumpAndSquare handler, you can see and modify the values of a, x, and temp, but not b or c. Clicking on the Observe window's twist-down triangle will show which hander you're currently in.
Later in the script you'll encounter another error because the target application has no windows to close. In the Instant window, you can tell the Finder to open the startup disk as shown in Figure 2. (If you're targeting a different application, tell it to make a new window or document.) Again, the line with the error will now work, and you can continue stepping through your script.
Figure 2. Executing a command "on-the-fly."
Next, if you open the Log window, and start your debugging session again, you'll see what you've executed, and when you apply the fixes, they are also shown. (See Figure 3.) Now you can use the log as a road map for adjusting your source code to fix the bugs you found and to handle problems you might encounter, such as finding out if the front window exists before attempting to close it.
Figure 3. A debugging session Log.
Experimentation
Along the way, the Command window offers a number of interesting possibilities for exploration. If you want to try out a series of single commands to see the cumulative effect, you can type a line of AppleScript in the Command window and execute it, then type another line and execute that, and see the effect of each new line on what came before it This experimentation can be carried on without affecting the scripts in other editing windows.
You can also associate the Command window with a particular application and have a "conversation" using AppleScript commands, to get information about that application's properties and objects, or to try out various possibilities to help you figure out the right way to express a particular action.
Debugging Handlers
In AppleScript any handler in a script can be called from within the script or from another application. Trying to debug a handler can be tricky. You could comment out the handler declaration (the to or on lines) and the end line, then type set commands to simulate the parameter passing, and step through the resulting script as if commands in the handler were top-level commands. Or you could type up a fake call and step into it from the top level of the script (as in the example above).
Both of these methods have drawbacks. First, having to change and reverse what you do each time you save the script is a pain. Also, you are breaking a cardinal rule of programming -- debugging a version of your script that's different from the final one you're going to use. Not only might things behave differently between your original and modified versions, but worse, if you forget, and save the script with your modifications, then you'll have extra top-level commands that you definitely don't want.
An integral part of the dynamic debugging scenario in Scripter is the Call Box. You can start a debugging session for a particular handler by typing and initiating a call. In the previous example, you'd type a call like BumpAndSquare(7 * 6). Upon calling the handler (see Figure 4), the step indicator in the script window points to the first line and you can step normally through the call. The Observe, Instant, and Log windows are available to you while debugging. When the call returns, the result will be appear in the Call Box, so you can see what your handler would return to the caller.
Figure 4. Calling a handler.
Calls from outside the script
Even more tricky is debugging Apple event messages sent to your script from another application. After clicking two check boxes (one in the Call Box and one in the script window), You are now set up to debug messages received by your script. To stimulate this, you can send an Apple event message to Scripter from Apple's Script Editor (or any application that could send Apple event messages), by executing the following script:
tell the application "Scripter"
set x to 7
set y to 6
BumpAndSquare(x * y)
end tell
The message BumpAndSquare(42), complete with the parameter value 7 * 6 evaluated to 42, appears in the Call box. Then you can call the handler using this message, step through the handler, and the result will be returned back to the sender (in this case Apple's Script Editor, or whatever application sent the message) -- true interapplication message debugging!
If your script is going to be used as a standalone application, such as a CGI application, you can use a technique called "live editing" to arrange for the actual messages normally sent to that application to be sent to Scripter's Call Box. Real CGI data values from your Web server can appear in the Call Box. (See Figure 5.) So you can debug not only the parts of your script that handle messages, but the messages themselves.
Figure 5. A message sent to a CGI, captured in the Call Box.
Reentrancy
In AppleScript, if a handler is executing and another message arrives, the first message is interrupted and the second message takes over, initiating a second handler call. If a third message arrives, the second message is interrupted, and so on. As each handler finishes, the next most recent message continues from where it was interrupted. You can simulate this behavior either by manually making calls in the Call Box, or by sending Apple events from another application.
Extra Bonuses
Persistence
AppleScript scripts have special global variables (called properties), which can be initialized at compile time, and whose values persist between executions in applets (AppleScript applications). When an applet quits, any values for the properties that were changed while the applet was running are saved to disk. The next time the applet is launched, the properties are given the last saved values. When you're debugging, you have a choice between allowing property values to persist between executions or debugging sessions (to help simulate the script running in an applet), or automatically clearing them out each time you start.
Error handling
AppleScript has a mechanism for catching and handling runtime errors, known as a try block. Any errors encountered in the try part of the block will drop the script into the error handling part of the block. This allows you to gracefully handle errors encountered in your script.
However, AppleScript doesn't differentiate between a genuine runtime error and a bug in your script -- when you're debugging a script, you don't want bugs to be caught and handled by the error handler.
The example below shows what is a real runtime error, depending on the value of y. Normally you would want this error to be caught by the error handler:
try
set x to 5 / y
on error
set x to 0
end
On the other hand, this version is a bug in your script which you don't want to go into the error handler:
try
set x to y / 0
on error
set x to 0
end
You can turn off the error catching mechanism, step through the script, and if you encounter a bug, the script will stop as though the error handlers are not present. If you discover that the bug is really a genuine runtime error, then you can turn the error handling mechanism back on, and step into the error handler.
Recursion
In recursion, a routine is called from inside itself. Recursion is used for simple factorial computations or to traverse through nested folders on a disk (which requires starting through a sub-folder while you're in the middle of going through a folder). It's interesting to watch the recursion build up and unwind.
Here's a standard factorial routine that calls itself, modified slightly so you can watch and play with the value of x:
on factorial(x)
if x > 0 then
factorial(x - 1)
return x * result
else
return 1
end if
end factorial
factorial(10)
You can watch the results returned as each level of recursion unwinds. In the Observe window, you can see what level of recursion you're in, and modify the value of x for the current level of recursion.
It's Addictive
These dynamic debugging techniques are instructive and fun. It'll take you less time to find your errors and perfect your scripts. And you'll find and fix bugs that you wouldn't have known you had, so you'll end up with higher quality scripts. Whether you're making scripts for your own desktop use, or providing scripts for those around you, you'll have a much higher degree of satisfaction and you'll accomplish a lot more with AppleScript.
Cal Simone, cal@script.org, President of Main Event Software http://www.mainevent.com, and founder of the AppleScript Language Association http://www.script.org, defines the essence of user-authorability. He is relentless in his efforts to make AppleScript use as widespread as possible.