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

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
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 below... | Read more »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.