TweetFollow Us on Twitter

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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.