December 96 - Coding Your Object Model for Advanced Scriptability
Coding Your Object Model for Advanced
Scriptability
Ron Reuter
Implementing an Apple event object model makes your application
scriptable -- that is, it enables users to control your application
with AppleScript or some other OSA-compliant language. You can
provide anything from a basic implementation of the object model
that handles only simple scripts to a full implementation that can
handle the most complex scripts. This article will help you do the
latter. It will show you how to write object accessors and
handlers that process lists of objects, alert you to some common
pitfalls, and suggest other features you can add for completeness.
You've decided to give your users an alternate interface for controlling your
application by implementing an Apple event object model. You've read Richard Clark's
article, "Apple Event Objects and You," in develop Issue 10 to get an overview of the
Apple event object model and how to support it. As you've begun to think about your
scripting vocabulary, you've absorbed Cal Simone's article, "Designing a Scripting
Implementation," in developIssue 21 and his According to Script column in Issue 24.
You've checked out the portions of Inside Macintosh: Interapplication Communication
that apply. You've read and understood the Apple Event Registry, which defines the
primary events and objects that you should support in a scriptable application, and
have paid particular attention to the Core, Text, and QuickDraw Graphics suites.
With this basic knowledge, you're ready to read this article. Here you'll learn how to
structure your code to handle more complex user scripts. After a brief review of the
components of an object model implementation, I'll focus on object accessors and show
you how to handle script statements that require your code to act on a list of objects.
Then I'll describe in detail how to deal with three big "gotchas" that are bound to trip
you up unless you know about them. Finally, I'll tell you about some other goodies you
can implement for the sake of completeness. All of this is illustrated in the sample
application Sketch, which demonstrates object model support for a subset of the
QuickDraw Graphics suite. The code for Sketch, which accompanies the article on this
issue's CD and develop's Web site, contains many functions that you can use when you
get ready to code the object model for your own application.
COMPONENTS OF AN OBJECT MODEL IMPLEMENTATION
The components of an object model implementation are outlined in the "Apple Event
Objects and You" article and discussed in great detail in Inside Macintosh:
to refresh your memory and to show how they apply in our sample program.
When a script statement asks your application to perform an action on some object,
such as closing document 1, the object specifier (document 1) must be resolved --