12.2. Script Properties
As previously noted, you can use script objects for various purposes. Many times, you use them to take advantage of the persistent nature of their properties. A property is very similar to a variable. In fact, use of the term property variable is not inaccurate. Like ordinary variables, you can assign values to properties, use them in expressions, pass them as arguments to handlers, and so on.
To define a property, you write the keyword property followed by the name you choose, followed by a colon, followed by the initial value to assign to that property. This is the general format:
property name : value
This defines a property called name with an initial value specified by value. You must specify an initial value for a property when you define it, whether it's 0, missing value, or the result of an expression.
The following Try It Out illustrates how script properties are initialized once and retain their values through repeated executions of a script.
12.2.1.
12.2.1.1. Try It Out: Working with Script Properties
Follow these steps to work with script properties.
Type the following program into Script Editor:
-- Script properties script showXY property X : 100 set Y to 100 set X to X + 1 set Y to Y + 1 log "X = " & (X as string) & ", Y = " & Y as string end script run showXY run showXY run showXY
Click the Event Log tab and run the program. You should see the following output recorded in the Event Log:
(*X = 101, Y = 101*) (*X = 102, Y = 101*) (*X = 103, ...
Get Beginning AppleScript® now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.