Knowing Your Equipment
As you embark on the journey of writing SenseTalk, there are a few basics you'll need to be familiar with. Here we cover some of the fundamental equipment you'll need, from text and lists to loops and conditionals.
Working with Text
SenseTalk's text handling abilities are extensive and extremely powerful, and at the same time very readable and people oriented.
Text operators include:
&
for joining (concatenating) two strings&&
for joining two strings with a space in betweencontains
,is in
for determining if one string contains anotherbegins with
,ends with
for determining if a string begins or ends with anothermatches
for determining if a string matches a given pattern
Chunk Expressions
SenseTalk's chunk expressions let you work with text in a natural way. A chunk expression can directly identify characters, words, items (delimited by commas, by default), or lines of text, either for accessing or modifying part of a string.
put characters 3 to 9 of acctNum into branchNum
put the first 3 chars of word 2 of "library catalog" --> "cat"
A chunk of a container is also a container, meaning that it can be modified by any command that operates on variables or other containers.
set text to "my flower are beautiful"
put "s" after word 2 of text
put text --> "my flowers are beautiful"
add 1 to the last item of line 7 of file "counters.txt"
Patterns
SenseTalk's pattern language provides all the capabilities of regular expressions, but in a readable way.
set sentence to "Please use addresses of the form 42.212.86.x"
put every occurrence of <1 to 3 digits, then "."> in sentence --> ["42.","212.","86."]
Working with Lists
Lists can be created directly in a script, using ( )
, [ ]
, or { }
with items separated by commas (but square brackets are strongly encouraged for consistency).
[]
— an empty list[1,3,5]
— a list of 3 numbers("dog", "cat", [x,y])
— a list containing 2 strings and a nested sublist
Access individual items or sublists within a list by using the same syntax as for text chunks:
put item 4 of [a,b,c,d,e,f] --> d
put the last 3 items of of [a,b,c,d,e,f] --> [d,e,f]
In addition, SenseTalk supports ranges, which will generate values as needed when treated as a list:
set scoreRange to 101..1000
put item 4 of scoreRange --> 104
put the last 3 items of scoreRange --> [998,999,1000]
List commands include:
each
expressions to extract or operate on selected items (see Each Expressions)insert
for adding or inserting items (don't useput
for this. See the Insert Command)push
andpop
for treating a list as a stack (see the Push Command and Pop Command)push
andpull
for treating a list as a queue (see the Pull Command)shuffled
to randomize the order of a list (see the Shuffle Command)sorted
to get a list sorted in a particular order (see the Sort Command)
List operators include:
&&&
for concatenating two listscontains
oris in
for testing whether a list contains a value or sublistunique items
to get a list with duplicate values removedintersection
to get a list of values present in both of 2 listsunion
to get a list of values present in either of 2 listsexcluding
to get a list of values, leaving out those that are in another list
Important Global Properties
There are dozens of global properties which control various aspects of the runtime environment. For example:
the numberFormat
— controls the format of displayed numbers, including the number of decimal places displayedthe defaultStringEncoding
— controls the encoding used when reading and writing text filesthe assertionBehavior
— controls whether a failed assertion will throw an exception, log a warning, log an error, or pause the script executionthe itemDelimiter
— specifies the character(s) used as a delimiter between text items
All global properties must be preceded by the word the and may be changed at any time during script execution. Most will affect script operation until they are changed again, however some are considered "local" properties which will revert to their default value at the end of the current handler.
For more information, see Local and Global Properties.
Survival Tip: "The" Isn't Always Optional
When using of
to access a property of an object or property list, or to call a function, the word the
is optional:
put the length of "time" --> 4
put length of "time" --> 4
When calling a function with no parameters, the
is required if ()
are not used.
put parameterList() --> displays the parameters
put the parameterList --> displays the parameters
put parameterList --> displays the variable "parameterList"
When accessing a global property, the
is required.
put the itemDelimiter --> displays the global property
put itemDelimiter --> here "itemDelimiter" is a variable
Repeat Loops
All loops in SenseTalk start with a line beginning with the word repeat
and end with the line end repeat
. There are many types of repeat loops, including some corresponding to traditional loops in other languages like for loops (repeat for
), while loops (repeat while
, repeat until
), repeating with each value in a collection (repeat with
) and so forth. There are also some types of repeat loops not commonly found in other languages, such as repeating for a length of time, until a specific time of day, or repeating at least once while a condition is met. See Repeat Loops.
In repeat loops that iterate over a set of values, if a repeat variable is not given, the values being iterated over are assigned to the variable IT. The commands exit repeat
and next repeat
are similar to break or continue statements in C or other languages. The current iteration count is available within any type of loop as the counter
(also called the repeatIndex
).
Conditionals
A conditional can be written on a single line using if
, then
, and optionally else
.
if total < 100 then add 1 to total
Conditional blocks are written using if
, then
, and optionally else
, each on separate lines with blocks of code between, and terminated by end if
.
if balanceDue is greater than creditLimit then
add 1 to creditViolations
set message to “Overdue!”
else
set message to “Your account is up to date”
end if
Multi-case if statements provide the functionality of switch/case statements in other languages, in a clear and readable way.
if age …
… is less than 4 then set category to “toddler”
… is between 4 and 17 then set category to “youth”
… is at least 18 then set category to “adult”
end if
For more information, see Conditional Statements.