Messages
SenseTalk is an object-oriented language that works by passing messages from one object to another. When a message is sent to an object, it can respond if its script has a handler for that message. If the object does not have a handler for a particular message, that message is passed along to some other object. For more information on working with handlers, see Handlers.
Objects do things only when they receive a message. An object can send messages to itself, or they may be sent to it by another object, or by the environment in which the script resides. For example, when a script is invoked from the command line, it is sent a message based on the name of the script, which causes the script to run.
To begin with, all you need to know is that when a message is sent to an object, it can either receive and handle that message, or it can ignore it. Continue reading to look at messages and how they are sent.
Sending Messages
When a script is running, it sends messages constantly as it executes its commands. A message is always a single word, and the simplest message-sending statement is a single-word command, which is the name of the message to be sent. For example, the second line of this script sends the message “greetTheUser”:
put "Hello, World!"
greetTheUser
put farewellMessage()
In this script, the put
commands on the first and third lines also send messages. In fact, with the exception of a few fundamental SenseTalk control structures, almost every command in a script sends a message. Function calls, such as the call to the “farewellMessage()” function above, also send messages.
Command Messages and Function Messages
SenseTalk actually sends two different types of messages. Command messages are sent by commands, such as greetTheUser
in the example above. Function messages are sent by function calls, such as farewellMessage()
in the example above. The two message types are identical except for the type of handler that will receive them (see Handlers). For more information on how to call functions, see Functions. For more information on how to use commands, see Commands and Comments.
Where are Messages Sent?
We said earlier that messages are used to allow objects to communicate with one another, so you might be wondering where the “greetTheUser” and “put” messages above are being sent. What object will receive these messages? The answer is very simple, though perhaps somewhat surprising: the object containing these commands will send these messages to itself!
While at first it may not appear to be terribly useful for an object to send messages to itself, in practice any message which is not handled by the object is passed along for possible handling by other objects or ultimately by one of SenseTalk’s built-in commands or functions.
Messages can also be sent directly to some other particular object, if desired. This is done using the tell
command, send
command, run
command, run
function, the square-bracket messaging syntax, or by using of
, .
or 's
like this:
put the salary of writer into authorPay -- sends "salary" to writer
get investor's balance("Checking") -- sends "balance" to investor
add paycheck.netIncome() to it -- sends "netIncome" message to paycheck
The Message Path
When a message is sent to an object, the message follows a path through several different objects. Each object along the path which the message follows will be given a chance to handle the message. If the object has a handler for that message, the instructions in that handler are carried out. Otherwise, the message is offered to each of the other objects along the path in turn, until one is found which handles the message.
Usually, once a message is handled by an object, that is the end of it. However, in some cases an object’s handler might choose to pass the message on along the path, allowing other objects the opportunity to handle that same message (see the pass
commands under Passing Messages).
BackScripts and FrontScripts
Looking at the larger picture, in addition to the object to which the message is sent (known as the target object), the message path might also include other objects, both before and after the target object and its helpers. These inclusions are made up of the objects in the the FrontScripts
and the BackScripts
, plus any additional objects that might be included by the host application.
The BackScripts
is a global property containing a list of objects that get the chance to handle a message after the target object, if the target object did not handle it. So, by inserting an object (a library or script) into the BackScripts
that object’s handlers become available to be used by any script, while still allowing any individual scripts to provide their own handlers to override the BackScripts
behavior. The BackScripts
is such a useful mechanism that two special commands, Start Using
and Stop Using
, are provided to simplify adding and removing objects in the BackScripts
.
The FrontScripts
is a rarely-used global property containing a list of objects that get the chance to handle a message before the target object is even offered the message. This gives these objects the chance to intercept a message and potentially override behaviors of any object, which is rarely wanted but occasionally useful for tasks such as logging a message before passing it along the message path.
Start Using
Command
Behavior: Inserts a script or other object into the BackScripts
. This is equivalent to the insert <object> into the BackScripts
command but is simpler and more readable.
Syntax:
start using object
insert object into back
insert object into the BackScripts
Example:
start using "Folder1/Helper1" // Puts Helper1 (from Folder1) into the BackScripts