< Previous PageNext Page > Hide TOC

Terminology Fundamentals

The terminology described in this document gives AppleScript Studio applications the ability to work with Cocoa user interface objects in scripts. This chapter provides key information for working with AppleScript Studio’s scripting terminology. It contains the following sections:

Version Information

To build AppleScript Studio applications, you must install a version of the Mac OS X Developer Tools that includes AppleScript Studio. To run an AppleScript Studio application, the target machine must have the AppleScript Studio runtime required for the application. An AppleScript Studio runtime is available if AppleScriptKit.framework is present in /System/Library/Frameworks.

AppleScript Studio attempts to maintain the following:

For example, an application built with AppleScript Studio version 1.1 that uses features added in version 1.1 requires the 1.1 runtime. However, a similar application that doesn’t use any features from AppleScript Studio 1.1 can run with the 1.0 runtime. And an application built with AppleScript Studio version 1.0 can run with any runtime, through version 1.4.

Note: An application created with AppleScript Studio version 1.2 does not run with any earlier runtimes, even if it does not use any new features of 1.2. This has been fixed in AppleScript Studio version 1.2.1.

Table 1-1 lists AppleScript Studio versions, the development environment they are part of, and the system software the corresponding runtime is installed with.

Table 1-1  Availability for AppleScript Studio development environment and runtime

AppleScript Studio Version

Distributed with development environment

Runtime installed with

1.0

December 2001 Developer Tools CD

Developer Tools CD (with AppleScript 1.8.2), or Mac OS X version 10.1.2 software update (with AppleScript 1.8.3 or later)

1.1

April 2002 Developer Tools CD

Developer Tools CD (with AppleScript 1.8.2 or later)

1.2

Mac OS X version 10.2 Developer Tools CD

Mac OS X version 10.2, and later (with AppleScript 1.9.0 or later)

1.2.1

December 2002 Developer Tools CD

Developer Tools CD (with AppleScript 1.9.1), or Mac OS X version 10.2.3 software update (with AppleScript 1.9.1 or later)

1.3

Mac OS X version 10.3 Xcode Tools

Mac OS X version 10.3, and later (with AppleScript 1.9.2 or later)

1.4

Mac OS X version 10.4 Xcode Tools

Mac OS X version 10.4, and later (with AppleScript 1.10 or later)

1.5

Mac OS X version 10.5 with Xcode 2.5 and later

Mac OS X version 10.5, and later (with AppleScript 2.0 or later)

For an example of how your application can determine whether the required version of AppleScript Studio is present, see the Examples section for the will finish launching event handler.

Starting with the version of Interface Builder released with Mac OS X version 10.2, there is a Nib File Compatibility preference on the General pane of the Interface Builder Preferences window. You should select a nib-file preference that suits your compatibility goals, from the following choices (and restart Interface Builder for the changes to take affect):

Important: Starting with Interface Builder 3.0 in Mac OS X version 10.5, there is a new 3.x nib file format. However, nibs previously saved in the 10.2 and later format can still be used. For information on working with nib file formats in Interface Builder 3.0, see Interface Builder User Guide.

Building AppleScript Studio Applications

You use Xcode, distributed with the Mac OS X Developer Tools, to create and build AppleScript Studio application projects, using one of the following three templates:

You use Interface Builder, also distributed with the Mac OS X Developer Tools, to create application interfaces for AppleScript Studio applications. Interface Builder provides active support for the Apple human interface guidelines, including feedback lines that show when an object is properly placed. Throughout this document, you will find illustrations of the available Cocoa user interface objects and information on where to find them in Interface builder.

Note: In describing the user interface objects available to your application, this document points out some object properties you can set in Interface Builder, as well as some default property values for user interface objects you instantiate. Keep in mind that property values can vary depending on the settings you choose in Interface Builder and on the interaction of objects in your application.

You can also find illustrations of windows, menus, controls, and many other user interface elements, as well as guidelines for when to use them and how to position them, in Apple Human Interface Guidelines. Please use these guidelines to help create applications that take full advantage of the Aqua interface. “See Also” describes how to access this document, as well as documentation for Xcode and Interface Builder.

All AppleScript Studio applications are Cocoa applications, built on the Cocoa application framework. As such, they are a mix of Cocoa code and AppleScript scripts, though you can write robust applications that require no additional Cocoa code on your part.

Some Cocoa capabilities are not currently available to AppleScript Studio classes. Where this is true, you have several options:

Sources of AppleScript Studio Terminology

AppleScript allows you to write scripts that control multiple applications, including many parts of the Mac OS itself. There are a number of tools for writing scripts, including the Script Editor application (distributed with the Mac OS) and various third party products. Most of the power in your scripts comes from the scripting terminology provided by applications and the operating system, not from the relatively small number of terms that are native to AppleScript itself. To take full advantage of the capabilities available, you need to know what terminology you can use in your AppleScript Studio application scripts.

Scripts in AppleScript Studio applications have access to the basic terminology that is available to all scripts, including:

An AppleScript Studio application also has access to:

Note: The name spaces of these various terminologies may conflict, which can result in confusing behavior in scripts.

Script Suites, aetes, and Sdefs

The Cocoa framework provides scripting information in the form of script suites, which are available to any application using the framework. Applications can also define additional suites. A script suite consists of at least one suite definition and one suite terminology, contained in external files. A suite definition describes scriptable objects in terms of their attributes, relationships, and supported commands. This information is stored as key-value pairs in a property list. A property list is a structured, textual representation of data, commonly stored in Extensible Markup Language (XML) format.

A suite terminology provides corresponding AppleScript terminology—the English-like words and phrases you can use in a script—for the class and command descriptions in a suite definition. Suite terminologies are also stored as property lists. Frameworks and applications typically place terminology files in a localized resource directory named English.lproj. (English is currently the only supported dialect in AppleScript.)

Carbon applications originally stored their terminology information in a different format—as an 'aete' (Apple event terminology) resource. While this format is still supported, a new scripting definition, called the sdef format, was added in Mac OS X version 10.2, though not fully supported until Mac OS X version 10.4. The scripting definition provides a mechanism for defining scripting terminology that can be used by both Carbon and Cocoa applications.

For more information on how Cocoa applications define scripting terminology in different versions of Mac OS X, see Cocoa Scripting Guide.

How Suite Information Is Organized

Wherever possible, this document provides connecting links between class, command, event, property, element, and constant definitions. For each AppleScript Studio suite, it provides the following information.

Terminology Supplied by the Cocoa Application Framework

AppleScript Studio applications can take advantage of the built-in Cocoa terminology found in two default suites:

To examine the terminology in Cocoa’s default suites, use Script Editor or Xcode to open the dictionary for any scriptable Cocoa application, such as the Sketch sample application distributed with the Xcode developer tools. For more information on examining terminologies, see Support for Scriptable Applications in AppleScript Overview.

Important:  The Cocoa application framework provides default script suites to support scriptable Cocoa applications. While AppleScript Studio applications have access to this terminology, they will primarily use the terminology described in the next section.

Terminology Supplied by AppleScript Studio

AppleScript Studio defines its own script suites, which add to the scripting terminology defined by Cocoa. These suites provide additional terminology you can use to script objects based on many classes from the Cocoa application framework. This terminology is defined in several suite definition and suite terminology files in AppleScript Studio’s own framework, the AppleScriptKit framework. Each suite can include class definitions (which include elements and properties), command and event definitions (which have an associated syntax), and enumerations (or predefined constants).

Note: AppleScript Studio draws a distinction between a command, which is a word or phrase a script can send to an object to request an action, and an event, which is an action, typically generated through interaction with an application’s user interface, that causes a handler for the appropriate object to be executed. That is, scripts can send commands to objects, while events, often the result of user actions, generate calls to event handlers in scripts.

There are several ways to display the terminology for AppleScript Studio:

Each of the following chapters describes the terminology for one AppleScript Studio script suite. Enumerations (a form of constant) are available to all suites, and are described in “Enumerations” in the Application suite:

Standard Key Forms

To access a property or element of an object, a script can specify the property or element by any key form it supports. Key forms simply indicate how data should be interpreted. For example, if a property supports the absolute position key form, a script can use a statement like the following:

set myWindow to the third window

Objects in AppleScript Studio applications are based on Cocoa class definitions, and AppleScript Studio’s scripting support allows most classes to conveniently support a broad range of key forms. As a result, most objects you work with in AppleScript Studio applications support the following key forms:

See also “Accessing Properties and Elements.”

Naming Conventions for Methods and Handlers

The Cocoa application framework follows a naming convention that helps explain when certain methods are called. This convention, which is reflected in the terminology for AppleScript Studio’s event handlers, inserts should, will, and did in method names. Table 1-2 describes the meaning of these terms. Note that to indicate a completed operation, AppleScript Studio uses the past tense, rather than the term did.

Table 1-2  Naming conventions in Cocoa and AppleScript Studio

Cocoa phrase

Explanation

AppleScript Studio examples

should

Asks whether an operation should take place. You can cancel the operation by returning false.

should open

should close

will

An operation is about to take place. You can prepare for it, but not prevent it.

will resize

will hide

will quit

did

An operation has completed. You can perform actions in response to it. AppleScript Studio uses past tense, rather than the term did.

activated

launched

miniaturized

zoomed

So, for example, you can add a should close handler to a window object. When the handler is called, it can determine whether the user has performed some essential task—if not, it can return false and refuse to allow the window to close. A will close handler cannot cancel the close operation, but it can perform any necessary tasks to prepare for closing. Finally, a closed handler can perform any tasks required after closing.

At application startup time, handlers connected to the application object are called in this order:

  1. will finish launching

  2. awake from nib

    Important:  If you are working with the version of Interface Builder distributed with Mac OS X version 10.2, see “Version Information” for information on setting a Nib File Compatibility preference.

  3. launched

  4. will become active

  5. activated

  6. idle

Accessing Properties and Elements

While elements typically have a singular name, such as document, window, and pasteboard for certain elements of the application class, you use the plural form to access these elements in scripts. For example, you could use the following script in Script Editor to get a list of windows from an AppleScript Studio application (and you can use similar terminology within the application, where you won’t need the tell application block):

tell application "MyApp"
    set windowList to windows
end

A class may list properties that you cannot access in a particular application or at a particular time. For example, the application class lists a key window property that identifies the window that is the current target for keystrokes. But if an application has no windows open, it will not have a key window, and attempting to access the key window property will result in a nil value. (For more information on the key window, see the key and main property descriptions for the window class.)

Starting with AppleScript Studio 1.2, you can use the properties property to access the properties of most AppleScript Studio objects. This feature relies on changes present in the version of Cocoa distributed with Mac OS X version 10.2. The following Script Editor script shows how to display the properties of a button in an AppleScript Studio application:

tell application "simple"
    properties of button 1 of window 1
end tell

The results of this script might be something like the following, where missing value indicates a value couldn’t be obtained for a certain property:

{visible:true, content:false, double value:0.0, ignores multiple clicks:false, title:"Button", image:missing value, name:missing value, bordered:true, enabled:true, window:window id 1 of application "TestingControls", allows mixed state:false, float value:0.0, image position:no image, string value:"0", id:2, flipped:true, roll over:false, opaque:false, alternate image:missing value, bounds:{47, 45, 131, 77}, bounds rotation:0.0, tag:0, class:button, alignment:center text alignment, enclosing scroll view:missing value, can draw:true, menu:missing value, target:missing value, bezel style:rounded bezel, alternate title:"", needs display:false, current cell:item id 3 of application "TestingControls", auto resizes:false, key equivalent modifier:0, position:{47, 45}, formatter:missing value, continuous:false, size:{84, 32}, cell:item id 3 of application "TestingControls", state:0, button type:momentary push in button, font:item id 4 of application "TestingControls", sound:missing value, integer value:0, tool tip:missing value, super view:content view of window id 1 of application "TestingControls", key equivalent:"", current editor:missing value, transparent:false, visible rect:{0, 0, 84, 32}, contents:false}

If your application accesses a property that may not have a current value, you should enclose statements that use the property in a try, on error block. For an example of such a block, see the Examples section for the path for command.

Similarly, an object may have zero or more of each kind of element listed for its class. If you attempt to access an element of a type for which an object currently has no instances, you will get back an empty list. You won’t need a try, on error block, but depending on the logic in your script, you may need to verify that the list is not empty.

Important:  This document points out some properties you can set in Interface Builder, and some default property values, but it does not attempt to be an exhaustive guide to Interface Builder, which includes its own built-in help.

Event Handler Parameters

In an AppleScript Studio application, you use Interface Builder to connect application objects to event handlers in application scripts. For example, if you connect a clicked handler to a button, when a user clicks the button, the handler is called in your application script.

When you first connect a handler in Interface Builder, it installs a template in your script file. Listing 1-1 shows the template for a clicked handler. The template matches the full syntax for the event handler, which in this case has just one parameter, theObject. Nearly every event handler template includes the theObject parameter (which is almost always a reference to the object for which the handler is called), though some contain additional parameters as well.

Listing 1-1  A new clicked handler

on clicked theObject
    (* Add your script here. *)
end clicked

The syntax table for each event handler shows the complete syntax that is used for constructing a template. Once a template has been inserted into your application script, you are free to change the names of the parameters and to delete (or ignore) any parameters that are marked as optional in the syntax table.

One way to help document your scripts is, where possible, to change the name of the theObject parameter to reflect knowledge of the object it refers to. For example, if a clicked handler is only called in response to a click on a Search button, you might change the event handler declaration to on clicked searchButton. Changing a parameter name has no effect on how the handler operates.

If the handler may be called for one of several objects (say a series of buttons on one pane of a tab view), you can both change the parameter name and add a comment to clarify its usage:

on clicked importButton
    (* This handler handles buttons on the Import pane. *)
    -- Your script statements here
end clicked

Connecting Key and Mouse Event Handlers

AppleScript Studio provides a number of key and mouse event handlers, such as keyboard down, keyboard up, mouse down, mouse up, mouse dragged, and so on. For objects of certain types, particularly for application objects, key and mouse event handlers that you connect may never get called because they are handled by other objects before they get to the application object. If your application really needs to deal with these events, consider connecting them to objects in the user interface that inherit from the control class, such as button, slider, stepper, or text field objects.

Scripting Error Messages

AppleScript Studio applications are Cocoa applications, and when your application gets a script-related error message, that message is generated by Cocoa’s scripting support. The following table lists scripting errors, through Mac OS X version 10.2. The table includes the current error number. (For a brief description of some related AppleScript terminology, see Glossary of AppleScript Terms in Cocoa Scripting Guide.)

These messages may not always provide the detail you’d like for debugging your application, but other options are available. Among the most useful are using Xcode’s debugging support to set breakpoints, examine variables, and single-step through scripts; and using AppleScript Studio’s log command to output values and messages from your running application.

Table 1-3  Cocoa scripting error messages

Error constant (value)

Description

NSNoScriptError (0)

no error

NSReceiverEvaluationScriptError (1)

the object or objects specified by the direct parameter to a command could not be found

NSKeySpecifierEvaluationScriptError (2)

the object or objects specified by a key could not be found

NSArgumentEvaluationScriptError (3)

the object specified by an argument to a command could not be found

NSReceiversCantHandleCommandScriptError (4)

the object doesn’t support the command that was sent to it

NSRequiredArgumentsMissingScriptError (5)

one or more required arguments are missing

NSArgumentsWrongScriptError (6)

an argument (or more than one argument) is of the wrong type or otherwise invalid

NSUnknownKeyScriptError (7)

an unidentified error occurred; indicates an error in the scripting support of your application

NSInternalScriptError (8)

an unidentified error occurred; indicates an error in the scripting support of your application

NSOperationNotSupportedForKeyScriptError (9)

the implementation of a scripting command signaled an error

NSCannotCreateScriptCommandError (10)

the application received an invalid or unrecognized Apple event

Using the Sample Scripts

This document contains many sample scripts and event handlers, both complete and partial. In some cases, long statements in the samples contain hard carriage returns to make them easier to read. If you cut and paste samples from this document into AppleScript Studio application scripts, or into scripts you use with Script Editor or another script-editing application, you may have to remove the hard carriage returns to compile the sample.

Panels Versus Dialogs and Windows

You’ll have to get used to a bit of naming inconsistency regarding the use of panels, dialogs, and windows. For historical reasons, the Cocoa application framework uses “panel” in many cases where Apple Human Interface Guidelines, the final arbiter on Mac OS interface issues, would call for “dialog” or “window”. The use of “panel” is even embedded in Cocoa class names, such as NSPanel, NSFontPanel, NSOpenPanel, and so on. AppleScript Studio terminology, which is based closely on Cocoa user interface classes, generally adopts similar names, so that it provides the “Panel Suite,” which includes the color-panel, font-panel, open-panel, and related classes.

Given that history, it isn’t possible for this document to be completely consistent when using these terms. However, the following list shows how some usages of “panel” correspond to terms used by the Apple human interface guidelines. See “See Also” for information on where you can obtain Apple Human Interface Guidelines.

A dialog can be displayed as a sheet (attached to a window), in which case it is document modal, and a user can work with other windows in the application before dismissing the sheet. For example, you can use the optional attached to parameter of the display command to display the open-panel dialog as a sheet.

A Word on Unicode Text

When you get text back from AppleScript Studio, it will almost always be supplied as Unicode text. In some cases you may need to convert from Unicode text to plain text. For a discussion and example of how to convert Unicode text to plain text, see the Discussion section for the default entry class.

In some cases, when you supply text to AppleScript Studio, such as for use with the localized string command, you should ensure that you are working with Unicode text. See the Examples section for localized string for information on how to specify UTF-8 format for a file in your application project.



< Previous PageNext Page > Hide TOC


© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


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.