< Previous PageNext Page > Hide TOC

Preparing a Scripting Definition File

This chapter describes the steps you take to create a new scripting definition file and add scriptability information for your Cocoa application.

Important: You can read about additional refinements to sdef usage in Cocoa applications for Mac OS X v10.5 in the Scripting section of Foundation Release Notes.

If your scriptable application will run in versions of the Mac OS prior to version 10.4, it must supply scriptability information in script suite and script terminology files. For more information, see “Converting and Updating Scriptability Information” and “Script Suite and Script Terminology Files.”

This chapter uses examples from the sdef file Sketch.sdef from the Sketch sample application, available from Apple.

For definitions of terms such as property and element used throughout this chapter, see the “Glossary.”

In this section:

Structure of a Scripting Definition File
Create a Scripting Definition File
Add Information to the Scripting Definition File


Structure of a Scripting Definition File

A scripting definition file provides a statically stored representation of a set of scriptability terms and the classes, constants, and other information used to support an application's scriptability. Here is another look at the organization of the main XML structures used in the sdef format. All valid sdef files follow this structure, differing primarily in the contents of the application-specific suite elements.


Figure 4-1  Structure of an sdef file, revisited

Structure of an sdef file

Note: The sdef information in this chapter is provided to demonstrate the general construction of sdefs for scriptable Cocoa applications. It is not meant to be an exhaustive description of the sdef format.

Code Constants Used in Scriptability Information

Four-character codes (or Apple event codes, or simply codes), are used throughout AppleScript to identify various kinds of information. A four-character code is just four bytes of data that can be expressed as a string of four characters in the Mac OS Roman encoding. For example, 'docu' is the code that specifies the document class. The codes defined by Apple and the header files in which they are defined are described in “Apple Event Constants” in Building an Apple Event in Apple Events Programming Guide. They are also documented in Apple Event Manager Reference and AppleScript Terminology and Apple Event Codes Reference.

You use codes in your sdef file to identify scriptable commands, classes, properties, elements, and other items your application supports. When choosing a code, use an existing code from one of the Apple headers for a standard object—for example, use 'capp' for an application object. If you need to refer to the value in your code, you can use the corresponding Apple constant cApplication, but in the sdef you simply use the string literal ('capp'). For new codes you define, you can define a corresponding constant, if necessary.

Important: If you use a term (or a code) that is defined in AppleScript's built-in terminology or in the Standard suite, you must match it with the code (or the term) defined for it.

Apple reserves all values that consist entirely of lower-case letters and spaces. You can generally avoid conflicts with Apple-defined codes by including at least one upper-case letter when choosing your own codes.

You should always use a given code with the same term, and vice versa. While it is possible to use a term-code pair for different items (for example, to have a property with the same name as a class), this won't work for all elements, and you may want to avoid such reuse. Your codes do not have to be unique with respect to those used in other applications; in fact, as noted above, it is recommended that you use Apple-defined values for standard items.

Features Common to Many Scripting Definition File Elements

Except for the dictionary element (described below), most XML elements in an sdef have a name, a code (or pair of codes), and a description. They may also include documentation and implementation information. XML elements with the cocoa tag (described in “Cocoa Elements”) contain a particular kind of implementation information for Cocoa scripting. Some elements can be marked as hidden, meaning they won’t be displayed when the dictionary is viewed in Script Editor or Xcode, although they will be implemented. Elements can also include synonyms, which define an alternate term or code for the main element.

An XML element in an sdef may refer to any other element in the sdef, regardless of whether they are defined in the same suite element. For example, the word class defined in the Text suite can be referred to in any other XML elements in any other suites in the same sdef file.

High-level XML Elements

All sdef files used to supply Cocoa scriptability information contain the high-level XML elements described in the following sections.

XML Version and Document Type Definition

The first two lines of an sdef file, shown in the following listing, specify the XML version and DTD (document type definition) file for the sdef format. You don't change these lines.

Listing 4-1  Version and document type in an sdef file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

Dictionary Element

The root XML element in an sdef file is a dictionary element. A dictionary defines all the scriptability information for one application. Every sdef file you create should contain one dictionary definition. You typically name the dictionary after the application.

A dictionary element contains one or more suite elements. Most Cocoa applications include Cocoa’s default suites (the Standard and Text suites), as well as one or more suites of their own scriptability information.

The following listing shows the format for a dictionary element.

Listing 4-2  A dictionary element from an sdef file

<dictionary title="Sketch Terminology">
    <!--
        Contains one or more suite XML elements.
    -->
</dictionary>

Suite Elements

A suite is a collection of related scriptability information. For example, Cocoa's Standard suite contains default commands, enumerations, and other information, along with Cocoa scriptability information used to implement support for the standard AppleScript commands. Cocoa also implements a Text suite.

Your application supplies its own scriptability information in one or more suite XML elements. For example, if your application provides scripting support for graphics-related features and database-related features, you can provide separate graphics and database suites.

For most applications, the sdef should also include a suite element for the Standard suite and possibly the Text suite as well. You can copy these suites from Sketch's sdef file, then delete the XML definitions for commands, classes, and other items not supported by your application so they won't be displayed in your dictionary. For example, if your application does not support printing, you should delete the elements for the print command and the print settings record-type. Otherwise, users will see those terms when they view your application terminology and will expect the application to support them.

Important: You can read about additional refinements to sdef usage in Cocoa applications for Mac OS X v10.5 in the Scripting section of Foundation Release Notes. For example, in Mac OS X v10.5, an sdef for Cocoa’s Standard suite is provided in the file CocoaStandard.sdef in the location /System/Library/ScriptingDefinitions, and you can use an xinclude statement to include that file into your application’s sdef file.

A suite element contains one or more of the following elements: class, command, enumeration, record-type, and value-type. These elements are described in “Add Information to the Scripting Definition File.”

The following listing (from Sketch's sdef file) shows the XML format for a suite element.

Listing 4-3  A suite element for the Sketch suite

<suite name="Sketch Suite" code="sktc"
        description="Sketch specific classes.">
    <!--
        Contains one or more of the following sdef elements:
            class, command, enumeration, record-type, and value-type
    -->
</suite>

Create a Scripting Definition File

One option for creating an sdef file is to simply take an existing sdef, such as the one distributed with the Sketch application, and modify it for your application. You can do that with the following steps:

Important: As noted previously, starting in Mac OS X v10.5, the file CocoaStandard.sdef in the location /System/Library/ScriptingDefinitions provides an sdef for Cocoa’s Standard suite, and you can use an xinclude statement to include it into your application’s sdef file. For more information, see the Scripting section of Foundation Release Notes.

  1. Copy the file Sketch.sdef and rename it for your application (for example, MyApplication.sdef).

  2. Edit the sdef file in a text editor (in Xcode, select the file and choose File > Open As > Plain Text File to open the sdef as plain text). Rename the Sketch dictionary element, so it looks something like the following:

    <dictionary title="MyApplication Terminology">
  3. Rename the Sketch suite element and change its code, so that it looks something like the following:

    <suite name="MyApplication Suite" code="MyAp"
        description="MyApplication information.">
  4. Replace the scriptability information in the renamed suite element with the information for your application, as described in “Add Information to the Scripting Definition File.”

Creating an sdef file from scratch is quite similar, and also includes copying information for the Standard and Text suites. To create a new sdef file:

  1. Create a plain text file with the extension .sdef (for example, MyApplication.sdef).

  2. Add XML version and document type information. (All sdef files start with this information.)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
  3. Your sdef must contain a dictionary element as the root element. The dictionary is typically named after your application, as shown in previous steps. The complete dictionary element from Sketch is shown in “Dictionary Element.”

  4. If your application supports any of the commands, classes, or other scriptability information defined in the Standard and Text suites, as most do, you should copy the suite elements for those suites into your sdef from an existing sdef file, such as Sketch.sdef.

    Important: You should delete any information in the default Standard and Text suite elements for features your application does not support.

    For more information, see “Suite Elements.”

  5. Add one or more suite elements for your application’s scriptability information.

  6. Fill in the scriptability information in the suite element (or elements) for your application by adding whichever of the following kinds of XML elements your application uses:

    1. Classes (see “Class Elements”)

    2. Commands (see “Command Elements”)

    3. Enumerations (see “Enumeration Elements”)

    4. Simple structures (see “Record-Type Elements”)

    5. Simple types (see “Value-Type Elements”)

    Your suite elements can refer to definitions from the Standard or Text suites that you've copied into your sdef.

To add an sdef file to the project for your application, see “Add the Scripting Definition File to Your Xcode Project.”

Add Information to the Scripting Definition File

The following sections provide descriptions and examples of the XML elements you’ll most commonly add to the sdef file for your application. These examples are based on the sdef file from the Sketch sample application, available from Apple.

As you add information to your sdef file, you can verify that the file is still valid at each step. Later, when testing your application, you can examine the information that Cocoa scripting extracts from your sdef file. For information on how to perform these steps, see “Debugging Scriptability Information.”

Important: To test changes you make to an sdef file, you must quit the application, rebuild, and relaunch it to pick up the changes. In addition, the Script Editor application caches scriptability information, so you may need to quit and relaunch Script Editor as well.

Class Elements

You'll need to add a class element to your sdef file for each type of scriptable object in your AppleScript object model—that is, for the objects scripters can access in scripts. A class element defines an object class by listing the properties, elements, and supported commands for instances of that class. Using the rich text class from the Text suite as an example, here is the information you supply in a class element:

The following listing shows the full class definition for the rich text class.

Listing 4-4  A class element for the rich text class

<class name="rich text" plural="rich text" code="ricT"
        description="Rich (styled) text">
    <cocoa class="NSTextStorage"/>
    <type type="text"/>
    <property name="color" code="colr" type="RGB color"
        description="The color of the first character.">
        <cocoa key="foregroundColor"/>
    </property>
    <property name="font" code="font" type="text"
        description="The name of the font of the first character.">
        <cocoa key="fontName"/>
    </property>
    <property name="size" code="ptsz" type="integer"
        description="The size in points of the first character.">
        <cocoa key="fontSize"/>
    </property>
    <element type="character"/>
    <element type="paragraph"/>
    <element type="word"/>
    <element type="attribute run"/>
    <element type="attachment"/>
</class>

Property Elements

For each class element in your sdef file, you'll need to add a property element for each accessible property of that class. A property is a unique data member of an object—it is synonymous with an attribute or to-one relationship. (A property element can also appear in a record-type element; see “Record-Type Elements” for more information.)

Using the color property of the rich text class as an example, here is the information you supply in a property element:

Element Elements

For each class element in your sdef file, you'll need to add an element element for each accessible element of that class. An element defines a class of objects contained in another object and represents a to-many relationship. For each of its defined elements, an object may reference zero or more items of that type. For example, a rich text object can have any number of character elements, including none. Element definitions define part of the object model for the application.

When you display an sdef in a dictionary viewer, an element element shows up as a link to the class for its type. For example, an element that has the class type of window will show up as a link to the window class.

The element elements shown in Listing 4-4 for the rich text class (such as <element type="character"/>) are all simple ones that take the default value for access (read-write) and key (for example, "characters"), as described in “Cocoa Elements.”

Using the document element from the application class in the Standard suite, here is a slightly more interesting example of the information you can supply for an element element:

Contents Elements

For some classes, you may want to define a contents element. A contents element is similar to a property element, except that the name and code are optional. If you omit them, they default to "contents" and "pcnt", respectively.

Cocoa Scripting treats the contents property as the implied container for its class. Scripts may refer to elements of the contents property as if they were elements of the class. For example, TextEdit documents have a text contents property. Technically, the first word of the first document is word 1 of text of document 1 but, because text is an implied container, a script can also say word 1 of document 1.

For related information, see “Implicitly Specified Subcontainers.”

Command Elements

A command element represents an action or “verb” such as close that specifies an operation to be performed. You will need to add a command element to your sdef file for each scriptable operation your application supports, including those defined in the Standard suite and implemented by NSScriptCommand and its subclasses. For the commands supplied by Cocoa, you can obtain information for your sdef as described in “Create a Scripting Definition File.”

Some commands can be sent directly to the objects they operate on and some cannot. For more information on the different kinds of commands and how to work with them, see “Object-first Versus Verb-first Script Commands.”

Using commands from the Standard suite as examples, here is the information you supply in a command element:

Scriptability information for a command is packaged in the command object created to handle a received Apple event. For information on how your application works with that information, see “Script Command Components,” as well as other sections in “Script Commands.”

Enumeration Elements

You may need to define enumerated constants in your sdef. For example, you may want to provide constants a scripter can use to specify information to a command. The Standard suite does this with the save options enumeration, which provides values a scripter can use with either the close command or the quit command to specify how to handle unsaved documents.

An enumeration element is a collection of symbolic constants, referred to as enumerators. As shown in the following listing for the save options enumeration, you provide a name and a code for the enumeration as a whole. You also provide a name, code, and description for each enumerator.

Listing 4-5  Definition of the save options enumeration

<enumeration name="save options" code="savo">
    <enumerator name="yes" code="yes " description="Save the file."/>
    <enumerator name="no" code="no  " description="Do not save the file."/>
    <enumerator name="ask" code="ask "
        description="Ask the user whether or not to save the file."/>
</enumeration>

An enumeration element can optionally contain an inline entry, indicating how many of the enumerators should be shown with a term that uses the enumeration when the dictionary defined by the sdef is displayed. By default (if you do not use the inline element), all enumerators are shown. If you specify inline="0", just the enumeration name (save options, in the example above) is displayed. However, a user viewing the definition can click the enumeration name to view the actual enumeration definition.

The following line shows how to modify the save options definition to limit the display to just the first two enumerators (yes/no).

<enumeration name="save options" code="savo" inline="2">

Record-Type Elements

If you need to define a simple structure, rather than a class, for your sdef, you can add a record-type element. For example, the following listing shows the record-type definition for print settings from the Standard suite. The cocoa key entries in this definition match the names that NSPrintInfo uses in its attributes dictionary.

In addition to property name, code, and description, a record-type element contains “Property Elements,” described previously. You can use record-type elements you've defined anywhere you specify the type of an element, property, or parameter in your sdef.

Listing 4-6  Definition of the print settings record-type

<record-type name="print settings" code="pset">
    <property name="copies" code="lwcp" type="integer"
        description="the number of copies of a document to be printed">
        <cocoa key="NSCopies"/>
    </property>
    <property name="collating" code="lwcl" type="boolean"
        description="Should printed copies be collated?">
        <cocoa key="NSMustCollate"/>
    </property>
    <property name="starting page" code="lwfp" type="integer"
        description="the first page of the document to be printed">
        <cocoa key="NSFirstPage"/>
    </property>
    <property name="ending page" code="lwlp" type="integer"
        description="the last page of the document to be printed">
        <cocoa key="NSLastPage"/>
    </property>
    <property name="pages across" code="lwla" type="integer"
        description="number of logical pages laid across a physical page">
        <cocoa key="NSPagesAcross"/>
    </property>
    <property name="pages down" code="lwld" type="integer"
        description="number of logical pages laid out down a physical page">
        <cocoa key="NSPagesDown"/>
    </property>
    <property name="error handling" code="lweh" type="printing error handling"
        description="how errors are handled">
        <cocoa key="NSDetailedErrorReporting"/>
    </property>
    <property name="fax number" code="faxn" type="text"
        description="for fax number">
        <cocoa key="NSFaxNumber"/>
    </property>
    <property name="target printer" code="trpr" type="text"
        description="for target printer">
        <cocoa key="NSPrinterName"/>
    </property>
</record-type>

Value-Type Elements

If you need to define a new basic type for your scripting support, you can do so with a value-type element. A value-type element defines a simple type that has no properties or elements accessible through scripting. The following listing shows the value-type definition for color from the Text suite. In addition to the type name and code, Cocoa value-type definitions should specify a corresponding Objective-C class, such as NSData or NSNumber (or your class that supports your value-type). The built in AppleScript types supported by Cocoa are listed in Table 1-1.

You can use value-type elements you've defined anywhere you specify the type of an element, property, or parameter in your sdef.

Listing 4-7  Definition of the color value-type

<value-type name="RGB color" code="cRGB">
    <cocoa class="NSColor"/>
</value-type>

Cocoa Elements

Some of the information in your sdef describes implementation details from your application. For example, property names in the sdef serve as KVC keys for accessing property values of scriptable application objects, as described in “Provide Keys for Key-Value Coding” and “Maintain KVC Compliance.” Similarly, your sdef contains other information that directly identifies classes or methods that are part of your application's scripting support.

To supply this implementation information for use by Cocoa scripting, you use cocoa elements. A cocoa element can contain the following attributes:

See the previous sections in this chapter for examples of these attributes.

Cocoa scripting will generate default keys for property and element attributes and for commands, if you do not specify them. For a property, it capitalizes each word of the property’s name except the first word, then removes any spaces. For an element, it specifies the plural of the element type. For a command, the default is NSScriptCommand.

The following table shows some examples of default naming.

Table 4-1  Default naming for attributes of cocoa elements

Attribute

Default value

<property name="current resolution">

currentResolution (key name)

<element type="monitor">

monitors (pluralized key name)

<command name="someCommand"...

(with no command class specified)

NSScriptCommand (class name for command)



< Previous PageNext Page > Hide TOC


© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-03-11)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.