image

Testing Handlers

image

Script Debugger lets you run individual handlers in a script. This can be a valuable testing and debugging technique.

Standard Event Handlers

You can run a standard event handler, such as an idle handler, an open handler, or a Folder Action handler. To do so, choose from the Event Handler menu:

When you’re debugging a script, the same pop-up menu is attached to the Trace, Step Over, and Step Out menu items and toolbar buttons.

For example, consider the applet script in the illustration below. If you simply run this script, you’ll see the “Howdy!” dialog. That’s because what you’ve just run is the run handler. But what if you want to test the idle handler? Choose Idle from the Event Handler menu.

image

A continue statement in an event handler called in this way will generate an error in Script Debugger. This is deliberate and may be safely ignored. The technical reason is that if, for example, we permitted a continue quit statement in a script’s quit handler to execute, Script Debugger itself would quit!

The Current Event

When you choose from the Event Handler menu, that event becomes the script’s current event. This means that the event is now called by default when you run the script.

To alert you to this special situation:

image

To set the script’s current event back to the run handler:

To set a different current event without also running that event handler:

Parameters and the Event History

The open handler and the event handlers defined in the Folder Actions and Digital Hub Actions suites expect parameters, which are aliases or files.

For example, an open handler expects a list of aliases to the files and folders. An adding folder items to handler expects two parameters, an alias to the watched folder and a list of aliases to the added files. And so forth.

Such a handler is listed in the Event Handler menu with an ellipsis () after its name. When you choose it from the Event Handler menu, Script Debugger presents a dialog where you can select files and folders. An appropriate parameter or parameters will then be passed to the specified handler.

In the case of adding folder items to and removing folder items from, what you’re supplying in this dialog is the second parameter, the item(s) that are allegedly being added or removed. Script Debugger will then use the containing folder of the item(s) as the first parameter, the watched folder.

In the case of just the open handler, there is another alternative. Drag-and-drop files and folders directly from the Finder into your script. If your script has an open handler, one of the options in the resulting dialog is to invoke the open handler with these Finder items as parameter.

Script Debugger remembers each alias or list of aliases produced in this way, along with the event handler it is to be passed to, and adds this information to the bottom of the Event Handler menu. This list of remembered event handlers and parameters is the event history.

Thus, the next time you want to test this same handler with these same parameters, you can choose it from the event history (or, if it is the current event, click the Execute button).

image

The event history is remembered until you close the script window; or you can deliberately remove it by choosing Clear Menu (which is always the last item in the Event Handler menu if there is an event history).

Other Handlers

If the handler you want to test is not one of the standard event handlers already listed in the Event Handler menu, you can test it using Script Debugger’s scriptability, which allows one script to call a handler in another script.

For example, suppose you have opened a script file named MyCoolScript.scpt containing this top-level handler:

    on justName(s)
        set text item delimiters to ":"
        return last text item of s
    end justName

To test this handler without running the rest of MyCoolScript, run this script (from a different tab or script window):

    tell document "MyCoolScript.scpt"
        justName("hey:ho:nonny nonny no")
    end tell

This calls justName in the complex script. Moreover, it adds this handler call with this parameter to the event history. Thus, having done this once, you no longer need the second script in order to test the first.

image