Objective-C to AppleScript Quick Translation Guide
This appendix provides AppleScript equivalents for typical Objective-C features of a class. Below the table is a list of notes that correspond to the numbers in column 1.
Note |
Objective-C |
AppleScript |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
An Objective-C class corresponds to an AppleScript
script
object. In AppleScript, inheritance is denoted using theparent
property.An instance variable or
@property
in Objective-C corresponds to aproperty
in AppleScript.AppleScript doesn’t require explicit tagging of Interface Builder outlet properties (
IBOutlet
). Interface Builder sees any property with a value ofmissing value
as a potential outlet.Objective-C divides class definitions into an
@interface
section containing properties and an@implementation
section containing method definitions. AppleScript has no such division. Properties and methods are all contained within the samescript
object.An Objective-C method definition corresponds to an AppleScript handler that uses either an interleaved- or positional-style for parameter placement.
Interleaved parameters are preceded by colons and separated by spaces, similar to Objective-C syntax. See Handlers with Interleaved Parameters in AppleScript Language Guide.
A positional parameter hander name is the Objective-C selector name, with colons changed to underscores. This handler name is followed by parentheses enclosing comma-separated parameters. See Handlers with Positional Parameters in AppleScript Language Guide.
AppleScript doesn’t require explicit tagging of Interface Builder action methods (
IBAction
). Interface Builder sees any method with a single parameter as a potential action method.A method call in Objective-C corresponds to an AppleScript handler call that uses either interleaved- or positional-style parameters. Regardless of style, parameters must always be provided in the order the method definition specifies.
AppleScript has three equivalent syntaxes for addressing object handlers and properties:
object's method
,method of object
, andtell object to method
.An Objective-C method with no parameters, such as a property or constant, can be called using an explicit accessor method call or more concise dot syntax. Similarly in AppleScript, a method with no parameters can be called using a handler call with empty parentheses, or as a property without the parentheses.
In AppleScript, handlers and
script
objects are closed using theend
syntax.
Resolving Terminology Conflicts in AppleScriptObjC
AppleScript distinguishes between reserved words, application identifiers, and user identifiers. Reserved words are terms defined by AppleScript itself. Application identifiers, also known as application keywords, are terms defined by an app’s scripting dictionary. User identifiers are variable or subroutine names defined by the script writer.
Identifiers passed to AppleScriptObjC, in particular, Cocoa method names, must be user identifiers. If an identifier conflicts with a reserved word or an application identifier, you can force it to be considered a user identifier by escaping it—enclosing it in vertical bars. For example, the NSColor
class has a set
method for setting the current drawing color. However, set
is a reserved AppleScript term for assigning variables. Calling the set
method without escaping it would produce a syntax error. Listing 43-1 shows the correct usage.
APPLESCRIPT
myColor's |set|()
-- OR
|set|() of myColor
-- OR
tell myColor to |set|()
Similarly, NSWindow
has a bounds
property, but bounds
is an application-defined term. Therefore, any references to this property must also be escaped. See Listing 43-2.
APPLESCRIPT
get myWindow's |bounds|
-- OR
get |bounds| of myWindow
-- OR
tell myColor to get |bounds|
When in doubt, add the vertical bars. If they’re unnecessary, they are merely redundant and harmless.
Importing Frameworks
To import a framework in AppleScript, use the use
command, followed by the framework name. See Listing 43-3.
APPLESCRIPT
use framework "Foundation"
set theString to "Hello World"
set theString to stringWithString_(theString) of NSString of current application
set theString to (uppercaseString() of theString) as string
--> Result: "HELLO WORLD"
Accessing Scripting Additions
A normal AppleScript automatically loads and has access to terminology from scripting additions that are installed on the system. In AppleScriptObjC scripts, you must explicitly state that you want to use scripting addition terminology. Listing 43-4 shows how to do this.
APPLESCRIPT
use scripting additions
display dialog "Hello World" as string
Classes and Constants
In AppleScriptObjC, Objective-C classes and constants are referred to in the context of the current application
constant—a reference to the app that’s running the script.
In this context, classes are referenced using the class
specifier, followed by the class name, as shown in Listing 43-5.
APPLESCRIPT
use framework "Foundation"
class "NSView" of current application
Constants are referenced without a preceding identifier. See Listing 43-6.
APPLESCRIPT
use framework "Foundation"
current application's NSCalibratedRGBColorSpace
-- OR
NSCalibratedRGBColorSpace of current application
Listing 43-7 demonstrates how to reference both Objective-C classes and constants.
APPLESCRIPT
script MyView
property parent : class "NSView"
on drawRect:rect
set theWhiteColor to current application's class "NSColor"'s whiteColor()
-- OR
set theWhiteColor to whiteColor() of class "NSColor" of current application
-- OR
tell class "NSColor" of current application to set theWhiteColor to whiteColor()
theWhiteColor's colorUsingColorSpaceName:(current application's NSCalibratedRGBColorSpace)
-- OR
colorUsingColorSpaceName_(NSCalibratedRGBColorSpace of current application) of theWhiteColor
end drawRect:
end script
In places where current application
is the current context, such as the top level of a script, current application
may be shortened to my
or me
. In the case of class specifiers, it may be omitted entirely. See Listing 43-8.
APPLESCRIPT
use framework "Foundation"
class "NSView"
my NSCalibratedRGBColorSpace
-- OR
NSCalibratedRGBColorSpace of me
As a convenience technique to save typing, it’s good practice to define properties for classes at the top of your script. This way, you can refer to them by property name throughout your script.
APPLESCRIPT
script MyView
property parent : class "NSView"
property NSColor : class "NSColor"
on drawRect:rect
set theWhiteColor to NSColor's whiteColor()
theWhiteColor's colorUsingColorSpaceName:NSCalibratedRGBColorSpace
end drawRect:
end script
Resources
For additional information about AppleScriptObjC, see AppleScriptObjC Release Notes and the third-party book EveryDay AppleScriptObjC.
Using Dictation to Run Scripts
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13