Property Lists
Property lists are similar to lists, as both property lists and lists are collections of values. The fundamental difference is that a list is a simple sequence of values, while each value in a property list is identified by a name or label, called its key. For more information about working with property lists, see Property List Operators.
Example:
put {x:5,y:12} into myPropList // This property list includes two keys, x and y, and two values, 5 and 12, respectively.
Another difference, which is less obvious but can be even more significant, is that a property list is the simplest form of a SenseTalk object, which means it has behaviors as well as properties. The remainder of this section explains property lists primarily as data containers. For complete information about objects, see Objects and Messages.
Lists and property lists can be passed as parameters. For information on passing lists or property lists as parameters, see Parameters and Results.
Creating Property Lists
To create a property list, enter the keys and values for each of its properties, and enclose it in curly brackets . Each property’s key precedes its associated value, separated by a colon, with key/value pairs separated by commas.
Prior to SenseTalk 2.00 (Eggplant Functional 20.1), both lists and property lists used parentheses () by default instead of square brackets [] or curly braces (respectively). Parentheses are still considered valid syntax for property lists, but their use is not recommended as it can lead to confusion.
Example:
put {name:"Elizabeth", age:14} into daughter
To specify an empty property list, use an empty pair of curly braces {}
, a colon in curly braces{:}
, or the phrase empty object
or empty property list
.
Example:
put {:} into newPlist
A property list (because it is an object) can be helped by other objects, such as objects defined in text files.
Example:
put {name:"Hank", age:47} helped by parent,actor into candidate
Property lists can span multiple lines without the need to use line continuation (“\”) characters:
set detective to {name: "Sherlock Holmes",
address: "221 B Baker Street, London",
remarks: "Enjoys playing violin, solving mysteries"}
A property list can also use parentheses () instead of curly braces . When using parentheses, the entire property list must appear on a single line:
Example:
put (beverage: "milk", food: "chocolate chips") into groceries
When a property list occurs as the last item of a regular list, you can omit the curly braces from around the property list. This approach yields a syntax that has the appearance of a list/property list hybrid, but the named properties must come at the end. The following two lines are equivalent:
Examples:
put [5, 12, {color:"Green", size:42}] --> [5,12,{color:"Green", size:42}]
put [5, 12, color:"Green", size:42] --> [5,12,{color:"Green", size:42}]
Property Keys
All property names (keys) are text strings. Property keys can be entered without any special treatment if the property name: (a) begins with an alphabetic character or an underscore; and (b) contains only alphabetic characters, digits, and underscores. To use a key that contains spaces or special characters, or that begins with a digit, you must enclose the key in quotes.
Example:
set oddPlist to {alpha:"ok", "87":"four score and seven", "$":50}
For even greater flexibility, you can also specify a key using an expression enclosed in parentheses. The most common use of this capability is to simply use the value of a variable as the key, but any expression can be used.
Example:
if typeCode is 1 then set leaveType to "vacation" else set leaveType to "sick"
set leaveInfo to {employee:name, date:today, (leaveType):hoursTaken}
Here, a property list is created with 3 keys: "employee", "date", and either "vacation" or "sick" depending on the value of the typeCode
variable.
Duplicate Keys and the DuplicatePropertyKeyMode Global Property
If a key is repeated within a property list, SenseTalk uses the duplicatePropertyKeyMode
global property to control what happens. The default setting throws an exception. However, you can change the value so that it takes the first or last value instead, or adds all values to a list.
For more information about this global property, see the duplicatePropertyKeyMode
.
Property List Contents
Just like lists, SenseTalk lets you include any type of value in property lists, including lists and other property lists.
An expression can be used in assigning a value:
Example:
put {label:12592-A,
area:pi*r^2,
dimensions:[9,8,42],
color:{top:"Red", sides:"Black"} } into currentPart
Property lists are unordered, so when displaying a property list, such as with the log
or put
command, SenseTalk displays the keys in alphabetical order.
Example:
set Features to {width:9 inches,ports:16,hues:4}
put Features // Displays '{hues:4, ports:16, width:9 inches}'
Accessing the Properties in a Property List
You can access the properties in a property list by using several different methods. Using dot (.
) and apostrophe-S ('s
) as well as the of
syntax all invoke a special SenseTalk mechanism that you use either to access the requested property or to call a function on an object. The property ... of
syntax always accesses a property (it never calls a function).
When accessing a property of a simple property list, the effect of each mentioned syntax is the same: they each access a specific property. Property names are never case-sensitive.
Example:
put {firstName:"Joseph", age:50} into joe
put property age of joe // 50
put the AGE of joe // 50
put joe's firstName // Joseph
put joe.FirstName // Joseph
Example:
log evaluateProduct(price:49.99,category:"toys",name:"lego set #12332") // Passes a property list into the function handler "evaluateProduct"
function evaluateProduct ProductInfo // Declares function handler "evaluateProduct" with parameter "ProductInfo", which receives the passed property list
if ProductInfo.category is "toys" or ProductInfo.category is "books"
if ProductInfo.price is greater than 20 then
return ProductInfo.Name & " is too expensive."
else
return ProductInfo.Name & " has an appropriate price."
end if
else
throw "Unfamiliar product category.", "Category: " & ProductInfo.category
end if
end evaluateProduct
Referencing Property List Keys Using Variables
You can use a variable to access a property value within a property list. To use the value of a variable (or an expression) as the property name with any of these forms, you must enclose the variable name in parentheses. If you do not enclose the variable name in parentheses, SenseTalk treats it as the literal name of the property (or function) being accessed:
Example:
put {firstName:"Joseph", age:50} into joe
set myKey to "firstName"
put joe.(myKey) // Displays 'Joseph'