The Run Handler

The run handler of a script object (or script) has been discussed in "Code and the Run Handler" in Chapter 6 and "Run Handler" in Chapter 8. The run handler is an event handler. Thus it has event handler syntax; it takes no parentheses, either in the definition or in the call.

It turns out that an explicit run handler may take parameters . This ability is useful only under special circumstances, and in fact it prevents the script object (or script) from running at all under normal circumstances, because you usually have no way to supply these parameters in the call.

The official syntax for defining a run handler with parameters is to express all parameters as a single list. For example:

script s
    on run {what, what2}
        display dialog what & space & what2
    end run
end script

You can't simply tell that script object to run, because you can't supply the parameters. If you try, you'll get an error at runtime:

run s -- error: «script s» doesn't match the parameters {what, what2} for run

One solution is to use the run script command, which permits parameters to be passed to a run handler:

run script s with parameters {"howdy", "there"}

That approach is rather extreme; run script is expensive , because it requires the creation (and destruction) of an entire separate instance of the AppleScript scripting component. A clever and inexpensive approach (which I owe to Michael Terry) is to take advantage of script object inheritance (Chapter 8) and the continue command, which can pass parameters ...

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.