Objects
SenseTalk was designed to enable people to create their own software, without years of study to master the intricate and arcane details of programming. Information provided in other sections of this documentation focus on SenseTalk scripts primarily as simple lists of statements that tell the computer what you want it to do. For many people that basic approach to scripting will be sufficient for their needs.
For those who care to learn just a little bit more, SenseTalk’s object model offers a much richer environment, without adding greatly to its complexity.
The information explained here provides an overview of the modular structure of SenseTalk, and introduces the key concepts and terminology that you will want to be familiar with as you proceed with learning how to write your own object-oriented SenseTalk software.
Setting the Stage
SenseTalk is a "scripting" language. You create SenseTalk software by writing scripts that describe what different elements of your system will do. The usual way to do this in SenseTalk is to create a number of different “objects” which will be the actors in your system. Each object has its own script that tells what it does in the system. By taking this modular approach, each script can be relatively short and self-contained, which makes even complex systems relatively easy to work with.
The objects in your system will interact by sending and receiving messages. An object responds to a message if it has a handler for that message. A script consists of a series of handlers, one for each message that the object will respond to. Any other messages will be ignored by the object.
One SenseTalk object can help another. When you have a number of objects that need to have similar behaviors or abilities, you can create a helper object which embodies those shared behaviors and abilities. Each of the other objects can then be helped by the helper object, eliminating the need for them to each have those behaviors defined in their own scripts. This saves you a lot of time and effort. It also makes it easy to update a behavior by simply changing the script of the helper object. All of the other objects will then “inherit” the changed behavior.
It should be noted, for those who may be familiar with other object oriented languages, that SenseTalk is somewhat different. Most other languages define “classes” of objects and generally only implement behavior at the class level, for all objects in that class. In SenseTalk, each individual object has its own script, so it can have its own unique behavior.
SenseTalk has no classes, but its helpers provide a similarly rich set of capabilities by allowing objects to use (or inherit) functionality provided by any number of other objects. This all-object (classless) approach is both simpler and more versatile than class-based systems.
Objects Defined
SenseTalk is an Object-Oriented Language. SenseTalk scripts describe the behavior of objects. Depending on the host environment, an object may be something visible which can be directly manipulated and interacted with, such as a button or an image on the screen, or it may be more abstract, such as an object representing a bank account or an exercise schedule.
Whether an object represents something visual or something that is purely conceptual, all objects in the SenseTalk world share certain characteristics. Any object can have attributes, called properties, which store information that is important to that object. An object may also have behaviors, which are defined by the object’s script and define what the object can do.
In the case of a picture shown on a computer screen, its properties might include such things as its height and width as well as the colors of all the dots that form the image itself. An object representing a person’s contact information would include properties such as their name, telephone number, and mailing address.
An object’s behaviors as defined by its script might include such things as turning an image upside down, or sending an email message to a person on a mailing list. Other “behaviors” of an object may be more passive, such as providing information about the object which is not directly represented in its properties. For example, an object representing a person might include a property that stores their birth date. The object’s script could provide the person’s age by accessing the current date and calculating the number of years that have passed since their birth date.
Property Lists
Property lists were described in another section as a collection of values identified by their keys. It was mentioned there that a property list is actually a simple object.
A property list is an object that’s mainly properties. Behaviors can be added to a property list, though, either by assigning helpers, or by setting the script property to a valid script. For more information, see Helpers. To set the script of an object, simply store the text of one or more handlers into the “script” property of that object:
put {width:7,length:12} into myRect
set the script of myRect to {{
function area
return my width * my length
end area
}}
put myRect's area --> 84
Script Files
In many SenseTalk scripting environments, each script is stored in a text file on the disk. In these environments, each such file is a SenseTalk object, and the contents of the file is the object’s script.
A script consists of a sequence of SenseTalk commands (also called statements) which define the behaviors of that object. Here is the simplest and shortest complete SenseTalk script you are likely to encounter:
put "Hello, World!"
A script file is an object that’s mainly behaviors. The script above constitutes a very simple object, with a single behavior that will be invoked when the script is run: displaying the words “Hello, World!”.
Properties
Declarations
In addition to behaviors, a script file may also include properties
declarations if desired. When an object is created by loading a script file from disk, any properties declarations in that script will define initial values for the object’s properties.
A properties
declaration can be placed outside of any handler. If it is the first thing in your script, the initial handler comes after it. For example, it would appear before a params
declaration. Otherwise, it ends the initial handler like a declared handler does. For more information on working with handlers, see Handlers.
While it is possible to have multiple properties declarations in the same script, for simplicity and to avoid confusion it is recommended to only use one.
Syntax:
Properties
propertyKey1 : value1 ,
{propertyKey2 : value2} ...
end Properties
Example:
This properties declaration defines five properties of the object, and assigns them initial values. Values that contain spaces and special characters must be enclosed in quotation marks, as shown here for the name
and birthDate
properties. Simple values do not require quotes, but may be quoted if you prefer. Some properties may be assigned a list of values, as shown for the helpers
property.
Properties
name: "Charlie Brown",
birthDate: "May 14, 1942",
hairColor: brown,
numberOfSiblings: 1,
helpers: ["Linus", "Lucy"]
end properties