Developer --\> Technical Publications
PATH  Mac OS X Server Documentation > Making Your Applications Scriptable

Making Your Applications Scriptable

Previous | Chapter contents | Next | Book PDF

Concentrate Scriptable Behavior in Model Objects

Overview

The Model-View-Controller (MVC) design paradigm assigns objects in an application to one of the three indicated types, or roles. Model objects encapsulate and manipulate the data used by the application; they typically have no direct connection to the user interface. View objects know how to display and possibly edit data, but typically do not encapsulate any data that is not specific to displaying or editing. Controller objects act as mediators, coordinating the exchange of data between the model and view objects; controllers incorporate most application-specific logic and hence are the least reusable of the three types of objects. Applications that conform to MVC should maintain a distinct separation among objects of different types.

Generally, the objects that you make scriptable should be model objects. This principle is in line with how AppleScript is designed, and the Yellow Box accordingly gears its scriptability support to the model layer. The most efficient way for a script to perform a task is not the same thing as the best way for a user to do the same task. A script should not require the user's involvement, unless it is intended more as macro than as batch processing. In a macro type of script, the user must prepare things for the script, and then invoke it. If you anticipate that your application will be scripted for this purpose, you may move scripting behavior to the appropriate non-model objects in your application. Yet even in this case, ensure that the scriptability of objects such as windows and selections is confined to this purpose.

What You Must Do

You should design your application with MVC in mind and ensure, as much as possible, that the objects you want to make scriptable are the model objects of your application. There are two common violations of model-layer separation that you should guard against:

  1. Do not set scriptable state in action methods.

Action methods are typically owned by a controller object. State that should be scriptable should therefore not be directly set in action methods. For example, instead of this:

- (void)shapeSelected:(id)sender {
     /* shape is ivar */
     shape = [[sender selectedItem] representedObject];
}

Move the shape instance variable to an appropriate model object and use an accessor method to set it.

- (void)shapeSelected:(id)sender {
     NSBezierPath *newShape = [[sender selectedItem] 
representedObject];
     [modelObject setShape:newShape]; //modelObject is ivar
}
  1. Do not keep scriptable state in user-interface objects.

For example, suppose your application has an inspector panel with a checkbox in it. Instead of having a controller object "read" the state of this control, store the state in a model object each time the user toggles the state of the control.


Making Your Applications Scriptable

Previous | Chapter contents | Next | Book PDF