Developing AppleScript Applications
The following is a transcription of the introductory movie, demonstrating how to use outlets and actions to connect the interfaces of AppleScript-Cocoa applications:
Welcome to an overview, on connecting the interfaces of AppleScript-Cocoa applications, to their corresponding function code.
Specifically, you’ll learn how to connect, the interface of this example AppleScript-Cocoa application to this AppleScript script, that is included with the example project.
The example application, is a simple utility, for converting user-provided values, between three units of temperature: Fahrenheit, Celsius, and Kelvin.
The conversion is performed, when the user enters a numeric value in the input field, and presses the return key.
The conversion is also triggered, whenever a new unit type, is selected from the pop up menu.
The function code for the example application, is an AppleScript script object, containing a series of script properties, that will represent the various visual interface elements, and a script handler, triggered by actions in the interface, to perform a conversion between the three temperature systems.
The various interface elements, will be connected to the script object, using standard Cocoa programming constructs, called: outlets, and actions.
In Xcode, double-click the main menu nib, in the Groups and Files list.
And the application's visual interface, will be opened in the Interface Builder application.
The interface project window, contains the various components that make up the application, including a controller for the script object in the Xcode project.
This controller, is used to connect the various script elements, to the application's interface.
To begin the process of connecting the script to the interface, hold down the control key, and click the script controller object, to open its connections palette.
The connections palette, displays the names of all of the properties contained in the project script, as outlets, and the names of all of the handlers contained in the project script, as actions. The connections palette is used to connect script properties and handlers, to their corresponding interface elements in the application window.
Interface builder outlets, are a means of connecting an application interface element, to the code that controls or supports it. In AppleScript-based Cocoa applications, interface elements are commonly connected to script properties.
Once connected, the script property is used as a reference to its connected interface element.
To connect a script property to an interface element, click and hold on the link button, located to the right of the script property's name, and then drag the connection wire to the corresponding interface element: in the application interface. Once the cursor is over the interface element, the element will be highlighted, and a label describing its class type, will be displayed. Release the mouse, and an outlet connection will be made between the script property and interface element, and be displayed on the connections palette.
The connection link is a visual representation, of the relationship between the script property, and the interface element. Should you want to cancel this pairing, press the small ex to the left of the class name.
Continue the process of assigning outlets to the interface elements, by connecting the remaining items in the outlet list, to their corresponding interface elements.
Linking interface elements, that act as action triggers, to their script function code, is a process, similar to connecting outlets to script properties.
To connect a script handler to its corresponding interface trigger, click and hold on the link button, located to the right of the handler's name, and then drag the connection wire, to the corresponding interface element. Release the mouse, and an action connection will be made between the script handler and interface element, and displayed on the connections palette.
To connect a script handler to a second interface trigger, drag its connection wire, to the second interface element. Release the mouse, and an action connection will be made between the script handler and the second interface element, and be displayed on the connections palette.
With all of the connections made, the interface project can now be saved. Close the connections palette, and select the Save option, from the File menu.
And return to the open Xcode project.
A script object, is a section of AppleScript code, placed within an AppleScript script file.
It begins with the term “script,” immediately followed by the name of the script object, and closes with the terms: end script.
A script object, can contain, any: or all, of the standard AppleScript elements, such as properties, and handlers.
Script properties: used as outlets for interface elements, are written in the same manner, as standard script properties.
The property declaration begins with the term “property” followed by the variable, that will contain the value of the property.
Properties used as outlets, can have names that include underscore characters, or be named, using inner-capped terms.
As with standard property declarations, the property variable is delimited from its default value, by a colon.
Which is followed: by the enumeration of “missing value.”
All outlet properties, use this enumeration as their initial value, which is automatically replaced, with a cocoa reference to an interface object, once the script is executed.
In AppleScript-Cocoa, script handlers, can be triggered by user interaction, with the application interface.
To adapt a script handler to be an interface action, begin the handler with the term “on,” followed by the name of the handler.
When script handlers are used as actions, their names should not contain any underscore characters. And instead, should rely on inner-capped characters to delineate terms.
The name of the handler, is then followed by a single underscore character, which indicates, the handler’s status, as an interface action.
The script handler is then closed, with a pair of parentheses, placed around the term “sender.” Sender is a variable, and will contain a Cocoa object reference, to the interface element, that triggered the execution of the handler.
Prior to the release of Mac OS X v10.6 the various elements of an application’s interface, were considered to be AppleScript objects, and were managed using standard AppleScript methods.
However, starting in Mac OS X v10.6 interface elements: are now Cocoa objects, and are managed using standard Cocoa methods, called from within AppleScript scripts. Let’s examine the differences between the two designs.
In AppleScript Studio, the individual interface elements, were assigned unique names, as a way of identifying them in their controlling scripts. These names were then used to create object references, which were stored in AppleScript variables, when the scripts were run.
The stored object references, could then be used, to get or set, the value of the object’s properties.
In AppleScript-Cocoa applications, object references to interface elements, are automatically generated, when interface elements are linked to their corresponding script properties.
The values of a property of a cocoa object, are retrieved or set, by calling standard cocoa methods, from within the AppleScript script.
In other words, in Mac OS X point five, application interface elements, were treated as AppleScript objects, whose property values were retrieved or set, using standard Applescript methods.
In Mac OS X v10.6, this behavior changed, and application interface elements are now treated as cocoa objects, whose property values are retrieved or set, using standard Cocoa methods, called from within the AppleScript script.
Although different in their syntax, the two implementations perform essentially the same functions.
The same distinctions apply to changing the value of an interface object’s properties.
In AppleScript Studio, the syntax for changing the value of a property, is to tell, the AppleScript object, to set, [[slnc 500]] its property to a new value.
In AppleScript-Cocoa, the syntax for changing the value of a property, is to tell, the cocoa object, to execute a cocoa method, using the new value as the function parameter.
Even though they are based on different architectures, the two methods, result in the same actions being performed.
To complete the process of creating the application, click the Build and Run button, in the Xcode window toolbar.
And the temperature converter application will launch.
This concludes an overview, on connecting the interfaces, of AppleScript-Cocoa applications.
To learn more about AppleScript, or any of the Mac OS X automation technologies, visit: www.macosxautomation.com. Thank you!