Mac OS X Reference Library Apple Developer
Search

AppleScriptObjC Release Notes

Contents:

Introduction
Objective-C/AppleScript Quick Translation Guide
Identifiers
Classes and Constants


Introduction

AppleScriptObjC is a new framework in Mac OS X v10.6 that allows Cocoa applications to be written in AppleScript. Using AppleScriptObjC, your AppleScript code can work with any Cocoa framework, and can function as a first-class citizen in the Cocoa world.

This note covers the syntactical details of using AppleScriptObjC and accessing Cocoa functionality from AppleScript. To write a complete application, you will need to learn Cocoa itself, and should start with the Cocoa Fundamentals Guide.

AppleScriptObjC obsoletes AppleScript Studio, which is deprecated as of Mac OS X v10.6. Developers using AppleScript Studio should migrate to AppleScriptObjC, and should start new projects using AppleScriptObjC exclusively. AppleScript Studio development is still supported, but functions for creating new projects have been removed, and the AppleScript Studio palette in Interface Builder has been hidden. To re-enable it, use this command in Terminal, and then re-launch Interface Builder:

defaults write com.apple.InterfaceBuilder3 IBEnableAppleScriptStudioSupport -bool YES

Objective-C/AppleScript Quick Translation Guide

AppleScriptObjC lets AppleScript objects serve as Objective-C objects in the Cocoa runtime, so development using AppleScriptObjC follows Objective-C and Cocoa patterns. This table is a quick reference to the AppleScript equivalents for various Objective-C features, shown as a definition of an ersatz class with the Objective-C version on the left and the AppleScript version on the right.

Objective-C

AppleScript

@interface MyClass : NSObject {

script MyClass

   property parent: class "NSObject"

An Objective-C class corresponds to an AppleScript script object. Inheritance in AppleScript uses the parent property.

   int myProperty;

   IBOutlet NSTextField *myField;

}

IBOutlet @property (retain) NSButton *myButton;

   property myProperty: 0

   property myField: missing value

   property myButton: missing value

An instance variable or a @property in Objective-C corresponds to a property in AppleScript. AppleScript does not explicitly tag Interface Builder “outlet” properties; Interface Builder will use any property with missing value as its initial value.

@end

@implementation MyClass

Objective-C divides class definitions into an @interface section, with all the properties, and an @implementation section, with all the method definitions. There is no such division in AppleScript; everything is contained in a script object.

- (IBAction) myAction:(id) object {

   on myAction_(object)

An Objective-C method definition corresponds to an AppleScript positional-parameter handler definition. AppleScript does not explicitly tag Interface Builder “action” methods; Interface Builder will use any method with the right signature: a method taking one parameter, and therefore with a name with a single trailing underscore (see below).

   [object method];

   [object method:param];

   [object methodWithArg1:p1 arg2:p2];

      object's method()

      object's method_(param)

      object's methodWithArg1_arg2_(p1, p2)

A method call in Objective-C corresponds to a positional-parameter handler call in AppleScript. The handler name is the Objective-C selector name with colons changed to underscores. The number of parameters should always match the number of underscores.

Note: AppleScript has three equivalent syntaxes for addressing object handlers and properties: x's y, y of x, and tell x to y. Only the first is shown here, because it is the most compact and matches the Objective-C order the best, but any of them will work. For example, object's method() could equally well be written as tell object to method().

   [object property];

   object.property;

      object's property()

      object's property

An Objective-C method with no parameters can be treated as either a message send or as a property access. Similarly in AppleScript, a method with no parameters can be treated as a handler call, with the parentheses, or as a property access, omitting them.

}

@end

   end myAction_

end script

Identifiers

AppleScript distinguishes between application identifiers, which are terms defined by AppleScript or an application’s scripting interface, and user identifiers, which are terms defined by the script writer. In the AppleScript Editor preferences, these are referred to as “application keywords” and “variables and subroutine names”, respectively. Identifiers passed to AppleScriptObjC, in particular Cocoa method names, must be user identifiers. If an identifier would conflict with an existing application identifier (or even an AppleScript reserved word), you can force it to be considered a user identifier by escaping it with vertical bars.

For example, NSColor has a set method for setting the current drawing color, but set is also an AppleScript reserved word for assigning variables. Using set as a handler name without escaping it would be a syntax error, so the correct usage looks something like this:

tell myColor to |set|()

Similarly, NSWindow has a bounds property, but bounds is also an application-defined term, so to use it as the NSWindow, it must be escaped:

get myWindow's |bounds|

When in doubt, add the vertical bars: if they are not necessary, then they are merely redundant and harmless.

Classes and Constants

Classes are treated as by-name elements, and constants and enumerated values as properties, both of current application. Observe the use of NSView, NSColor, and NSCalibratedRGBColorSpace in this example:

script MyView
   property parent: class "NSView"
 
   on drawRect_(rect)
       set c to current application's class "NSColor"'s whiteColor()
       c's colorUsingColorSpaceName_(current application's NSCalibratedRGBColorSpace)
   end convert_
end script

In places where current application is the implicit container, such as in the parent property or at the top level of the script, it may be shortened to my, or in the case of class specifiers omitted entirely. (In the handler body, the implicit container is the script object’s parent, class "NSView", so the current application's must be explicit.) This allows a convenience technique to save typing: define properties at the top level of the script, and then refer to the properties in the handler, like this:

property NSColor: class "NSColor"
property NSCalibratedRGBColorSpace: my NSCalibratedRGBColorSpace
 
script MyView
   property parent: class "NSView"
   on drawRect_(rect)
       set c to NSColor's whiteColor()
       c's colorUsingColorSpaceName_(NSCalibratedRGBColorSpace)
   end convert_
end script


Last updated: 2009-05-27

Did this document help you? Yes It's good, but... Not helpful...