home *** CD-ROM | disk | FTP | other *** search
/ Wacky Windows Stuff... / WACKY.iso / toolbook / oslab.tbk (.txt) < prev    next >
Asymetrix ToolBook File  |  1990-09-26  |  215KB  |  1,667 lines

  1. 1.    Read about to get and to set handlers in the "Beyond the Basics" chapter of Using OpenScript manual.
  2. 2.    Try writing a Set handler to set the size of the green ellipse to the right.
  3. 3.    Test the handler by typing:
  4.     set the objectSize of ellipse "green" to 2000, 500
  5.     from the command window.
  6.     To see one solution switch to Reader level and click on the Hint button.the Hint button.
  7. Setting an Object's Size with a To Set Handler, cont.
  8. green
  9. objSize 
  10. xyList
  11. [theSize
  12. objSize
  13. objSize
  14. objSize
  15. xyList
  16. objSize
  17. theSize
  18. -- Get and Set handlers for the size property.
  19. -- Place them in an object's script.
  20. to set objectSize to xyList
  21.     get my bounds
  22.     set item 3 of it to item 1 of it + item 1 of xyList
  23.     set item 4 of it to item 2 of it + item 2 of xyList
  24.     set my bounds to it
  25. to get objectSize
  26.     get my bounds
  27.     clear theSize
  28.     push item 4 of it - item 2 of it onto theSize
  29.     push item 3 of it - item 1 of it onto theSize
  30.     return theSize
  31. "Hint"
  32. buttonUp
  33. buttonUp
  34. Control Structures - Branching
  35. Control Structures - Branching
  36. 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.....
  37. to handle buttonUp
  38.     ask "What's your name?"
  39.     if it is not "Cancel" then
  40.         set text of field "Name" to it
  41.     else
  42.         beep 10
  43.     end if
  44. endep 10
  45.     end if
  46. to handle buttonUp
  47.     request "How Hot?" with "Mild" or \
  48.         "Medium" or "Hot"
  49.     conditons
  50.         when it is "Mild"
  51.             set spice to 0
  52.         when it is "Medium"
  53.             set spice to 5
  54.         else
  55.             set spice to 10
  56. If/then/else:
  57. Conditions/then/else:
  58. Implementing a Menu System, cont.
  59.     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). 
  60.     Try adding a new choice to one of the existing menus and have it go to the beginning of this exercise (the previous page).
  61.     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.
  62. Control Structures - Looping
  63. Control Structures - Looping
  64. Step, While and Do/until are ToolBook's looping control structures:
  65.     Step            - repeats 
  66. to handle buttonUp
  67.     step i from 1 to 10
  68.         set fieldName to "field " & i
  69.         clear the text of field fieldName
  70. end1 seconds
  71. end    beep 10
  72.     end if
  73. Step:
  74. to handle buttonUp
  75.     put fasle into giveAccess
  76.     while not giveAccess
  77.         ask "Password"
  78.         if it is securityCode of this book then
  79.             put true into giveAccess
  80.         end if
  81. While:
  82. to handle buttonUp
  83.         pop objectList
  84.         show it
  85.     until objectList is null
  86. endd"
  87.     if it is securityCode of this book then
  88.         put true into giveAccess
  89.     end if
  90. Do/until:
  91.     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.
  92. 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.
  93. 2.    Switch to Reader level and click on the button to see how the cursor shape is changed.
  94. 3.    Look up sysCursor in the OpenScript encyclopedia and edit the button's script to try some of the other cursor shapes....
  95. The sysCursor Property
  96. Please Wait
  97. -- Shows setting the cursor 
  98. a different shape
  99. default
  100. buttonUp
  101. buttonUp
  102. default
  103. Please Wait
  104. -- Shows setting the cursor to a different shape
  105. to handle buttonUp
  106.     set sysCursor to 4
  107.     pause 3 seconds
  108.     set sysCursor to default
  109. Z    \    \    ,
  110.     IT is a special variable used
  111.     as a default variable in 
  112.     OpenScript. Whenever you 
  113.     use a get command, 
  114.     OpenScript places the 
  115.     result in IT.
  116.     Ask and Request also store their results in IT. 
  117.       IT can also be used in the Command window. 
  118.     Type the following in the Command window (see the next topic on the Command window if you are unsure how to do this):
  119.     Get the text of field "Stuff"
  120.     put it
  121.     put it contains "brillig"
  122.     request it
  123.     request it
  124. Stuff
  125. T'was brillig and the slithy toves did gyre and ...eeeeeeeeeeeeeeeeee
  126. Field Stuff
  127.  h    B!
  128. T'was brillig and the slithy toves did gyre and ...bal in the wabe
  129. to handle buttonUp
  130.     get the text of field "stuff"
  131.     if it contains "brillig" then
  132. end buttonUp
  133. User-Defined Messages
  134. An Object's Location
  135. Handler Placement
  136. Handler Placement
  137. 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
  138. Recursive Handlers
  139. Recursive Handlers
  140. 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).
  141.     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.
  142. 1.    Create the originalSize property; type:
  143.         Set originalSize of polygon "triangle" to 
  144.         bounds of polygon "triangle"
  145.     This creates a user-defined property called "originalSize" containing the size of the triangle.
  146. 2.    Display the value of originalSize; type:
  147.         put the originalSize of polygon "triangle" 
  148. 3.    Finally, select and resize the triangle then type:
  149.         set bounds of polygon "triangle" to 
  150.         originalSize of polygon "triangle"
  151. A Self-Correcting Object
  152. triangle
  153. -- This handler places the 
  154. {clicked
  155. fields on 
  156. . If you 
  157.   but 
  158. *will be cleared.
  159. Glocation
  160. objectFromPoint(
  161. "Object Name" 
  162. Type" 
  163. ,UniqueName" 
  164. buttonup
  165. buttonup
  166. Object Name
  167. Object Type
  168. Object UniqueName
  169. Object Name
  170. Object Type
  171. Object UniqueName
  172. location
  173.     <    f    
  174.     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.
  175. 1.    Switch to Reader level.
  176.     Use F3 or the Reader menu command. If you're not at Reader level the script cannot function.
  177. 2.    Click on different objects on the page and observe the name, type and unique name.
  178.     Note: it will only show information about objects on the page. It will not show background information.
  179. 3.    Switch back to Author level and create an object of your own. Try the previous two steps clicking on your new object.
  180.     Was the information what you expected???????
  181. Object Names
  182. Object Name
  183. Nameeeeeeee
  184. Object Type
  185. Object UniqueName
  186. Unique Name
  187. Red Square
  188. Diamond
  189. Pie Slice
  190. Button
  191. Hi There!
  192. Diamond
  193. curveonle
  194. irregularpolygon id 18 of page 19 of Book "C:\BBSBOOKS\OSLAB.TBK"
  195. irregularpolygon
  196.     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:
  197.     factorial(4) = 1 x 2 x 3 x 4     = 24
  198.     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
  199.     factorial(4) = factorial(3) x 4
  200.     If fact, for any number, factorial(n) is just:
  201.     factorial(n) = factorial(n-1) * n
  202.     This is a recursive definition. The factorial function is defined in terms of itself.
  203.     Continued on the next page...
  204. A Factorial Function
  205. -- Clear the total fields 
  206. leaving 
  207. 4count
  208. /ext 
  209. totalSys
  210. totalLocal
  211. leavePage
  212. leavePage
  213. totalLocal
  214. totalSys
  215. count
  216.     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.
  217.     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.
  218.     Each time the one of the buttons is clicked the value in the variable count is incremented and displayed in the total field.
  219.     Switch to Reader level and try clicking on the buttons. How do they act differently???
  220. Local versus System Variables
  221. to handle buttonUp
  222.     system count
  223.     if count is null then
  224.         set count to 0
  225.     increment count
  226.     set the text of field "totalSys" to count
  227. System
  228. 4count
  229. "totalSys" 
  230. buttonUp
  231. buttonUp
  232. totalSys
  233. count
  234. System
  235. totalSys
  236. System Variable:
  237. to handle buttonUp
  238.     local count
  239.     if count is null then
  240.         set count to 0
  241.     increment count
  242.     set the text of field "totalLocal" to count
  243. Local
  244. Zcount
  245. "totalLocal" 
  246. buttonUp
  247. buttonUp
  248. totalLocal
  249. count
  250. Local
  251. totalLocal
  252. Local Variable::
  253. . To k
  254.     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:
  255.     set the size of ellipse green to 800,300
  256.     and have its bounds change accordingly.
  257.     Continued on the next page.
  258. 1.    Read about to get and to set handlers in the Using OpenScript manual.
  259. 2.    Following the guidelines above write handlers
  260. Setting an Object's Size with a Set Handlerrrr
  261. butto
  262.     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.
  263. 1.    Look up the keyChar message in the Beyond the Basics chapter of Using OpenScript.
  264. 2.    Try to write a keyChar handler in the script of the field that allows only digits to be typed.
  265.     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.
  266.     To see one solution, switch to Reader level and click on the Hint button.
  267. Verifying Input Data with a keyChar Handler
  268. to handle keyChar key
  269.     get ansiToChar(key)
  270.     if key < 32 or it is in "0123456789" then
  271.         forward
  272.     else
  273.         beep 5
  274.     end if
  275. "Hint"
  276. buttonUp
  277. buttonUp
  278. Number:
  279.     The Conditions/when/else control structure  allows you to accomodate many conditions in one structure.
  280. 1.    Create a button.
  281. 2.    Edit the button's script to:
  282.     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.
  283.     For one solution switch to Reader level and click on the hint button.
  284. 3.    Try the button out.
  285. Branching with Conditions
  286. to handle buttonUp
  287.     request "Which color?" with "Light" or \
  288.         "Medium" or "Dark"
  289.     conditions
  290.         when it is "Light"
  291.             set my fillColor to White
  292.         when it is "Medium"
  293.             set my fillColor to Cyan
  294.         when it is "Dark"
  295.             set my fillColor to Red
  296. enddddd
  297. -- shows 
  298. hides the hint
  299. "Hint"
  300. buttonUp
  301. buttonUp
  302.     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.
  303. 1.    Look at the script of the "Flat Face" button. Also look at the script of the "Round Face" button.
  304.     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.
  305. 2.    Switch to Reader level and click on both the buttons.
  306. k on a button to try it out.button to try it out.
  307. Changing an Object's Bounds
  308. -- Returns the smiley face 
  309. its original 
  310. "Smiley" 
  311. 5776, 837, 7998, 3162
  312. buttonUp
  313. buttonUp
  314. Smiley
  315. Round Face
  316. -- Returns the smiley face 
  317. its original 
  318. "Smiley" 
  319. 5776, 837, 7998, 1500
  320. buttonUp
  321. buttonUp
  322. Smiley
  323. Flat Face
  324. Smiley
  325.     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.
  326. 1.    Type the following command in the Command window:
  327.     restore menubar at both
  328.     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.
  329. Restoring Menus
  330. -- leaveBook handler to restore the menu
  331. -- bar when going to another book.
  332. to handle leaveBook
  333.         restore menuBar at both
  334. -- leavePage handler to restore the menu
  335. -- bar when going to another page.
  336. to handle leaveBook
  337.         restore menuBar at both
  338. Topic
  339.     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.
  340. 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.
  341.     If you want one answer, switch to reader level and click on the "Hint" button. (clicking again will cause it to disappear).
  342. 3.    Try the script out.
  343.     Remember, you have to activate the field's script and switch to Reader level.  
  344. Branching with If/then/else
  345. Attendance
  346. Attendance
  347. to handle buttonUp
  348.     ask "Attendance?"
  349.     if it >0 and it < 11 then
  350.         set the my text to it
  351.     else
  352.         beep 20
  353.     end if
  354. enddddddddddddddddddddddd
  355. -- shows 
  356. hides the hint
  357. "Hint"
  358. buttonUp
  359. buttonUp
  360. Containers
  361. Special Effect Commands
  362. To Get / To Set Handlers
  363.     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.
  364.     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.
  365.     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).
  366.     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.
  367. A Recursive Object Lister
  368. blank hint
  369. to handle buttonUp
  370.     clear my text
  371.     send listObjects (objects of this page)
  372. end buttonUp
  373. to handle listObjects x, place
  374.     set syscursor to 4
  375.     step i from 1 to itemcount(x)
  376.         get item i of x
  377.         put object of it & ", " & quote & name of it & \
  378.             quote & CRLF             after my text
  379.         if object of it is "group"
  380.             send listObjects (objects of it)
  381.         end
  382.     set syscursor to default
  383. end listObjectsfault
  384. end listObjectssects
  385. objectList
  386. "Hint"
  387. buttonUp
  388. buttonUp
  389. Geometrics
  390. Square
  391. Triangle
  392. Object List:
  393. Objects
  394. Objectssssssssss
  395. 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.
  396. As an example, this page consists of a background object, a page object, record fields (for text), a button, and various graphic objects.....
  397. Objects
  398. Hide and Show Commands
  399. Visual Effects
  400. Properties
  401. Properties
  402. 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.
  403. The following exercises show you some examples of changing properties.
  404. Properties
  405. Recursive Handlers
  406.     You can place multiple commands in the Command window at the same time by separating them with semicolons.
  407. 1.    Display the Command window.
  408.     press Shift+F3 if it isn't already visible.
  409. 2.    Type the following command and press Enter when you're finished:
  410.     step i from 1 to 100; move ellipse dot by 10,10;     end
  411. 3.    What command would you type to move the dot back to its starting point???????ng point?
  412. Multiple Commands on a Line
  413. -- Clear the messages 
  414. leaving 
  415. "Messages"
  416. leavePage
  417. leavePage
  418. Messages
  419.     There are numerous messages sent to an object as a result of mouse movement. The messages are:
  420.     mouseEnter            mouseLeave
  421.     buttonDown                buttonUp
  422.     buttonStillDown            buttonStillUp
  423.     buttonDoubleClick        rightButtonDown
  424.     rightButtonDoubleClick    rightButtonDown
  425. 1.    Switch to Reader level (mouse messages aren't sent at author level).
  426.     Press F3.
  427. 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.
  428. Mouse Location and Button Messagess
  429. Messages
  430. checkScroll
  431. "messages"
  432. ) > 500 
  433. " & CRLF 
  434. {" & 
  435. |" & 
  436. }" & 
  437. z" & 
  438. R" & 
  439. S" & 
  440. Q" & 
  441. buttonDown
  442. buttonStillDown
  443. mouseEnter
  444. buttonUp
  445. mouseLeave
  446. buttonDoubleClick
  447. rightButtonDown
  448. checkScroll
  449. rightButtonUp
  450. rightButtonDoubleClick
  451. checkScroll
  452. messages
  453. mouseEnter
  454. mouseEnter
  455. messages
  456. checkScroll
  457. mouseLeave
  458. mouseLeave
  459. messages
  460. checkScroll
  461. buttonDown
  462. buttonDown
  463. messages
  464. checkScroll
  465. buttonStillDown
  466. buttonStillDown
  467. messages
  468. checkScroll
  469. buttonUp
  470. buttonUp
  471. messages
  472. checkScroll
  473. buttonDoubleClick
  474. buttonDoubleClick
  475. messages
  476. checkScroll
  477. rightButtonDown
  478. rightButtonDown
  479. messages
  480. checkScroll
  481. rightButtonUp
  482. rightButtonUp
  483. messages
  484. checkScroll
  485. rightButtonDoubleClick
  486. rightButtonDoubleClick
  487. messages
  488. checkScroll
  489. Messages:
  490. Object Drawing and Display
  491. Object Drawing and Display
  492. 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.
  493. Navigation and Visual Effects
  494. Navigation and Visual Effects
  495. 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.
  496. Get and Set Handlers
  497. Types of Objects
  498. A Self-Sorting Field
  499. A Self-Sorting Field
  500. 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.
  501. Implementing a Menu System
  502. Implementing a Menu System
  503. 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.
  504. In the following exercise you can examine how menus were designed for this book and try to creating your own menus in another book.
  505. Implementing a Menu System
  506. Name versus ID
  507. Containers - Variables
  508. Listing All Objects on a Page
  509. Listing All Objects on a Page
  510. 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........
  511. OpenScript Exercises
  512. disclaimer
  513. 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."
  514. 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.
  515. Copyright (c) 1990 by Asymetrix Corporation. All rights reserved. Asymetrix, ToolBook, and OpenScript are registered trademarks of Asymetrix Corporation.  
  516. 1.    Create a button and label it "Get First Name".
  517.     Use the button tool.
  518. 2.    Edit the button's script and type:
  519.     to handle buttonUp
  520.         ask "Type your first name" with "Sigmund"
  521.         set the text of field "First Name" to it
  522.     end buttonUp
  523. 3.    Switch to Reader level and click on the button.
  524.     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?
  525. Using the Ask Commanddddd
  526. First Name
  527. Field: First Name
  528. Structure of a Handler
  529. A handler has three parts: the beginning, middle and end.
  530. 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.
  531. In the following exercise you will 
  532. write a simple buttonUp handler.you will write a simple buttonUp handler.
  533. -- Changes to reader level when
  534. -- page is entered.
  535. to handle enterPage
  536.     send Reader
  537. end enterPagerPageeeeee
  538. Beginning: identifies the name of the message this handler is for.
  539. Middle: contains instructions in OpenScript saying what to do when the message is received.
  540. End: marks the end of the handler
  541. OpenScript Topics
  542. Special Effects
  543. -- Menu Background
  544. -- To 
  545. cchoice: 
  546. )Author level 
  547. }click 
  548. bullets 
  549. type 
  550. must be the 
  551. clicked.
  552. -- To 
  553. c: create a 
  554. /. At 
  555. ctitle 
  556. ilarge diamond
  557. enter a 
  558. -- Change 
  559. Reader 
  560. entering 
  561. -- If 
  562. mouse 
  563.  over a 
  564. handler 
  565. color 
  566. 5. Otherwise 
  567. forwards 
  568. }message.
  569. c"Choice"
  570. -- This 
  571. }branches 
  572. correct 
  573. Bwas pressed 
  574. released 
  575. same 
  576. sent 
  577. {that received 
  578. -- matching 
  579. , even 
  580. no longer
  581. {. The objectFromPoint function returns 
  582. -- under 
  583. . If 
  584. goes 
  585.  whose 
  586. . Before 
  587. pushes 
  588. current 
  589. ^called menuStack. 
  590. menuPop 
  591. -- below pops 
  592. }location
  593. can be 
  594. anywhere 
  595. Hsending the
  596. "). If there are no 
  597. -- on 
  598. stack 
  599. buttonDown
  600. enterPage
  601. buttonUp
  602. menuPop
  603. enterPage
  604. Reader
  605. buttonDown
  606. Choice
  607. 0,0,0
  608. buttonUp
  609. Choice
  610. 0,100,0
  611. menuStack
  612. location
  613. menuPop
  614. menuStack
  615. Title
  616. Choice1
  617. choice
  618. choice3
  619. choice4
  620. choice5
  621. choice6
  622. Go Back
  623. -- Returns 
  624. Hpopping its 
  625. cstack.
  626. menuPop 
  627. /"Menu"
  628. buttonUp
  629. buttonUp
  630. o MmenuPop
  631. Go Back
  632. Click on a Topic
  633. OpenScript Topics
  634. OpenScript Topics
  635. OpenScript Concepts
  636. OpenScript Commands
  637. Advanced OpenScript Topics
  638. OpenScript Applications
  639. OpenScript Concepts
  640. OpenScript Concepts
  641. Programming with ToolBook
  642. Objects
  643. Messages
  644. Properties
  645. Handlers
  646. Containers
  647. ow, click 
  648. OpenScript Commands
  649. OpenScript Commands
  650. The Command Window
  651. Ask and Request and Effectsomm
  652. Control Structures
  653. Navigation and Visual Effects
  654. Object Drawing and Displaysssssssssss
  655. Menuslllllll
  656. Advanced OpenScript Topics
  657. Advanced OpenScript Topics
  658. User-Defined Properties
  659. User-Defined Messages
  660. Handler Placement
  661. Recursive Handlers
  662. Handling Key Strokes
  663. Get and Set Handlersssss
  664. OpenScript Applications
  665. OpenScript Applications
  666. A Self-Sorting Field
  667. Listing all Objects on a Page
  668. Implementing A Menu System
  669. OpenScript Applications
  670. Drawing Objects
  671. Programming with ToolBook
  672. Control Structures - Branching
  673. Menu System Exercise
  674. Developing ToolBook Applications
  675. 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.
  676. Handlers
  677. Handler Placement
  678. While
  679. Object Names
  680. Navigation and Visual Effects
  681. The Command Window
  682. Control Structures - Looping
  683. An Object's Unique Name
  684. Containers
  685. Containers
  686. 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.
  687. The following exercises demonstrate using local versus system variables and the special variable IT. Properties are covered in their own topic.. topic.
  688. Adding a menu
  689.     In this exercise you will see a script that alters how ToolBook responds to the Author menu selection.
  690. 1.    Switch to Reader level and choose Author from the Edit menu.
  691.     Notice what happens.
  692. 1.    Edit the script of this page and add the handler displayed to the right.
  693. 2.    Change to Reader level again and choose Author from the Edit menu.
  694.     What's different this time?
  695.     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
  696. Handling Standard Menu Choices
  697. script1
  698. to handle author
  699. request "Do you want to switch to Author level"\
  700.     with "Yes" or "No"
  701.       if it is "Yes"
  702.             forward
  703.       end if
  704. end author
  705. e enterBook
  706. --positions the workbook window
  707.     set syslevel to author
  708.     send sizeToPage
  709. end enterBook
  710. Author Handler
  711. Hiding and showing
  712.     In this exercise you will write scripts to hide and show a field that contains a definition for a hotword.
  713. 1.    Select the bold word "score" in the text and create a hotword from it.
  714.     Use the Create Hotword command from the Text menu or Ctrl-W
  715. 2.    Edit the script of the hotword creating a buttonUp handler that shows the field called "score" (the white field).
  716. 3.    Edit the script of field "score" to hide itself (you can use the special term "self" in the hide command).
  717. 4.    Switch to Reader level and try your scripts. d  
  718. Show and Hide: Making a Popup Definitiondddddddddddddd
  719. hotwords
  720. 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.
  721. score
  722. A score is twenty years. "Four score and seven" is eight-seven years.""ight-seven years."
  723. -- Handler for the hotword
  724. to handle buttonUp
  725.       show field score
  726. end buttonUp
  727. -- Handler for the popup definition
  728. to handle buttonUp
  729.       hide self
  730. end buttonUp
  731. -- shows 
  732. hides the hint
  733. "Hint"
  734. buttonUp
  735. buttonUp
  736. Introduction
  737. enterPage
  738. enterPage
  739. reader
  740. buttonDown
  741. buttonUp
  742. buttonDown
  743. 0,0,0
  744. buttonUp
  745. 0,100,0
  746. buttonDown
  747. buttonUp
  748. buttonDown
  749. 0,0,0
  750. buttonUp
  751. 0,100,0
  752. Using command window
  753. 1.    Display the Command window.
  754.     Press Shift+F3 if it isn't already displayed.
  755. 2.    Type each of the following commands in the  Command window. Press Enter after each one. 
  756.     select ellipse dot
  757.     unselect ellipse dot
  758.     put position of ellipse dot
  759.     move ellipse dot by 100,100
  760.     set the fillColor of ellipse dot to green
  761. set bounds of ellipse dot to 300,300,1300,1300
  762.     edit the script of ellipse dot
  763.     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."""""""""""""""""""""
  764. Working with Objects from the Command Window
  765. buttonUp
  766. buttonDown
  767. buttonUp
  768. 60,50,100
  769. buttonDown
  770. 240,50,100
  771.     The script to the right shows an example of adding a custom menu and menu items.
  772. 1.    Enter the script to right into the script of this page.
  773.     An easy way to do this is to type the following in the Command window:
  774.         set script of this page to text of field script1
  775. 2.    Install the menus by sending a setupMenu message to this page.
  776.     Send the setupMenu message by typing the following in the Command window:
  777.         send setupMenu
  778. 3.    Try the new menu items.
  779.     See the next exercise to restore menubars.
  780. pt menu.
  781. 5.  Switch back to Author level.
  782. Adding Menus and Menu Items
  783. script1
  784. originalFill 
  785. buttonUp
  786. buttonUp
  787. 180,50,100
  788. originalFill
  789. to handle setupMenu
  790.     add menu"Script" at both
  791.     add menuitem "Object" to menu "Script" at both
  792.     add menuitem "Page" to menu "Script" at both
  793.     add menuitem "Book" to menu "Script" at both
  794.     deactivate menuitem "Object" at Reader
  795. end setupMenu
  796. to handle Object
  797.     if selection is null
  798.         request "Please select an object first."
  799.     else
  800.         edit the script of the selection
  801.     end if
  802. end Object
  803. to handle Page
  804.       edit the script of this page
  805. end page
  806. to handle book
  807.       edit the script of this book
  808. end book
  809. to handle leavepage
  810.       restore menubar at both
  811. end leavepageeeeeeeeeeeeenubar at both
  812. end leavepageepage leavepageepageleavepageepageepageepageepageepageeeeee
  813. The UniqueName
  814. 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.
  815. The following example shows you the name, object type and unique name of several common objects.
  816. Alaska
  817. Advanced OpenScript Topics
  818. Refering to an Object
  819. script window shortcuts
  820.     Another of an object's properties is its pattern property. This is the pattern used to fill filled shapes.
  821. 1.    Look up the Pattern property in the OpenScript encyclopedia chapter in the Using OpenScript manual.
  822. 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.
  823. 2.    Switch to Reader level and click on the Watch button.
  824. 3.    Edit the button's script and add a line so that the screen returns to white after finished displaying the other patterns.erns.
  825. The Pattern Propertyyyyyyyyyyyyyy of 
  826. script1
  827. -- Changes the pattern property of a rectangle
  828. -- to all possible patterns.
  829. to handle buttonUp
  830.     step i from 1 to 128
  831.         set pattern of rectangle "boob toob" to i
  832. endddddddddddddd
  833. Boob Toob
  834. Watch
  835. -- Changes the 
  836. property 
  837. possible patterns.
  838. "boob toob" 
  839. buttonUp
  840. buttonUp
  841. boob toob
  842. Watch
  843.  field
  844. Topic
  845. ction
  846. Reader
  847. enterPage
  848. enterPage
  849. Reader
  850. ButtonShadow
  851. Topic
  852. buttonDown
  853. buttonUp
  854. buttonDown
  855. 0,0,0
  856. buttonUp
  857. 0,100,0
  858. buttonDown
  859. buttonUp
  860. buttonDown
  861. 0,0,0
  862. buttonUp
  863. 0,100,0
  864. -- Returns 
  865. Hpopping its 
  866. cstack.
  867. menuPop 
  868. /"Menu"
  869. buttonUp
  870. buttonUp
  871. o MmenuPop
  872. script window shortcuts
  873. Moving objects
  874. User-Defined Properties
  875. abcdefghijklmno
  876. Handlers
  877. Handlerssandlerss
  878. 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).
  879. In order to respond to a message, an object must have what's called a "handler" in its script.
  880. 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.
  881.     Here's another example of using the Step control structure.
  882. 1.    Edit the script of the "Clear Names" button.
  883. 2.    Create a script to clear the names of the fields. The fields are called Name1, Name2, etc.
  884.     For one solution, switch to Reader level and click on the Hint button.
  885. Another Example of the Step Loop
  886. Name1
  887. Name6
  888. Name5
  889. Frank
  890. Name4
  891. Name3
  892. Henderson
  893. Name2
  894. Bertrand
  895. Clear Names
  896. to handle buttonUp
  897.     step i from 1 to 6
  898.         set fieldName to "Name" & i
  899.         clear the text of field fieldName
  900. endddd1 seconds
  901. end    beep 10
  902.     end if
  903. -- shows 
  904. hides the hint
  905. "Hint"
  906. buttonUp
  907. buttonUp
  908. exercise
  909. Introduction
  910. Defining own message
  911. Hiding and showing
  912. Containers - Properties
  913. Messages
  914. Object Drawing and Display
  915. Menus
  916. Creating objects
  917. Setting text
  918. Using command window
  919. Control Structures
  920. Ask and Request
  921. Types of Objects
  922. Types of Objects
  923. There are different types of objects:
  924. Books                                Record fields
  925. Backgrounds                        Hotwords
  926. Pages                                Groups
  927. Graphic Objects
  928. Buttons
  929. Fields
  930. 1.    Create a rectangle on this page.
  931.     Use the rectangle tool on the tool palette.
  932. 2.    Edit the script of the rectangle.
  933.     Use the Graphic Properties dialog box or press the Ctrl key while double-clicking the rectangle with the selection tool.
  934. 3.    Enter and save the following script:
  935.         to handle buttonUp
  936.             beep 20
  937.         end
  938. 4.    Switch to Reader level and click on the rectangle.
  939.     What messages did clicking on the rectangle send? Which one caused the rectangle to beep? What happened to the other message?
  940. Writing a Simple buttonUp handler
  941. Refering to an Object
  942. Referring to an Object
  943. 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:
  944. ellipse "oval" or ellipse ID 0. nu" of background "Topic".
  945. 1.    Display the Command window.
  946.     Press Shift+F3 if it isn't already visible.
  947. 2.    Type the following commands in the Command window:
  948.     hide rectangle box
  949.     show rectangle box
  950.     hide menubar
  951.     show menubar
  952.     hide toolpalette
  953.     show toolpalette
  954.     move toolpalette to 100,100
  955. 3.    What commands would you type to hide and show the Pattern palette? The Color Tray?
  956.     Hint: If you don't know, try looking in the Using OpenScript manual.
  957. Hiding and Showing Objects from the Command Window
  958. 1.    Display the Command window.
  959.     Press Shift+F3 if it isn't already visible.
  960. 2.    Type the following commands:
  961.     set text of field "thatfield" to text of field "thisfield"
  962.     clear text of field "thisfield"
  963.     set text of field "thisfield" to "Hi there!"
  964.     put the third word of text of field "thatfield"
  965.     put the last word of text of field "thisfield"
  966.     put words 4 to 7 of text of field "thatfield"
  967. 3.    How would you refer to the third through thirteenth characters of field thatfield?"
  968. 3.    How would you refer to the third through thirteenth characters of field thatfield?
  969. Setting and Getting Field Text in the Command Windowow
  970. thisfield
  971. The text of a field is one of its properties just like its border style. and can be set with an OpenScript statement.
  972. thisfield
  973. thatfield
  974. thatfield
  975. Programming with ToolBook
  976. Traditional Programming
  977. The program controls the user by requesting input and displaying output..
  978. PRINT "What's your name?";
  979. INPUT Name$
  980. PRINT "Hi, "; Name$
  981. PRINT "Enter a number: ";
  982. INPUT Num
  983. PRINT "The square root of "; Num; " is: "; SQRT(Num)
  984. FUNCTION Fact(n)
  985.     f = 1
  986.     FOR i=2 to n
  987.         f = f*i
  988.     NEXT i
  989.     Fact = f
  990. END FUNCTIONNNNNNNNNNNNNNNN
  991. What's your name? Sue
  992. Hi, Sue
  993. Enter a number: 4
  994. The square root of 4 is 2
  995. ToolBook Programming
  996. 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.................
  997. Number:
  998. number
  999. ("What 
  1000. buttonUp
  1001. buttonUp
  1002. What number?
  1003. to handle buttonUp
  1004.     get text of self
  1005.     ask "Type a number" with it
  1006.     if it is not "cancel" then
  1007.         set the text of self to it
  1008. end buttonUp
  1009. Calculate
  1010. "answer"
  1011. buttonUp
  1012. buttonUp
  1013. number
  1014. answer
  1015. answer
  1016. Calculate
  1017. to handle buttonUp
  1018.     get text of field "number"
  1019.     clear text of field "answer"
  1020.     if it is not null then
  1021.         get sqrt(it)
  1022.         set text of field "answer" to it
  1023. end buttonUp ndld "answer"
  1024. Answer
  1025. Square Root:is:::::
  1026. Name versus ID
  1027. Name versus ID
  1028. 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. 
  1029. 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.
  1030. An Object's Location
  1031. An Object's Locationame
  1032. 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:
  1033. button "Next Page" of background "Database"
  1034. he following example shows you the namess of 
  1035. Ask and Request
  1036. Ask and Request Commands
  1037. 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.
  1038. 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.
  1039. r, IT....on in the container, IT.
  1040. Handling Key Strokes
  1041. Handling Key Strokes
  1042. 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.
  1043. The following exercise shows you how to write a keyChar handler to make sure data is numeric.ger
  1044. 1.    Create a button. Label it "Decision"
  1045.     Use the button tool in the Tool palette.
  1046. 2.    Edit the button's script, typing the following:
  1047.     to handle buttonUp
  1048.         request "Save humanity or not?" with \
  1049.             "Save" or "Destroy"
  1050.         set text of field "fate" to it
  1051.     end buttonUp
  1052. 4.    Switch to Reader level and click on the button.
  1053.     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).
  1054. Using the Request Command
  1055. Neitherbout it
  1056. Field: fate
  1057. Get and Set Handlers
  1058. Get and Set Handlersssss
  1059. 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.
  1060. 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..
  1061.     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:
  1062.     send sortLines to field fieldname
  1063.     where fieldname  stands for the name of the field. So, for this exercise:
  1064. 1.    Create a field.
  1065. 2.    Write a sortLines handler for the field that sorts the field's lines.
  1066.     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.....................
  1067. A Self-Sorting Field
  1068. "Hint"
  1069. buttonUp
  1070. buttonUp
  1071. -- Handler to sort a field. The handler must be 
  1072. -- located in the field's script and is activated by 
  1073. -- sending a sortLines message to the field.
  1074. to handle sortLines
  1075.     get my text
  1076.     set N to textLineCount(it)
  1077.     clear my text
  1078.     step i from N to 2 by -1
  1079.         step j from 1 to i-1
  1080.             if textLine i of it < textLine j of it as text 
  1081.                 set temp to textLine i of it
  1082.                 set textLine i of it to textLine j of it
  1083.                 set textLine j of it to temp
  1084.             end if
  1085.         end step
  1086.     end step
  1087.     set my text to it
  1088. enddd
  1089. if/then/else
  1090. OpenScript Commands
  1091. Menu Control
  1092. A Self-Sorting Field
  1093. Navigation
  1094. Names and variables
  1095. Listing All Objects on a Page
  1096. fx commands
  1097. OpenScript Concepts
  1098. Conditions
  1099. Object Types
  1100. fx commands II
  1101. Do/until
  1102. Adding a menu
  1103. Handling Key Strokes
  1104. 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.  
  1105. What this Book Contains
  1106. Topic
  1107. Explana-
  1108. tionnn
  1109. Exercise
  1110. Explana-
  1111. tionnn
  1112. Exercise
  1113. The Command Window
  1114. The Command WindowWindow
  1115. 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.
  1116. :PHYSSIZE
  1117. User-Defined Properties
  1118. User-Defined Properties
  1119. 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.
  1120. User-Defined Messages
  1121. User-Defined Messages
  1122. 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.
  1123. Menus
  1124. ontrol
  1125. Menuslllllll
  1126. 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.
  1127. Navigating in this Book
  1128. Following this introduction are menus you can use to locate a topic. You can also browse through topics page by page.
  1129. 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.
  1130. A Final Note ....
  1131. 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.
  1132. 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.
  1133. Now, click on the forward arrow for a list of topics.....cs.
  1134. Messages
  1135. ToolBook Messageslers, Cont.
  1136. A user's actions cause 
  1137. messages to be sent. Each 
  1138. message has a name. For 
  1139. example, clicking the mouse on 
  1140. an object causes two messages 
  1141. to be sent to the object: 
  1142. buttonDown and buttonUp.
  1143. Button Message Example
  1144. -- This 
  1145. does crude animation 
  1146. -- two 
  1147. Bmessages 
  1148.  example
  1149. doMouse
  1150. B"mouseClick" 
  1151. "downMessage"
  1152. Move 
  1153. 6435, 2647
  1154. 6390, 2557
  1155. 6300, 2467
  1156. 6210, 2332
  1157. 6120, 2212
  1158. 6045, 2122
  1159. 5910, 1942
  1160. 5745, 1852
  1161. 5565, 1687
  1162. 5430, 1597
  1163. "upMessage"
  1164. 6435, 2647
  1165. 6390, 2557
  1166. 6300, 2467
  1167. 6210, 2332
  1168. 6120, 2212
  1169. 6045, 2122
  1170. 5910, 1942
  1171. 5745, 1852
  1172. 5565, 1687
  1173. 5430, 1597
  1174. doMouse
  1175. doMouse
  1176. mouseClick
  1177. downMessage
  1178. button
  1179. mouseClick
  1180. upMessage
  1181. button
  1182. matte
  1183. button
  1184. inverse
  1185. false
  1186. Print Labels
  1187. arrow
  1188. mouseClick
  1189. inverse
  1190. upMessage
  1191. buttonUp
  1192. downMessage
  1193. buttonDown
  1194. -- This handler sends a doMouse message 
  1195. the grouped 
  1196. animate 
  1197. example.
  1198. "Button Message Example"
  1199. buttonUp
  1200. buttonUp
  1201. FdoMouse
  1202. Button Message Example
  1203. Show Example
  1204. arning ToolBook
  1205. System
  1206. enScript Exercises
  1207. s Rmn
  1208. `D|D|
  1209. -- a 
  1210. renumbering a 
  1211. n > 0 
  1212. enterBook
  1213. renumber
  1214. enterBook
  1215. sizeToPage
  1216. renumber
  1217. :REPORTDATA
  1218. :CONDITIONDATA
  1219. Text of RecordField "Instructions" is "null" As Text
  1220. Text of RecordField "Instructions" is null
  1221. OpenScript Lab Exercises
  1222. System
  1223. y`D|D|
  1224. `D|D|
  1225. Exercise Name
  1226. "gxp0
  1227. `D|D|
  1228. `D|D|
  1229. `D|D|
  1230. `D|D|
  1231. `D|D|
  1232. rminal
  1233. `D|D|
  1234. `D|D|
  1235. `D|D|
  1236. fault
  1237. 9_D|D|
  1238. `D|D|
  1239. `D|D|
  1240. `D|D|
  1241. s Rmn
  1242. `D|D|
  1243. restore
  1244. restoreobjects
  1245. restoreobjects
  1246. restoreobjects
  1247. restoreobjects
  1248. paste
  1249. pageobjs
  1250. `D|D|
  1251. :PRINTLAYOUT
  1252. `D|D|
  1253. `D|D|
  1254. u`D|D|
  1255. `D|D|
  1256. ("Exercise title?"
  1257. "Cancel"
  1258. NewPage
  1259. hName" 
  1260. ("Topic?"
  1261. newTopic
  1262. ("Menu 
  1263. J"Title" 
  1264. movePages n, dest
  1265. M+i-1
  1266. erBook
  1267. author
  1268. enterBook
  1269. movePages
  1270. enterBook
  1271. sizeToPage
  1272. author
  1273. Exercise title?
  1274. Cancel
  1275. Exercise
  1276. y5NewPage
  1277. Exercise Name
  1278. Topic?
  1279. Cancel
  1280. Topic
  1281. y5NewPage
  1282. Topic
  1283. newTopic
  1284. Menu Topic?
  1285. Cancel
  1286. y5NewPage
  1287. Title
  1288. newTopic
  1289. movePages
  1290. exercise
  1291. enterPage
  1292. enterPage
  1293. author
  1294. Instructions
  1295. Exercise Name
  1296. To move to next page, use Ctrl+<right arrow>.........................................
  1297. Control Structures
  1298. Control Structures
  1299. 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.
  1300. Defining own message
  1301. -- When you click on a 
  1302. Reader mode, 
  1303. {identified
  1304. 's targetObject property (user-defined).
  1305. buttonUp
  1306. buttonUp
  1307. script
  1308. targetObject
  1309. 180,50,100
  1310. 0,100,0
  1311.     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.
  1312. 1.    Type the following two commands in the Command window:
  1313.     set script of rectangle peep to text of field script1
  1314.     set script of button send to text of field script2
  1315.     This will copy the scripts into the two objects
  1316. 3.    Switch to Reader level and click the "Send Message" button.
  1317. 4.    Create another button and add another handler to the rectangle to handle a message called "envy"..
  1318.     User-Defined Messages
  1319. Send message
  1320. script1
  1321. targetobject
  1322. rectangle id 0 of page id 83
  1323. to handle blush
  1324.         set my fillcolor to red
  1325.         beep 10
  1326.         set my fillcolor to white
  1327. end blush
  1328. script2
  1329. targetObject
  1330. button id 1 of page id 83
  1331. to handle buttonUp
  1332.     send blush to rectangle peep
  1333. end buttonUpUp
  1334. -- Make sure we're 
  1335. )Reader level 
  1336. entering 
  1337. -- This animates the world (a metaphysical thought). The "
  1338. -- consists 
  1339. 16 stacked 
  1340. }named Earth 1, 
  1341. 2, etc.
  1342. -- showing 
  1343. hearth 
  1344. different stages 
  1345. rotation.
  1346. handler 
  1347. repeatedly whenever ToolBook isn't doing
  1348. -- anything 
  1349. . Every 
  1350. moves one 
  1351. images 
  1352. ^, counter, 
  1353. incremented
  1354. through 
  1355. 16 earths.
  1356. " && 
  1357.  > 16 
  1358. enterPage
  1359. enterPage
  1360. reader
  1361. Earth
  1362. earth
  1363. counter
  1364. OpenScript Exerciseskkkkk
  1365. -- This handler animates the "Earth" and
  1366. -- is placed in the script of this page. ToolBook
  1367. -- sends an idle message whenever it isn't 
  1368. -- doing anything else.
  1369. to handle idle
  1370.     system counter
  1371.     if counter is null
  1372.         set counter to 1
  1373.     end
  1374.     set earth to "Earth" && counter
  1375.     set the layer of group earth to 20
  1376.     increment counter
  1377.     if counter > 16 then
  1378.         set counter to 1
  1379.     end
  1380. end idle
  1381. Earth 7
  1382. Earth 8
  1383. Earth 9
  1384. Earth 10
  1385. Earth 11
  1386. Earth 12
  1387. Earth 13
  1388. Earth 14
  1389. Earth 15
  1390. Earth 16
  1391. fJd    *
  1392. Earth 1
  1393. lOM    E
  1394. HRM    F
  1395. Earth 2
  1396. Earth 3
  1397. Earth 4
  1398. Earth 5
  1399. Earth 6
  1400. Earth 6
  1401. Earth 7
  1402. Earth 8
  1403. JvG    T
  1404. Earth 9
  1405. Earth 10
  1406. Earth 11
  1407. Earth 12
  1408. Earth 13
  1409. Earth 14
  1410. Earth 15
  1411. Earth 16
  1412. Earth 1
  1413. Earth 2
  1414. Earth 3
  1415. Earth 4
  1416. Earth 5
  1417. Creating objects
  1418.     In this exercise you will write a script to draw a rectangle.
  1419. 1.    Edit the script of the button, "Blue Square"
  1420. 2.    Create a buttonUp handler to draw a rectangle from 6000,900 to 7500,2400.
  1421.     For a hint, switch to Reader level and click on the Hint button.
  1422. 3.    Switch to Reader level and try the button.
  1423. 4.    Try the same for an ellipse, or polygon.
  1424. Drawing Objects from a Script
  1425. bluesquare
  1426. Blue square
  1427. to handle buttonUp
  1428.     draw rectangle from 6000,900 to \
  1429.         7500,2400
  1430.     set the fillcolor of the selection to blue
  1431. end buttonUpppppppppppppppp
  1432. -- shows 
  1433. hides the hint
  1434. "Hint"
  1435. buttonUp
  1436. buttonUp
  1437. Menu System Exercise
  1438. Implementing a Menu System
  1439.     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.
  1440.     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.
  1441.     To return to the previously viewed menu send a menuPop message to the background, "Menu".
  1442.     Continued on the next page.nu".
  1443.     the background, "Menu".
  1444.                                     
  1445. Moving objects
  1446.     In this exercise you will write a script to move 
  1447.     the red dot around the screen.
  1448. 1.    Edit the script  of the button "Fun With Dot" and create a buttonUp handler to move the dot to the following points:
  1449.         8000, 100
  1450.         8000, 4000
  1451.         6300, 2050
  1452.         4600, 4000
  1453.         4600, 100
  1454.     Use any order you want. For one solution, switch to Reader level and click the Hint button.
  1455. Moving a Draw Object
  1456. funbutton
  1457. Fun With Dot
  1458.  to handle buttonUp
  1459.          move ellipse "dot" to 8000,100
  1460.         move ellipse "dot" to 8000,4000
  1461.         move ellipse "dot" to 6300, 2050
  1462.         move ellipse "dot" to 4600,4000
  1463.         move ellipse "dot" to 4600,100
  1464. end buttonUppttonUppppppppppppnUp
  1465. -- shows 
  1466. hides the hint
  1467. "Hint"
  1468. buttonUp
  1469. buttonUp
  1470. f/then/else
  1471.     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.
  1472. 1.    Look at the script in the white box. Which lines would change the fillColor property of the ellipse, scoop?
  1473.     This is the script of the button labeled "Chocolate or Vanilla?"
  1474. 2.    Switch to Reader level and click on the button.
  1475.     Try it again with the other flavor.ick on the button.
  1476.     Try it again with the other flavor..hen switch back to Author level.
  1477. Changing an Object's Color Properties
  1478. scoop
  1479. vanilla
  1480. chocolate
  1481. Flavor
  1482. "What flavor would you like?" 
  1483.     "Chocolate" 
  1484. "Vanilla"
  1485. "scoop" 
  1486. buttonUp
  1487. buttonUp
  1488. What flavor would you like?
  1489. Chocolate
  1490. Vanilla
  1491. scoop
  1492. Chocolate
  1493. scoop
  1494. Vanilla
  1495. Chocolate
  1496. Chocolate or Vanilla?
  1497. Script1
  1498. to handle buttonUp
  1499.     request "What flavor would you like?" with \
  1500.     "Chocolate" or "Vanilla"
  1501.     if It is Chocolate
  1502.         set the fillcolor of ellipse "scoop" to the \
  1503.         fillcolor of ellipse "Chocolate"
  1504.     else
  1505.         set the fillcolor of ellipse "scoop" to the \
  1506.         fillcolor of ellipse "Vanilla"
  1507.     end if
  1508. end buttonUpttonUptonUpttonUptonUpttonUppppppp
  1509.     The step control structure can be used for animation. In this exercise it is used for moving the red dot around the screen.
  1510. 1.    Edit the script of the button, "Fun with Dot".
  1511. 2.    Create a buttonUp handler that will move it to the bottom of the screen and back.
  1512.     (the starting position is 7750, 100 and the ending position is 7750, 5000)
  1513.     You can find information about the Move command in the Using OpenScript manual.
  1514.     For one solution, switch to Reader level and click on the "Hint" button.
  1515. 3.    How could you move the dot in a square around the screen??
  1516. Moving a Draw Object Using Steps
  1517. to handle buttonUp
  1518.     move ellipse "dot" to 7750, 100
  1519.     step yPos from 100 to 5000 by 100
  1520.         move ellipse "dot" to 7750, yPos
  1521.     end step
  1522.     step yPos from 5000 to 100 by -100
  1523.         move ellipse "dot" to 7750, yPos
  1524.     end step
  1525. end buttonUp 0,var2
  1526.         end step
  1527.         step var3 from 300 to 1 by -10
  1528.               move ellipse "dot" by var3,0
  1529.         end step
  1530. end buttonUp
  1531. -- shows 
  1532. hides the hint
  1533. "Hint"
  1534. buttonUp
  1535. buttonUp
  1536. Fun with Dot
  1537. etting text
  1538.     When ToolBook sends a message the
  1539.     first object that receives the message,
  1540.     whether or not it handles it, is recorded in
  1541.     the special variable, target. Now, try the following:
  1542. 1.    Edit the page script and enter the script you see to the right.
  1543. 2.    Switch to Reader level and click on one of the states in the map.
  1544.     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.    
  1545. ning all the states. If you feel adventurous, you can ungroup the map and look at how each of the states is named.
  1546. A buttonUp handler Higher in the Hierarchyyy
  1547. State Name
  1548. Washington
  1549. Montana
  1550. Oregon
  1551. California
  1552. Nevada
  1553. Idaho
  1554. Wyoming
  1555. Arizona
  1556. New Mexico
  1557. Colorado
  1558. Texas
  1559. Oklahoma
  1560. Kansas
  1561. Nebraska
  1562. South Dakota
  1563. North Dakota
  1564. Minnesota
  1565. Missouri
  1566. Arkansas
  1567. Louisiana
  1568. Wisconsin
  1569. Illinois
  1570. Kentucky
  1571. Indiana
  1572. Tennessee
  1573. Mississippi
  1574. Alabama
  1575. Georgia
  1576. Florida
  1577. North Carolina
  1578. South Carolina
  1579. Michigan
  1580. Pennsylvania
  1581. West Virginia
  1582. Virginia
  1583. Maryland
  1584. Delaware
  1585. New York
  1586. New Jersey
  1587. Vermont
  1588. New Hampshire
  1589. Maine
  1590. Massachusetts
  1591. Connecticut
  1592. Rhode Island
  1593. Alaska
  1594. Hawaii
  1595. Hawaii
  1596. Hawaii
  1597. Hawaii
  1598. Hawaii
  1599. script1
  1600.  to handle buttonDown
  1601.         set text of field "State Name" to name of target
  1602. end buttonDown
  1603. to handle buttonUp
  1604.         set the text of field "State Name" to null
  1605. end buttonUppppppppp
  1606. 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.
  1607.     If you want to see one solution, switch to Reader level and click on the Hint button.
  1608. 2.    Test the handler from the Command window by typing:
  1609.     factorial(5)
  1610.     The answer should be 120.
  1611. A Factorial Function, cont.
  1612. "Hint"
  1613. buttonUp
  1614. buttonUp
  1615. to get factorial n
  1616.     if n is 1 then
  1617.         return 1
  1618.     else
  1619.         return factorial(n-1) * n
  1620.     end if
  1621. fx commands
  1622. -- Generic 
  1623. install the 
  1624. Effect 
  1625. c"Script" 
  1626. buttonUp
  1627. buttonUp
  1628. Script
  1629. 180,50,100
  1630. Effect
  1631. 0,100,0
  1632.     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.
  1633. 1.    Switch to Reader level.
  1634.     Most messages aren't sent at Author level.
  1635. 2.    Click on one of the scripts.
  1636.     This puts the script in the "Effect" button.
  1637. 3.    Click on the "Effect" button.
  1638. 4.    Try it with the other scripts.
  1639. 5.    Make up an effect script of your own.
  1640. ript2, switch to Reader level and click the button.
  1641. 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.
  1642. fxZoom, fxDissolve and fxWipe
  1643. Landscape
  1644. Effect
  1645. Effect
  1646. script3
  1647. to handle buttonUp
  1648.     fxDissolve slow to next page
  1649. end buttonUputtonUp
  1650. script2
  1651. to handle buttonUp
  1652.     fxZoom to next page at sysmouseposition
  1653. end buttonUpppppppppp
  1654. script1
  1655. to handle buttonUp
  1656.     fxWipe left to gray
  1657.     fxWipe right to next page
  1658. end buttonUpd buttonUpd buttonUp
  1659. !Z!w!
  1660. fx commands II
  1661.     ! 4    
  1662.  Go Back
  1663. buttonUp
  1664. buttonUp
  1665.     You've made it!
  1666.     Now, switch back to Reader level and click on the "Go Back" button.
  1667.