Key Elements of the Language
SenseTalk in a Nutshell
SenseTalk is a powerful, high level, and easy-to-read language. It is designed to be similar to English, avoiding cryptic symbols and rigid syntax when possible, in order to be both easy to read and to write. This section briefly describes many aspects of the language.
The information presented here is intended to provide a quick overview of most of the key elements of SenseTalk. Experienced scripters and programmers may find this is enough to get them started using SenseTalk. Other users may want to skim over this section to get a quick sense of what’s ahead before moving on to the more detailed explanations in following sections.
You may also find this section to be a convenient place to turn later when all you need is a quick reminder of how things work. For each topic, references are given to the section where more complete information may be found.
Scripts
A SenseTalk script is a series of command statements. This is often stored as a text file on your computer. When the script is run, each statement is executed in turn. Commands usually begin with a verb, and are each written on a separate line.
One of the first things you will notice about SenseTalk as you write scripts is the colorization. The script is color-coded indicating what the various parts of the script are. For instance, a variable will have a different color than a string literal. Bold and italic are also used to differentiate the different components of a script. There are multiple script themes available, and the styling can also be fully customized in the Eggplant Functional Script Preferences Theme Pane.
SenseTalk is not case-sensitive; commands can be written in uppercase, lowercase, or a mixture without changing the meaning.
Example:
Put 7 Into days
multiply Days by 4
PUT DAYS -- Prints "28"
(See Script Structure and Control Flow)
Simple Values
In SenseTalk there are simple values.
Examples:
5 -- number
sixty-four -- number expressed in words
"❤ hello ❤" -- text string (full international Unicode text allowed)
@"Hello\nNew\nWorld" -- text string with escape codes
empty -- constant
0x7F -- hexadecimal number
<3FA64B> -- binary data
(see Values)
Text Blocks
Multi-line blocks of text may be enclosed in {{
and }}
. This type of text block is particularly useful for dynamically setting the script of an object, or for defining a block of data.
Example:
set names to {{
Harry Potter
Hermione Granger
Ron Weasley
}}
(see Values)
Tech Talk
Text blocks can use labels, similar to "here is" blocks in some other languages. This also allows them to be nested.
Operators
Operators combine values into expressions. A full range of common (and some uncommon) operators is available. A put
command with no destination displays the value of an expression.
Example:
put 3 + 2 * 7 -- Prints: 17
put five is less than two times three -- Prints: true
put "a" is in "Magnificent" -- Prints: true
put 7 is between 5 and 12 -- Prints: true
put "poems/autumn/InTheWoods" split by "/" -- Prints: [poems,autumn,InTheWoods]
Example:
Parentheses can be used to group operations.
put ((3 + 2) * 7) is 35 -- true
(see Expressions)
Concatenation
Text strings can be joined (concatenated) using &
or &&
. The &
operator joins strings directly; &&
joins them with an intervening space.
Example:
put "red" & "orange" -- "redorange"
put "Hello" && "World" -- "Hello World"
(see Expressions)
Value assignment
Values can be stored in containers. A variable is a simple container. Either the put into
or set to
command may be used to assign a value to a container.
Example:
put 12 into counter
set counter to 12
Variables are created automatically when used; they do not need to be declared first.
(see Containers)
The Put Command
The put
command can also append values before or after the contents of a container.
Example:
put 123 into holder -- "123"
put "X" before holder -- "X123"
put "Y" after holder -- "X123Y"
(see Containers)
Typeless Language
SenseTalk is a typeless language. Variables can hold any type of value.
Example:
put 132 into bucket -- bucket is holding a number
put "green cheese" into bucket -- now bucket holds a text string
Values are converted automatically as needed.
Example:
put ("1" & "2") / 3 -- 4
(see Expressions)
Unquoted Strings
Any variable that has not yet had a value stored into it will evaluate to its name. In this way, they can be used as unquoted strings, which can be convenient.
Example:
put Bread && butter -- "Bread butter"
(see Containers)
Constants
Some words have predefined values other than their names. These are commonly called “constants” but SenseTalk tries to allow you maximum freedom to use any variable names you choose, so only a very few of these are truly constant; the others can be used as variables and have their values changed.
The actual constants include true
, false
, end
, empty
, and return
.
Example:
put street1 & return & street2 & return & city into address
if line 2 of address is empty then delete line 2 of address
The predefined variables include numbers and special characters like quote and tab.
Examples:
put 2*pi -- 6.283185
add one to count
put "Edward" && quote & "Red" & quote && "Jones" -- Edward "Red" Jones
put comma after word 2 of sentence
(see Values.)
Comments
Comments can add descriptive information. Comments are ignored by SenseTalk when your script is running. The symbols --
(two dashes in a row), —
(an em dash), or //
(two slashes) mark the remainder of that line as a comment.
For longer (or shorter) comments you may enclose the comment in (*
and *)
. This technique is sometimes used to temporarily turn off (or “comment out”) part of a script. These "block comments" can be nested.
Example:
This script adds two numbers and returns the sum. In-line comments are notated using the two dash notation (--
).
params a,b -- this line declares names for the two parameters
return a+b -- returns the sum (that's all there is to it!)
Example:
This larger block of code is commented out using the parentheses and asterisks notation (*
and *)
. These are nested.
(*
put "the total so far is : " & total -- check the values
put "the average is : " & total / count (* a nested comment *)
*)
(See Script Structure and Control Flow)
Chunk Expressions
A chunk expression can be used to specify part of a value.
Examples:
put word 2 of "green cheese" -- "cheese"
put item 3 of "one,two,three,four" -- "three"
put lines 1 to 3 of bigText -- the first 3 lines
put the first 3 lines of bigText -- also the first 3 lines
put any character of "abcdefg" -- one letter chosen at random
Negative numbers count back from the end of a value:
Examples:
put item -3 of "one,two,three,four" -- "two"
put chars 2 to -2 of "abcdefg" -- "bcdef"
Chunks of containers are also containers (you can store into them).
Example:
put "green cheese" into bucket -- bucket contains "green cheese"
put "blue" into word 1 of bucket -- bucket now contains "blue cheese"
put "ack" into chars 3 to 4 of word 1 of bucket -- bucket now contains "black cheese"
(see Chunk Expressions)
Tech Talk
SenseTalk's chunk expressions provide capabilities similar to substring functions or slices in other languages, but are much more expressive and powerful.
Lists
You can create a list of values by merely listing the values in square brackets [
]
separated by commas.
Lists are typically enclosed in square brackets [], but can also be enclosed by parentheses for historical reasons.
Examples:
put [1,2,3]
put ["John",67, "555-1234", cityName]
put ["bread", "milk", "tofu"]
Lists may include any type of value, including other lists.
Example:
put ["Mary", 19, ["555-6545", "555-0684"], "Boston"]
Lists can be stored in containers.
Example:
put [2,3,5,7,11,13] into earlyPrimes
(see Values)
List Items
Items in a list can be accessed by number. The first item is number 1.
Example:
put item 1 of ["red", "green", "blue"] -- Prints: "red"
put the third item of ["c","d","e","f","g"] -- Prints: "e"
List items in a container are also containers (they can be stored into).
Example:
put [12,17,32] into numbers -- numbers is: [12,17,32]
put 99 into item 5 of numbers -- numbers is now: [12,17,32,,99]
add 4 to item 2 of numbers -- numbers is now: [12,21,32,,99]
(see Chunk Expressions)
Combining Lists
Lists can be joined using &&&
.
Example:
put ["red", "green", "blue"] into colors
-- The colors variable contains: ["red", "green", "blue"]
put [12,17,32] into numbers
-- The numbers variable contains: [12,17,32]
put colors &&& numbers into combinedList
-- The combinedList variable contains: ["red","green","blue",12,17,32]
To create a list of nested lists instead of combining into a single list, just use parentheses to create a new list.
Example:
put ["red", "green", "blue"] into colors
-- colors contains: ["red", "green", "blue"]
put [12,17,32] into numbers
-- numbers contains: [12,17,32]
put colors &&& numbers into combinedList
-- combinedList contains: ["red","green","blue",12,17,32]
put [colors,numbers] into nestedList
-- nestedList contains both the colors and numbers lists:
-- [["red","green","blue"], [12,17,32]]
(see Expressions)
Property Lists
A simple object or property list is a collection of named values (called properties). Each value in an object is identified by its property name.
Property lists are typically enclosed in curly braces , but can also be enclosed by parentheses for historical reasons.
Examples:
put {size:12, color:blue} into myJeans -- Property names in this list are "size" and "color".
put {name:"John", age:67, phone:"555-1234", city:"Denver"} into GreatSinger -- Property names in this list are "name", "age", "phone", and "city".
Objects can be stored in containers.
Example:
put {size:8, color:pink} into description
(see Values, Lists, and Property Lists)
Tech Talk
SenseTalk's property lists are similar to collections known as hash tables, associative arrays, dictionaries or records in other languages. However, a SenseTalk property list is an object that can have behaviors as well as data values when certain special properties are set.
Object Properties
Properties in an object can be accessed by name.
Example:
put property width of {shape:"oval", height:12, width:16} -- Prints: 16