1. Read about to get and to set handlers in the "Beyond the Basics" chapter of Using OpenScript manual.
2. Try writing a Set handler to set the size of the green ellipse to the right.
3. Test the handler by typing:
set the objectSize of ellipse "green" to 2000, 500
from the command window.
To see one solution switch to Reader level and click on the Hint button.the Hint button.
Setting an Object's Size with a To Set Handler, cont.
green
objSize
xyList
[theSize
objSize
objSize
objSize
xyList
objSize
theSize
-- Get and Set handlers for the size property.
-- Place them in an object's script.
to set objectSize to xyList
get my bounds
set item 3 of it to item 1 of it + item 1 of xyList
set item 4 of it to item 2 of it + item 2 of xyList
set my bounds to it
to get objectSize
get my bounds
clear theSize
push item 4 of it - item 2 of it onto theSize
push item 3 of it - item 1 of it onto theSize
return theSize
"Hint"
buttonUp
buttonUp
Control Structures - Branching
Control Structures - Branching
If/then/else and Conditions/when/else are ToolBook's branching Control Structures. If/then/else provides two-way branching and Conditions/when/else provides many way.....vides many way.....
to handle buttonUp
ask "What's your name?"
if it is not "Cancel" then
set text of field "Name" to it
else
beep 10
end if
endep 10
end if
to handle buttonUp
request "How Hot?" with "Mild" or \
"Medium" or "Hot"
conditons
when it is "Mild"
set spice to 0
when it is "Medium"
set spice to 5
else
set spice to 10
If/then/else:
Conditions/then/else:
Implementing a Menu System, cont.
For this exercise, examine the script for the background, "Menu" (you'll have to go to a menu page in this book or type: edit the script of background "Menu" in the Command window).
Try adding a new choice to one of the existing menus and have it go to the beginning of this exercise (the previous page).
Since all the scripts for the menu are contained in the menu's background you can cut and paste a menu page into another book and all the scripts will go with it.
Control Structures - Looping
Control Structures - Looping
Step, While and Do/until are ToolBook's looping control structures:
Step - repeats
to handle buttonUp
step i from 1 to 10
set fieldName to "field " & i
clear the text of field fieldName
end1 seconds
end beep 10
end if
Step:
to handle buttonUp
put fasle into giveAccess
while not giveAccess
ask "Password"
if it is securityCode of this book then
put true into giveAccess
end if
While:
to handle buttonUp
pop objectList
show it
until objectList is null
endd"
if it is securityCode of this book then
put true into giveAccess
end if
Do/until:
The ToolBook system is an object also and has properties. They are called system properties. One of many useful system properties is the sysCursor property. This can be used to change the cursor shape.
1. The "Please Wait" button contains the script shown in the field at the bottom of the page. Look at the script and try to identify the commands that change the sysCursor property.
2. Switch to Reader level and click on the button to see how the cursor shape is changed.
3. Look up sysCursor in the OpenScript encyclopedia and edit the button's script to try some of the other cursor shapes....
The sysCursor Property
Please Wait
-- Shows setting the cursor
a different shape
default
buttonUp
buttonUp
default
Please Wait
-- Shows setting the cursor to a different shape
to handle buttonUp
set sysCursor to 4
pause 3 seconds
set sysCursor to default
Z \ \ ,
IT is a special variable used
as a default variable in
OpenScript. Whenever you
use a get command,
OpenScript places the
result in IT.
Ask and Request also store their results in IT.
IT can also be used in the Command window.
Type the following in the Command window (see the next topic on the Command window if you are unsure how to do this):
Get the text of field "Stuff"
put it
put it contains "brillig"
request it
request it
Stuff
T'was brillig and the slithy toves did gyre and ...eeeeeeeeeeeeeeeeee
Field Stuff
h B!
T'was brillig and the slithy toves did gyre and ...bal in the wabe
to handle buttonUp
get the text of field "stuff"
if it contains "brillig" then
end buttonUp
User-Defined Messages
An Object's Location
Handler Placement
Handler Placement
You can often simplify an application by grouping the handler's for similar objects higher up in the object hierarchy. The following exercise shows an example of placing a buttonUp handler on the page rather than duplicating it in a number of draw objects. handler on the page that check tttttttttttttttttttttttttt
Recursive Handlers
Recursive Handlers
Recursion is a powerful feature in which a handler can send a message to itself. The following example shows how to use a recursive handler to define the factorial function (a classic example of recursion).
This exercise will be done using the Command window and shows how you can use a user-defined property to remember an object's size.
1. Create the originalSize property; type:
Set originalSize of polygon "triangle" to
bounds of polygon "triangle"
This creates a user-defined property called "originalSize" containing the size of the triangle.
2. Display the value of originalSize; type:
put the originalSize of polygon "triangle"
3. Finally, select and resize the triangle then type:
set bounds of polygon "triangle" to
originalSize of polygon "triangle"
A Self-Correcting Object
triangle
-- This handler places the
{clicked
fields on
. If you
but
*will be cleared.
Glocation
objectFromPoint(
"Object Name"
Type"
,UniqueName"
buttonup
buttonup
Object Name
Object Type
Object UniqueName
Object Name
Object Type
Object UniqueName
location
< f
There is a script on this page that will put the name, type and unique name of an object you click on in the fields at the bottom of the page.
1. Switch to Reader level.
Use F3 or the Reader menu command. If you're not at Reader level the script cannot function.
2. Click on different objects on the page and observe the name, type and unique name.
Note: it will only show information about objects on the page. It will not show background information.
3. Switch back to Author level and create an object of your own. Try the previous two steps clicking on your new object.
Was the information what you expected???????
Object Names
Object Name
Nameeeeeeee
Object Type
Object UniqueName
Unique Name
Red Square
Diamond
Pie Slice
Button
Hi There!
Diamond
curveonle
irregularpolygon id 18 of page 19 of Book "C:\BBSBOOKS\OSLAB.TBK"
irregularpolygon
The factorial of a number is defined as the product of all the integers from 1 to the number. For example, the factorial of 4 would be:
factorial(4) = 1 x 2 x 3 x 4 = 24
One thing you might notice in this formula is that, since the factorial of 3 is 1 x 2 x 3, the factorial of 4 can also be defined as
factorial(4) = factorial(3) x 4
If fact, for any number, factorial(n) is just:
factorial(n) = factorial(n-1) * n
This is a recursive definition. The factorial function is defined in terms of itself.
Continued on the next page...
A Factorial Function
-- Clear the total fields
leaving
4count
/ext
totalSys
totalLocal
leavePage
leavePage
totalLocal
totalSys
count
In OpenScript there are two types of variables: system and local. System variables retain their value when a handler has finished and can be shared between handlers. Local variables are discarded.
In this example the two buttons, system and local, contain the scripts displayed below them. The scripts are identical except for the System declaration at the beginning of one of them compared to the Local declaration in the other.
Each time the one of the buttons is clicked the value in the variable count is incremented and displayed in the total field.
Switch to Reader level and try clicking on the buttons. How do they act differently???
Local versus System Variables
to handle buttonUp
system count
if count is null then
set count to 0
increment count
set the text of field "totalSys" to count
System
4count
"totalSys"
buttonUp
buttonUp
totalSys
count
System
totalSys
System Variable:
to handle buttonUp
local count
if count is null then
set count to 0
increment count
set the text of field "totalLocal" to count
Local
Zcount
"totalLocal"
buttonUp
buttonUp
totalLocal
count
Local
totalLocal
Local Variable::
. To k
Bounds is a built-in property of an object that contains coordinates of the upper left and lower right corners of the object's bounding rectangle. The size of the object (width and height) can be calculated by subtracting the left coordinate from the right and the top from the bottom. The size of an object can be set by adding the width and height to the left and top coordinates. In this exercise you'll try writing a Set handler to set the size of an ellipse. After adding the set handler to the object's script you should be able to type the following command in the Command window to change the object's size:
set the size of ellipse green to 800,300
and have its bounds change accordingly.
Continued on the next page.
1. Read about to get and to set handlers in the Using OpenScript manual.
2. Following the guidelines above write handlers
Setting an Object's Size with a Set Handlerrrr
butto
The object in this example is to write a keyChar handler to allow only digits to be typed in the Number field shown to the right.
1. Look up the keyChar message in the Beyond the Basics chapter of Using OpenScript.
2. Try to write a keyChar handler in the script of the field that allows only digits to be typed.
Hint: in the handler, if a character from 0 to 9 is typed, the message should be forwarded. Otherwise, the handler should beep and do nothing. Test the handler by switching to Reader level and typing in in the field.
To see one solution, switch to Reader level and click on the Hint button.
Verifying Input Data with a keyChar Handler
to handle keyChar key
get ansiToChar(key)
if key < 32 or it is in "0123456789" then
forward
else
beep 5
end if
"Hint"
buttonUp
buttonUp
Number:
The Conditions/when/else control structure allows you to accomodate many conditions in one structure.
1. Create a button.
2. Edit the button's script to:
Use the Request command to request a color: Light, Medium or Dark. Then using the Condition control structure, set the button's fillColor to White, Cyan or Red according to the chosen value.
For one solution switch to Reader level and click on the hint button.
3. Try the button out.
Branching with Conditions
to handle buttonUp
request "Which color?" with "Light" or \
"Medium" or "Dark"
conditions
when it is "Light"
set my fillColor to White
when it is "Medium"
set my fillColor to Cyan
when it is "Dark"
set my fillColor to Red
enddddd
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
Another of an object's properties is its bounds. The bounds of an object is a list of four numbers representing the (x,y) coordinates of the upper left corner and lower right corner of a rectangle bounding the object. In this exercise you'll write a handler to change the bounds of the smiley face object.
1. Look at the script of the "Flat Face" button. Also look at the script of the "Round Face" button.
Select the button and use the Button Properties command or press the Ctrl key and double-click the button with the selection tool. Notice the command that changes the bounds of the "Smiley" group.
2. Switch to Reader level and click on both the buttons.
k on a button to try it out.button to try it out.
Changing an Object's Bounds
-- Returns the smiley face
its original
"Smiley"
5776, 837, 7998, 3162
buttonUp
buttonUp
Smiley
Round Face
-- Returns the smiley face
its original
"Smiley"
5776, 837, 7998, 1500
buttonUp
buttonUp
Smiley
Flat Face
Smiley
Changing the menu bar by adding or deleting menus and menu items changes the menu until you change it back--even if you open another book. The way to restore the menu bar to the ToolBook default is to use the Restore Menubar command.
1. Type the following command in the Command window:
restore menubar at both
A good way to ensure that your menu bar is restored whenever you open a new book is to add a leaveBook or leavePage message handler to the script of any book that changes the menu. See the handlers on the right for examples.
Restoring Menus
-- leaveBook handler to restore the menu
-- bar when going to another book.
to handle leaveBook
restore menuBar at both
-- leavePage handler to restore the menu
-- bar when going to another page.
to handle leaveBook
restore menuBar at both
Topic
The If/then/else control structure allows you to test a condition and do different things as a result. In this exercise you will use the If/then/else to test the results of an Ask command.
1. Edit the "Attendance" field's script and create a buttonUp handler. In the handler, ask for the "Attendance". Check the result to see if it is between 1 and 10. If it isn't, beep. If it is, set the text of field "Attendance" to the value.
If you want one answer, switch to reader level and click on the "Hint" button. (clicking again will cause it to disappear).
3. Try the script out.
Remember, you have to activate the field's script and switch to Reader level.
Branching with If/then/else
Attendance
Attendance
to handle buttonUp
ask "Attendance?"
if it >0 and it < 11 then
set the my text to it
else
beep 20
end if
enddddddddddddddddddddddd
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
Containers
Special Effect Commands
To Get / To Set Handlers
The objective in this exercise is to write a handler to list all the objects on a page, even if they are nested in groups.
The scrolling field to the right is named "Object List". Try writing a handler for that field so that clicking on the field will list the objects on the page.
If you want to see one solution, switch to Reader level and click on the Hint button (you can cut and paste this script into the field's script if you want to try it and make modifications).
The basic listing can be modified in numerous ways: you could list out more information about object properties for each object; you could indent groups; and so on.
A Recursive Object Lister
blank hint
to handle buttonUp
clear my text
send listObjects (objects of this page)
end buttonUp
to handle listObjects x, place
set syscursor to 4
step i from 1 to itemcount(x)
get item i of x
put object of it & ", " & quote & name of it & \
quote & CRLF after my text
if object of it is "group"
send listObjects (objects of it)
end
set syscursor to default
end listObjectsfault
end listObjectssects
objectList
"Hint"
buttonUp
buttonUp
Geometrics
Square
Triangle
Object List:
Objects
Objectssssssssss
A ToolBook book consists of objects--buttons, fields, graphics, pages, and others. An author defines the objects, their properties and behavior. The book's reader interacts with the objects.
As an example, this page consists of a background object, a page object, record fields (for text), a button, and various graphic objects.....
Objects
Hide and Show Commands
Visual Effects
Properties
Properties
Objects have properties. By changing their properties you can change their appearance and by changing their script property you can change the way they behave.
The following exercises show you some examples of changing properties.
Properties
Recursive Handlers
You can place multiple commands in the Command window at the same time by separating them with semicolons.
1. Display the Command window.
press Shift+F3 if it isn't already visible.
2. Type the following command and press Enter when you're finished:
step i from 1 to 100; move ellipse dot by 10,10; end
3. What command would you type to move the dot back to its starting point???????ng point?
Multiple Commands on a Line
-- Clear the messages
leaving
"Messages"
leavePage
leavePage
Messages
There are numerous messages sent to an object as a result of mouse movement. The messages are:
mouseEnter mouseLeave
buttonDown buttonUp
buttonStillDown buttonStillUp
buttonDoubleClick rightButtonDown
rightButtonDoubleClick rightButtonDown
1. Switch to Reader level (mouse messages aren't sent at author level).
Press F3.
2. As you move the mouse over and click on the magenta rectangle the messages sent to the rectangle will be displayed in the Messages field. Try to display each of the ones listed above.
Mouse Location and Button Messagess
Messages
checkScroll
"messages"
) > 500
" & CRLF
{" &
|" &
}" &
z" &
R" &
S" &
Q" &
buttonDown
buttonStillDown
mouseEnter
buttonUp
mouseLeave
buttonDoubleClick
rightButtonDown
checkScroll
rightButtonUp
rightButtonDoubleClick
checkScroll
messages
mouseEnter
mouseEnter
messages
checkScroll
mouseLeave
mouseLeave
messages
checkScroll
buttonDown
buttonDown
messages
checkScroll
buttonStillDown
buttonStillDown
messages
checkScroll
buttonUp
buttonUp
messages
checkScroll
buttonDoubleClick
buttonDoubleClick
messages
checkScroll
rightButtonDown
rightButtonDown
messages
checkScroll
rightButtonUp
rightButtonUp
messages
checkScroll
rightButtonDoubleClick
rightButtonDoubleClick
messages
checkScroll
Messages:
Object Drawing and Display
Object Drawing and Display
OpenScript has commands to draw objects as well as manipulate them in various ways. The following exercises look at drawing objects using drawing tools, hiding and showing objects, and moving objects.
Navigation and Visual Effects
Navigation and Visual Effects
OpenScript has commands that allow you to navigate between pages and book and commands that provide different visual effects when switching pages. The following exercise looks at page navigation as well as the three special effects ToolBook provides.
Get and Set Handlers
Types of Objects
A Self-Sorting Field
A Self-Sorting Field
One of the powerful things about ToolBook's object-oriented apporach is that you can design objects with scripts and handlers that can be "cut and pasted" into other applications. An example of this is a field that has the script to sort itself. Such a field could be pasted into another application ready to go. The following exercise shows you how to create a self-sorting field.
Implementing a Menu System
Implementing a Menu System
If you're designing a book to present information that follows a hierarchical organization, one way to allow a user to navigate to topics is through a set of hierarchical menus. This exercise book uses menus.
In the following exercise you can examine how menus were designed for this book and try to creating your own menus in another book.
Implementing a Menu System
Name versus ID
Containers - Variables
Listing All Objects on a Page
Listing All Objects on a Page
Books, pages, backgrounds, groups, etc. all have an Objects property. The value of the property is a list of all the objects of the book, page, and so on. It is fairly easy to get the value of the Objects property and list all the items. A complication arises when one of the objects is a group. The group itself contains other objects. One solution is to use a recursive handler. The following exercise shows how to write a handler that lists the object type and name of objects on a page..a page.. of objects on a page........
OpenScript Exercises
disclaimer
IMPORTANT: These ToolBook files and Dynamic Link Library files are being made available free of charge exclusively for demonstration purposes to show some approaches programmers are taking for applications using ToolBook. The files have not undergone the rigorous review process which Asymetrix's commercial products undergo. Asymetrix does not intend to provide support for these files. Asymetrix specifically disclaims all warranties including without limitation all warranties of title or non-infringement, warranties of merchantability or fitness for a particular purpose and all other warranties express or implied. The files are made available "as is."
ASYMETRIX SHALL NOT BE LIABLE FOR ANY DAMAGES INCLUDING BUT NOT LIMITED TO DIRECT, INDIRECT, INCIDENTAL, SPECIAL, COVER, RELIANCE, OR CONSEQUENTIAL DAMAGES ARISING FROM ITS MAKING THE FILES AVAILABLE, OR THE USE OR INABILITY TO USE THE FILES. ANY USE OF FILES IS STRICTLY AT YOUR OWN RISK.
Copyright (c) 1990 by Asymetrix Corporation. All rights reserved. Asymetrix, ToolBook, and OpenScript are registered trademarks of Asymetrix Corporation.
1. Create a button and label it "Get First Name".
Use the button tool.
2. Edit the button's script and type:
to handle buttonUp
ask "Type your first name" with "Sigmund"
set the text of field "First Name" to it
end buttonUp
3. Switch to Reader level and click on the button.
What happens when you click on the cancel button? What happens if you remove the "with "Sigmund"" from the Ask command?he cancel button? What happens if you remove the "with "Sigmund"" from the Ask command?
Using the Ask Commanddddd
First Name
Field: First Name
Structure of a Handler
A handler has three parts: the beginning, middle and end.
In the following exercise you will write a simple handler for a buttonUp message.ritten using OpenScript. The end is a line identifying the end of the handler.
In the following exercise you will
write a simple buttonUp handler.you will write a simple buttonUp handler.
-- Changes to reader level when
-- page is entered.
to handle enterPage
send Reader
end enterPagerPageeeeee
Beginning: identifies the name of the message this handler is for.
Middle: contains instructions in OpenScript saying what to do when the message is received.
End: marks the end of the handler
OpenScript Topics
Special Effects
-- Menu Background
-- To
cchoice:
)Author level
}click
bullets
type
must be the
clicked.
-- To
c: create a
/. At
ctitle
ilarge diamond
enter a
-- Change
Reader
entering
-- If
mouse
over a
handler
color
5. Otherwise
forwards
}message.
c"Choice"
-- This
}branches
correct
Bwas pressed
released
same
sent
{that received
-- matching
, even
no longer
{. The objectFromPoint function returns
-- under
. If
goes
whose
. Before
pushes
current
^called menuStack.
menuPop
-- below pops
}location
can be
anywhere
Hsending the
"). If there are no
-- on
stack
buttonDown
enterPage
buttonUp
menuPop
enterPage
Reader
buttonDown
Choice
0,0,0
buttonUp
Choice
0,100,0
menuStack
location
menuPop
menuStack
Title
Choice1
choice
choice3
choice4
choice5
choice6
Go Back
-- Returns
Hpopping its
cstack.
menuPop
/"Menu"
buttonUp
buttonUp
o MmenuPop
Go Back
Click on a Topic
OpenScript Topics
OpenScript Topics
OpenScript Concepts
OpenScript Commands
Advanced OpenScript Topics
OpenScript Applications
OpenScript Concepts
OpenScript Concepts
Programming with ToolBook
Objects
Messages
Properties
Handlers
Containers
ow, click
OpenScript Commands
OpenScript Commands
The Command Window
Ask and Request and Effectsomm
Control Structures
Navigation and Visual Effects
Object Drawing and Displaysssssssssss
Menuslllllll
Advanced OpenScript Topics
Advanced OpenScript Topics
User-Defined Properties
User-Defined Messages
Handler Placement
Recursive Handlers
Handling Key Strokes
Get and Set Handlersssss
OpenScript Applications
OpenScript Applications
A Self-Sorting Field
Listing all Objects on a Page
Implementing A Menu System
OpenScript Applications
Drawing Objects
Programming with ToolBook
Control Structures - Branching
Menu System Exercise
Developing ToolBook Applications
Developing a ToolBook application usually begins with the user interface. You can lay out the buttons, fields and other objects, then develop scripts to make them behave the way you want. While traditional program development tends to be linear, ToolBook development is more flexible and modifications can be made and tested readily.
Handlers
Handler Placement
While
Object Names
Navigation and Visual Effects
The Command Window
Control Structures - Looping
An Object's Unique Name
Containers
Containers
Containers is the generic term to refer to everything in OpenScript that can hold a value. Containers can be properties, variables, and the special variables IT and sysError.
The following exercises demonstrate using local versus system variables and the special variable IT. Properties are covered in their own topic.. topic.
Adding a menu
In this exercise you will see a script that alters how ToolBook responds to the Author menu selection.
1. Switch to Reader level and choose Author from the Edit menu.
Notice what happens.
1. Edit the script of this page and add the handler displayed to the right.
2. Change to Reader level again and choose Author from the Edit menu.
What's different this time?
When you choose a menu item ToolBook sends a message with a name the same as the menu item to the current page. If you wanted
Handling Standard Menu Choices
script1
to handle author
request "Do you want to switch to Author level"\
with "Yes" or "No"
if it is "Yes"
forward
end if
end author
e enterBook
--positions the workbook window
set syslevel to author
send sizeToPage
end enterBook
Author Handler
Hiding and showing
In this exercise you will write scripts to hide and show a field that contains a definition for a hotword.
1. Select the bold word "score" in the text and create a hotword from it.
Use the Create Hotword command from the Text menu or Ctrl-W
2. Edit the script of the hotword creating a buttonUp handler that shows the field called "score" (the white field).
3. Edit the script of field "score" to hide itself (you can use the special term "self" in the hide command).
4. Switch to Reader level and try your scripts. d
Show and Hide: Making a Popup Definitiondddddddddddddd
hotwords
Four score and seven years ago, our fathers brought forth a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.
score
A score is twenty years. "Four score and seven" is eight-seven years.""ight-seven years."
-- Handler for the hotword
to handle buttonUp
show field score
end buttonUp
-- Handler for the popup definition
to handle buttonUp
hide self
end buttonUp
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
Introduction
enterPage
enterPage
reader
buttonDown
buttonUp
buttonDown
0,0,0
buttonUp
0,100,0
buttonDown
buttonUp
buttonDown
0,0,0
buttonUp
0,100,0
Using command window
1. Display the Command window.
Press Shift+F3 if it isn't already displayed.
2. Type each of the following commands in the Command window. Press Enter after each one.
select ellipse dot
unselect ellipse dot
put position of ellipse dot
move ellipse dot by 100,100
set the fillColor of ellipse dot to green
set bounds of ellipse dot to 300,300,1300,1300
edit the script of ellipse dot
You can find out more about these commands in the Using OpenScript manual........ You can find more information on each command in the Using OpenScript manual."""""""""""""""""""""
Working with Objects from the Command Window
buttonUp
buttonDown
buttonUp
60,50,100
buttonDown
240,50,100
The script to the right shows an example of adding a custom menu and menu items.
1. Enter the script to right into the script of this page.
An easy way to do this is to type the following in the Command window:
set script of this page to text of field script1
2. Install the menus by sending a setupMenu message to this page.
Send the setupMenu message by typing the following in the Command window:
send setupMenu
3. Try the new menu items.
See the next exercise to restore menubars.
pt menu.
5. Switch back to Author level.
Adding Menus and Menu Items
script1
originalFill
buttonUp
buttonUp
180,50,100
originalFill
to handle setupMenu
add menu"Script" at both
add menuitem "Object" to menu "Script" at both
add menuitem "Page" to menu "Script" at both
add menuitem "Book" to menu "Script" at both
deactivate menuitem "Object" at Reader
end setupMenu
to handle Object
if selection is null
request "Please select an object first."
else
edit the script of the selection
end if
end Object
to handle Page
edit the script of this page
end page
to handle book
edit the script of this book
end book
to handle leavepage
restore menubar at both
end leavepageeeeeeeeeeeeenubar at both
end leavepageepage leavepageepageleavepageepageepageepageepageepageeeeee
The UniqueName
An object's uniqueName identifies the object, page and book. It the object is in other than the current book the book's file name is included in the Unique name.
The following example shows you the name, object type and unique name of several common objects.
Alaska
Advanced OpenScript Topics
Refering to an Object
script window shortcuts
Another of an object's properties is its pattern property. This is the pattern used to fill filled shapes.
1. Look up the Pattern property in the OpenScript encyclopedia chapter in the Using OpenScript manual.
1. The button, Watch, contains the script displayed at the bottom of this page. Examine the script and Identify the line that sets the pattern property.
2. Switch to Reader level and click on the Watch button.
3. Edit the button's script and add a line so that the screen returns to white after finished displaying the other patterns.erns.
The Pattern Propertyyyyyyyyyyyyyy of
script1
-- Changes the pattern property of a rectangle
-- to all possible patterns.
to handle buttonUp
step i from 1 to 128
set pattern of rectangle "boob toob" to i
endddddddddddddd
Boob Toob
Watch
-- Changes the
property
possible patterns.
"boob toob"
buttonUp
buttonUp
boob toob
Watch
field
Topic
ction
Reader
enterPage
enterPage
Reader
ButtonShadow
Topic
buttonDown
buttonUp
buttonDown
0,0,0
buttonUp
0,100,0
buttonDown
buttonUp
buttonDown
0,0,0
buttonUp
0,100,0
-- Returns
Hpopping its
cstack.
menuPop
/"Menu"
buttonUp
buttonUp
o MmenuPop
script window shortcuts
Moving objects
User-Defined Properties
abcdefghijklmno
Handlers
Handlerssandlerss
When an object receives a message it can respond to it, or ignore it (in which case the message gets passed on to the group, page, background, or book that contains the object).
In order to respond to a message, an object must have what's called a "handler" in its script.
ed by instructions in OpenScript. At the end is a line identifying the end of the handler. In the following exercise you will write a simple buttonUp handler.
Here's another example of using the Step control structure.
1. Edit the script of the "Clear Names" button.
2. Create a script to clear the names of the fields. The fields are called Name1, Name2, etc.
For one solution, switch to Reader level and click on the Hint button.
Another Example of the Step Loop
Name1
Name6
Name5
Frank
Name4
Name3
Henderson
Name2
Bertrand
Clear Names
to handle buttonUp
step i from 1 to 6
set fieldName to "Name" & i
clear the text of field fieldName
endddd1 seconds
end beep 10
end if
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
exercise
Introduction
Defining own message
Hiding and showing
Containers - Properties
Messages
Object Drawing and Display
Menus
Creating objects
Setting text
Using command window
Control Structures
Ask and Request
Types of Objects
Types of Objects
There are different types of objects:
Books Record fields
Backgrounds Hotwords
Pages Groups
Graphic Objects
Buttons
Fields
1. Create a rectangle on this page.
Use the rectangle tool on the tool palette.
2. Edit the script of the rectangle.
Use the Graphic Properties dialog box or press the Ctrl key while double-clicking the rectangle with the selection tool.
3. Enter and save the following script:
to handle buttonUp
beep 20
end
4. Switch to Reader level and click on the rectangle.
What messages did clicking on the rectangle send? Which one caused the rectangle to beep? What happened to the other message?
Writing a Simple buttonUp handler
Refering to an Object
Referring to an Object
In writing scripts you often have to refer to an object, maybe to hide or show it, or change it in some way. To refer to an object you use a combination of the object's type (field, button, etc.) and the object's name or ID. If the object isn't on the current page you may also have to add location information. As an example, to refer to the blue ellipse you would use:
ellipse "oval" or ellipse ID 0. nu" of background "Topic".
1. Display the Command window.
Press Shift+F3 if it isn't already visible.
2. Type the following commands in the Command window:
hide rectangle box
show rectangle box
hide menubar
show menubar
hide toolpalette
show toolpalette
move toolpalette to 100,100
3. What commands would you type to hide and show the Pattern palette? The Color Tray?
Hint: If you don't know, try looking in the Using OpenScript manual.
Hiding and Showing Objects from the Command Window
1. Display the Command window.
Press Shift+F3 if it isn't already visible.
2. Type the following commands:
set text of field "thatfield" to text of field "thisfield"
clear text of field "thisfield"
set text of field "thisfield" to "Hi there!"
put the third word of text of field "thatfield"
put the last word of text of field "thisfield"
put words 4 to 7 of text of field "thatfield"
3. How would you refer to the third through thirteenth characters of field thatfield?"
3. How would you refer to the third through thirteenth characters of field thatfield?
Setting and Getting Field Text in the Command Windowow
thisfield
The text of a field is one of its properties just like its border style. and can be set with an OpenScript statement.
thisfield
thatfield
thatfield
Programming with ToolBook
Traditional Programming
The program controls the user by requesting input and displaying output..
PRINT "What's your name?";
INPUT Name$
PRINT "Hi, "; Name$
PRINT "Enter a number: ";
INPUT Num
PRINT "The square root of "; Num; " is: "; SQRT(Num)
FUNCTION Fact(n)
f = 1
FOR i=2 to n
f = f*i
NEXT i
Fact = f
END FUNCTIONNNNNNNNNNNNNNNN
What's your name? Sue
Hi, Sue
Enter a number: 4
The square root of 4 is 2
ToolBook Programming
The user controls the program. Everything the user does (like clicking on a field or button) sends a message to an object which runs a "handler" to respond..d.................
Number:
number
("What
buttonUp
buttonUp
What number?
to handle buttonUp
get text of self
ask "Type a number" with it
if it is not "cancel" then
set the text of self to it
end buttonUp
Calculate
"answer"
buttonUp
buttonUp
number
answer
answer
Calculate
to handle buttonUp
get text of field "number"
clear text of field "answer"
if it is not null then
get sqrt(it)
set text of field "answer" to it
end buttonUp ndld "answer"
Answer
Square Root:is:::::
Name versus ID
Name versus ID
You name an object in the object's "Properties" dialog box. If an object has a name you can use it to refer to the object. If an object doesn't have a name you can refer to it using the object's ID. The ID is assigned by ToolBook and is unique for each object.
It is usually best to refer to objects by name. When an object is cut and pasted its ID changes so references to the old ID will be invalid. The name is unchanged.e. object and the ID changes.
An Object's Location
An Object's Locationame
If the object you're refering to isn't on the current page you may have to add location information to refer to it. For example to refer to a button on the background you might say:
button "Next Page" of background "Database"
he following example shows you the namess of
Ask and Request
Ask and Request Commands
The Ask and Request commands are useful for getting information or choices from a user. The Ask command asks the user to enter some text. Whatever the user types is placed in the container, IT.
The Request command displays a message and asks the user to click a button in response. The name of the button clicked is returned in the container, IT.
r, IT....on in the container, IT.
Handling Key Strokes
Handling Key Strokes
Sometimes you want to give a user feedback on characters they are typing at the keyboard. You can do this with a keyChar handler. One common application of the keyChar handler is to check data as it's being input to make sure it is valid for the field.
The following exercise shows you how to write a keyChar handler to make sure data is numeric.ger
1. Create a button. Label it "Decision"
Use the button tool in the Tool palette.
2. Edit the button's script, typing the following:
to handle buttonUp
request "Save humanity or not?" with \
"Save" or "Destroy"
set text of field "fate" to it
end buttonUp
4. Switch to Reader level and click on the button.
Try this a couple of times making different choices. Which choice is default? Try adding a third choice, "Neither" (Remember, you can look in the Using OpenScript manual). What's the difference between Ask and Request?????????????sing OpenScript manual).
Using the Request Command
Neitherbout it
Field: fate
Get and Set Handlers
Get and Set Handlersssss
Most commonly, a user-defined property is used to store a value. By defining To Get and To Set handlers for an object, though, a user-defined property can be much more powerful.
The following exercise shows you how you can define a "Size" property for an object such that setting its size will cause its bounds to change..
To create a self-sorting field, write a handler in the field's script to handle a user-defined "sort" message, say sortLines. Then to sort the lines of the field simply use the command:
send sortLines to field fieldname
where fieldname stands for the name of the field. So, for this exercise:
1. Create a field.
2. Write a sortLines handler for the field that sorts the field's lines.
Remember the terms my, and self can be used to refer to the object containing a script. If you want to see one solution, switch to Reader level and click on the Hint button....l and click on the Hint button.....................
A Self-Sorting Field
"Hint"
buttonUp
buttonUp
-- Handler to sort a field. The handler must be
-- located in the field's script and is activated by
-- sending a sortLines message to the field.
to handle sortLines
get my text
set N to textLineCount(it)
clear my text
step i from N to 2 by -1
step j from 1 to i-1
if textLine i of it < textLine j of it as text
set temp to textLine i of it
set textLine i of it to textLine j of it
set textLine j of it to temp
end if
end step
end step
set my text to it
enddd
if/then/else
OpenScript Commands
Menu Control
A Self-Sorting Field
Navigation
Names and variables
Listing All Objects on a Page
fx commands
OpenScript Concepts
Conditions
Object Types
fx commands II
Do/until
Adding a menu
Handling Key Strokes
This book contains exercises and examples to help you learn to use OpenScript for writing scripts. The book is divided into topics each containing a mix of explanation and exercises. es. You can locate topics through the use of menus (starting at the end of this introductiond.
What this Book Contains
Topic
Explana-
tionnn
Exercise
Explana-
tionnn
Exercise
The Command Window
The Command WindowWindow
While OpenScript is used most often in writing scripts, it can also be used directly from the Command window to do things that would be difficult or impossible with a menu.dow to do things that would be difficult or impossible with a menu. following pages contain exercises using OpenScript and the Command Window...uld be difficult or impossible through a menu. The following pages contain exercises using OpenScript and the Command Window.
:PHYSSIZE
User-Defined Properties
User-Defined Properties
In addition to normal properties of an object you can create your own. The following exercise shows an example of using a user-defined property to remember the original bounds of an object.
User-Defined Messages
User-Defined Messages
Through the use of custom menus, message handlers and the Send command you can define your own messages and handlers. There is an example of user-defined menus in the section on menus. The following exercise shows a simple example of using a user-defined message and handler.
Menus
ontrol
Menuslllllll
Using OpenScript you can customize ToolBook's pull-down menus by adding or removing menus and menu items as well as changing the way ToolBook responds to existing menu selections.
Navigating in this Book
Following this introduction are menus you can use to locate a topic. You can also browse through topics page by page.
On pages like this, with arrow buttons at the bottom right, you can click on the arrows to change pages or the "Menu" button to return to the previously viewed menu. On exercise pages, ToolBook is automatically placed at Author level so you must use ToolBook menus or Ctrl+<arrow> to change pages.
A Final Note ....
The exercises in this book are just starting points. Feel free to pick and choose topics that seem interesting, explore on your own and make up your own examples.
Now, click on the forward arrow for a list of topics.ed you can view the scripts to see an example of scripts in an application.
Now, click on the forward arrow for a list of topics.....cs.
Messages
ToolBook Messageslers, Cont.
A user's actions cause
messages to be sent. Each
message has a name. For
example, clicking the mouse on
an object causes two messages
to be sent to the object:
buttonDown and buttonUp.
Button Message Example
-- This
does crude animation
-- two
Bmessages
example
doMouse
B"mouseClick"
"downMessage"
Move
6435, 2647
6390, 2557
6300, 2467
6210, 2332
6120, 2212
6045, 2122
5910, 1942
5745, 1852
5565, 1687
5430, 1597
"upMessage"
6435, 2647
6390, 2557
6300, 2467
6210, 2332
6120, 2212
6045, 2122
5910, 1942
5745, 1852
5565, 1687
5430, 1597
doMouse
doMouse
mouseClick
downMessage
button
mouseClick
upMessage
button
matte
button
inverse
false
Print Labels
arrow
mouseClick
inverse
upMessage
buttonUp
downMessage
buttonDown
-- This handler sends a doMouse message
the grouped
animate
example.
"Button Message Example"
buttonUp
buttonUp
FdoMouse
Button Message Example
Show Example
arning ToolBook
System
enScript Exercises
s Rmn
`D|D|
-- a
renumbering a
n > 0
enterBook
renumber
enterBook
sizeToPage
renumber
:REPORTDATA
:CONDITIONDATA
Text of RecordField "Instructions" is "null" As Text
Text of RecordField "Instructions" is null
OpenScript Lab Exercises
System
y`D|D|
`D|D|
Exercise Name
"gxp0
`D|D|
`D|D|
`D|D|
`D|D|
`D|D|
rminal
`D|D|
`D|D|
`D|D|
fault
9_D|D|
`D|D|
`D|D|
`D|D|
s Rmn
`D|D|
restore
restoreobjects
restoreobjects
restoreobjects
restoreobjects
paste
pageobjs
`D|D|
:PRINTLAYOUT
`D|D|
`D|D|
u`D|D|
`D|D|
("Exercise title?"
"Cancel"
NewPage
hName"
("Topic?"
newTopic
("Menu
J"Title"
movePages n, dest
M+i-1
erBook
author
enterBook
movePages
enterBook
sizeToPage
author
Exercise title?
Cancel
Exercise
y5NewPage
Exercise Name
Topic?
Cancel
Topic
y5NewPage
Topic
newTopic
Menu Topic?
Cancel
y5NewPage
Title
newTopic
movePages
exercise
enterPage
enterPage
author
Instructions
Exercise Name
To move to next page, use Ctrl+<right arrow>.........................................
Control Structures
Control Structures
OpenScript contains a range of control structures for both branching and looping within a script. The following pages show some examples of the branching and looping control structures. These are followed by exercises.
Defining own message
-- When you click on a
Reader mode,
{identified
's targetObject property (user-defined).
buttonUp
buttonUp
script
targetObject
180,50,100
0,100,0
In this exercise the button "Send message" sends a user-defined message to the rectangle. The rectangle, in turn, has a handler that responds to the message.
1. Type the following two commands in the Command window:
set script of rectangle peep to text of field script1
set script of button send to text of field script2
This will copy the scripts into the two objects
3. Switch to Reader level and click the "Send Message" button.
4. Create another button and add another handler to the rectangle to handle a message called "envy"..
User-Defined Messages
Send message
script1
targetobject
rectangle id 0 of page id 83
to handle blush
set my fillcolor to red
beep 10
set my fillcolor to white
end blush
script2
targetObject
button id 1 of page id 83
to handle buttonUp
send blush to rectangle peep
end buttonUpUp
-- Make sure we're
)Reader level
entering
-- This animates the world (a metaphysical thought). The "
-- consists
16 stacked
}named Earth 1,
2, etc.
-- showing
hearth
different stages
rotation.
handler
repeatedly whenever ToolBook isn't doing
-- anything
. Every
moves one
images
^, counter,
incremented
through
16 earths.
" &&
> 16
enterPage
enterPage
reader
Earth
earth
counter
OpenScript Exerciseskkkkk
-- This handler animates the "Earth" and
-- is placed in the script of this page. ToolBook
-- sends an idle message whenever it isn't
-- doing anything else.
to handle idle
system counter
if counter is null
set counter to 1
end
set earth to "Earth" && counter
set the layer of group earth to 20
increment counter
if counter > 16 then
set counter to 1
end
end idle
Earth 7
Earth 8
Earth 9
Earth 10
Earth 11
Earth 12
Earth 13
Earth 14
Earth 15
Earth 16
fJd *
Earth 1
lOM E
HRM F
Earth 2
Earth 3
Earth 4
Earth 5
Earth 6
Earth 6
Earth 7
Earth 8
JvG T
Earth 9
Earth 10
Earth 11
Earth 12
Earth 13
Earth 14
Earth 15
Earth 16
Earth 1
Earth 2
Earth 3
Earth 4
Earth 5
Creating objects
In this exercise you will write a script to draw a rectangle.
1. Edit the script of the button, "Blue Square"
2. Create a buttonUp handler to draw a rectangle from 6000,900 to 7500,2400.
For a hint, switch to Reader level and click on the Hint button.
3. Switch to Reader level and try the button.
4. Try the same for an ellipse, or polygon.
Drawing Objects from a Script
bluesquare
Blue square
to handle buttonUp
draw rectangle from 6000,900 to \
7500,2400
set the fillcolor of the selection to blue
end buttonUpppppppppppppppp
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
Menu System Exercise
Implementing a Menu System
Menus in this book were implemented as pages on the Menu background. The background script contains all the handlers necessary for navigating through the menus. To keep track of the menus as you go through them, the Push command is used to push the menu page name onto a stack variable. To return to a previous menu, the Pop command is used to get the previous menu's page from the stack.
To create a new menu, create a new menu page and fill in the title and choices fields in Author mode. The choices must be names of the page you want to branch to.
To return to the previously viewed menu send a menuPop message to the background, "Menu".
Continued on the next page.nu".
the background, "Menu".
Moving objects
In this exercise you will write a script to move
the red dot around the screen.
1. Edit the script of the button "Fun With Dot" and create a buttonUp handler to move the dot to the following points:
8000, 100
8000, 4000
6300, 2050
4600, 4000
4600, 100
Use any order you want. For one solution, switch to Reader level and click the Hint button.
Moving a Draw Object
funbutton
Fun With Dot
to handle buttonUp
move ellipse "dot" to 8000,100
move ellipse "dot" to 8000,4000
move ellipse "dot" to 6300, 2050
move ellipse "dot" to 4600,4000
move ellipse "dot" to 4600,100
end buttonUppttonUppppppppppppnUp
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
f/then/else
In this exercise there are three ellipses: "scoop", on top of the cone; "Vanilla", on top of the vanilla tub and "Chocolate", on top of the chocolate tub. The listing of the script in the white box shows how the color of the scoop ellipse (i.e. the fillColor property) can be set to the fillColor's of the flavors.
1. Look at the script in the white box. Which lines would change the fillColor property of the ellipse, scoop?
This is the script of the button labeled "Chocolate or Vanilla?"
2. Switch to Reader level and click on the button.
Try it again with the other flavor.ick on the button.
Try it again with the other flavor..hen switch back to Author level.
Changing an Object's Color Properties
scoop
vanilla
chocolate
Flavor
"What flavor would you like?"
"Chocolate"
"Vanilla"
"scoop"
buttonUp
buttonUp
What flavor would you like?
Chocolate
Vanilla
scoop
Chocolate
scoop
Vanilla
Chocolate
Chocolate or Vanilla?
Script1
to handle buttonUp
request "What flavor would you like?" with \
"Chocolate" or "Vanilla"
if It is Chocolate
set the fillcolor of ellipse "scoop" to the \
fillcolor of ellipse "Chocolate"
else
set the fillcolor of ellipse "scoop" to the \
fillcolor of ellipse "Vanilla"
end if
end buttonUpttonUptonUpttonUptonUpttonUppppppp
The step control structure can be used for animation. In this exercise it is used for moving the red dot around the screen.
1. Edit the script of the button, "Fun with Dot".
2. Create a buttonUp handler that will move it to the bottom of the screen and back.
(the starting position is 7750, 100 and the ending position is 7750, 5000)
You can find information about the Move command in the Using OpenScript manual.
For one solution, switch to Reader level and click on the "Hint" button.
3. How could you move the dot in a square around the screen??
Moving a Draw Object Using Steps
to handle buttonUp
move ellipse "dot" to 7750, 100
step yPos from 100 to 5000 by 100
move ellipse "dot" to 7750, yPos
end step
step yPos from 5000 to 100 by -100
move ellipse "dot" to 7750, yPos
end step
end buttonUp 0,var2
end step
step var3 from 300 to 1 by -10
move ellipse "dot" by var3,0
end step
end buttonUp
-- shows
hides the hint
"Hint"
buttonUp
buttonUp
Fun with Dot
etting text
When ToolBook sends a message the
first object that receives the message,
whether or not it handles it, is recorded in
the special variable, target. Now, try the following:
1. Edit the page script and enter the script you see to the right.
2. Switch to Reader level and click on one of the states in the map.
The buttonDown message is sent to the state you click. It is passed on to the group ("map"). Finally, it is passed to the page which has a handler for it that puts the name of the state object into the "State Name" field.
ning all the states. If you feel adventurous, you can ungroup the map and look at how each of the states is named.
A buttonUp handler Higher in the Hierarchyyy
State Name
Washington
Montana
Oregon
California
Nevada
Idaho
Wyoming
Arizona
New Mexico
Colorado
Texas
Oklahoma
Kansas
Nebraska
South Dakota
North Dakota
Minnesota
Missouri
Arkansas
Louisiana
Wisconsin
Illinois
Kentucky
Indiana
Tennessee
Mississippi
Alabama
Georgia
Florida
North Carolina
South Carolina
Michigan
Pennsylvania
West Virginia
Virginia
Maryland
Delaware
New York
New Jersey
Vermont
New Hampshire
Maine
Massachusetts
Connecticut
Rhode Island
Alaska
Hawaii
Hawaii
Hawaii
Hawaii
Hawaii
script1
to handle buttonDown
set text of field "State Name" to name of target
end buttonDown
to handle buttonUp
set the text of field "State Name" to null
end buttonUppppppppp
1. You can define functions using "to get" handlers in ToolBook. For this exercise, try to write a "to get" handler for the factorial function and put it in the page script.
If you want to see one solution, switch to Reader level and click on the Hint button.
2. Test the handler from the Command window by typing:
factorial(5)
The answer should be 120.
A Factorial Function, cont.
"Hint"
buttonUp
buttonUp
to get factorial n
if n is 1 then
return 1
else
return factorial(n-1) * n
end if
fx commands
-- Generic
install the
Effect
c"Script"
buttonUp
buttonUp
Script
180,50,100
Effect
0,100,0
This exercise shows you the effect of the different "fx" commands when changing pages. There are three scripts. Clicking on each one at Reader level will install it in the "Effect" button.
1. Switch to Reader level.
Most messages aren't sent at Author level.
2. Click on one of the scripts.
This puts the script in the "Effect" button.
3. Click on the "Effect" button.
4. Try it with the other scripts.
5. Make up an effect script of your own.
ript2, switch to Reader level and click the button.
5. Switch back to Author level and use the Command window to set the script of the button to the text of samplescript3, then switch to Reader level and click the button.
fxZoom, fxDissolve and fxWipe
Landscape
Effect
Effect
script3
to handle buttonUp
fxDissolve slow to next page
end buttonUputtonUp
script2
to handle buttonUp
fxZoom to next page at sysmouseposition
end buttonUpppppppppp
script1
to handle buttonUp
fxWipe left to gray
fxWipe right to next page
end buttonUpd buttonUpd buttonUp
!Z!w!
fx commands II
! 4
Go Back
buttonUp
buttonUp
You've made it!
Now, switch back to Reader level and click on the "Go Back" button.