[ Home | Prev | Next ]

Chapter 10: continued


Write the Needed Scripts

We need six scripts, as we've already determined. In a large suite, it's helpful to write the scripts in an order that lets you test each one before going on to the next. For this suite, the order doesn't matter much. Let's start with the easy ones. (Actually, in "real life," sometimes it's better to start with the hard ones!)

To write the script that will open the list of accomplishments for editing, we need to know what we want to call the outline. If we were going to incrementally test at each step, we would create the outline now; we're going to forego that nicety to make the explanation simpler. Let's call the outline "Accomplishments." Now we're going to write a script to open that outline.

Create a new entry in the suites.Activities table. Name it editList. Identify it as a script object and open it. This script is simple:

edit(@Activities.Accomplishments)

Because suites is a known path in Frontier, we don't need to include the full name of the table, only enough to enable Frontier to locate it. Once you've entered this script, click on the "Compile" button to be sure there aren't any errors. Then close the script-editing window.

Let's take care of our other small scripts while we're at it. The three standard scripts are easy. First, create a script called "installMenu" and enter:

In the previous script we took a shortcut, omitting the "on" handler. In this script, we included it. The single script statement uses the Frontier verb menu.install to install the menubar stored in the Object Database at the address Activities.menu. It returns the result (hopefully "true" to indicate success!) to the calling routine.

Scripts for the other two standard items can be associated directly with the menu item, we don't need a script object in the table. Go to the Menubar editor, click on the "Edit Menu" item and click the "Script" button. Type this line:

edit(@Activities.menu)

Similarly, open the "Edit Table" item's script and type this line:

edit(@Activities)

Now let's work on the script that will tell us how many items have been added to the list of our accomplishments since we opened it. This routine takes a bit of thought. There are at least two possible approaches we could take. We could write a script that would open the list (the outline) and count the number of items in each heading, accumulating the total. Or, we could create a variable in our Activities table and increment it each time we add an item to the list. This latter script would execute more quickly but has a drawback. If the user adds or deletes items from the list manually, outside the Tracker suite, our total won't be right. For our present purpose (a simple example!), we've decided this problem is not critical.

We decide, then, to store the number of accomplishments in the table in a numeric entry called howMany. Again, if we wanted to test this function incrementally, we'd have to create this entry now. Let's just write the script as if the entry existed; we'll create it when the time comes.

When the user asks for a count, how can we display the information? Look through the Dialog verbs using the Frontier verb list or DocServer; dialog.notify looks good. Let's create a new script entry in the Activities table, and name it tellHowMany.

Note that the plus sign is a string concatenation operator, used here to insert the value from Activities.howMany into the middle of the string we display to the user on request.

We're down to our final script, the one that does all the hard work in our suite. Create a new script in the Activities table. Call it addAccomplishment. Here is the UserTalk code that goes into this script; it is explained below.

This script begins in the usual way with the keyword "on." The first line declares a local variable called "temp." We give this local variable an initial value of an empty string; otherwise, Frontier will assume it is a number and give it a value of 0. Normally that wouldn't matter since Frontier would change the variable to a string whenever string data was assigned. In this case, however, the value or text of the variable is the default answer for the user in the dialog.ask verb.

The second line sets the target of the following verb operations. The target is an important notion in UserTalk; see DocServer for details. Essentially, setting the target opens an invisible outline (or word processing text) window; subsequent outline ("op") commands operate on the contents. You could open a visible window using the edit verb, but it's nice to enter the data without bothering the user with the details.

The next two lines use another dialog verb to ask the user to describe an accomplishment. The user may press the "Cancel" button so we use "if not" to catch the result of false and stop (by returning out of the "on" handler to whatever called the script). We store the user's response in temp.

The following five lines update the outline. First, we place the cursor on the first summit in the outline. Then we use the op.go verb to move all the way to the last summit. (Note that if there is only one summit, no movement takes place and the verb returns a value of false. Normally, we'd check for that condition, but since our only intent here is to make sure we're on the last summit in the outline, this situation causes no harm.)

The next line uses op.getLineText to read the last summit in the outline and compares it to the current date. If they are not equal, the script inserts a new summit with the current date. Either way, the script inserts the accomplishment to the right of (under) this last summit.

We increment the value of the counter that's keeping track of how many accomplishments we've added. Finally, we return a value of true indicating successful completion.

Attaching Scripts to the Menu

Now that the scripts are written, we could test them before we add them to the menu. Since this suite is pretty simple, we'll proceed without testing.

If the Tracker menu is still on the menu bar, type Command-M to edit its menu. Otherwise, open the menubar in one of the other usual ways.

Select the item "How Many Activities?" and add the following as its script:

Activities.tellHowMany()

Similarly, open the "Edit the List" item's script and type:

Activities.editList()

Finally, for the "Add an Item" menu item's script, type this line:

Activities.addAccomplishment()

This would be a good time to save the root if you're as paranoid as we tend to be about computer systems, power companies, lightning bolts, and the like.

Create Needed Objects

Besides the menu and the scripts, this suite calls for two pieces of data to be stored in its table: an outline called Accomplishments and a number called howMany. If you were creating the suite for others to use, you should modify the scripts to create the items if the don't exist. For this example, we'll just create them by hand.

Create a new number object called howMany. Notice that Frontier obligingly initializes its value to 0, which in this case is exactly what we want. If we wanted it to have some other initial value, of course, that would be easy to assign.

Now create a new outline object called Accomplishments.

You are ready to test your suite.

Testing the Suite

There are many approaches to testing. The goal is to test every script under as many circumstances as you can think of. Here's one way to test the Tracker suite.
  1. In the Quick Script window, type and execute this line to start the suite running:
    Activities.installMenu()
  2. From the Tracker menu, select "Edit Menu." Make sure the menubar editor appears, then close it.
  3. Select "Edit Table" from the Tracker menu; make sure the table appears, then close it.
  4. Select "How Many?" from the Tracker menu. It should display a dialog box telling you that you have added 0 items to the log.
  5. Select "Add an Item" from the menu or type Command-I. When the dialog appears asking you what you accomplished, type something short that you'll remember. Press Return or click the "OK" button.
  6. Select "How Many?" from the Tracker menu. It should show a value of 1.
  7. Add another item or two, repeating the previous two steps.
  8. Now select "Edit the List" from the Tracker menu. When the list appears, confirm that it has today's date as its first summit and your items under it. (There may be a blank summit at the top. It's harmless, but if it bothers your sense of aesthetics and balance, delete it.)
  9. Edit the date so that it is yesterday's date (or last Tuesday's or the anniversary date of the last Mets' World Series victory; whatever you like as long as it's not today).
  10. Close the outline.
  11. Add another item or two to the list.
  12. Select "How Many?" from the Tracker menu and satisfy yourself that the counter is indeed keeping accurate track of your life.
  13. Select "Edit the List." Now you should see two summits, one with the date from Step 9, the other with today's date, each with its items beneath it.
That's probably sufficient testing for this suite. Unless you ran into bugs you had to fix, you're nearly done.

Adding the Suite to a Menu

It would of course be terribly inconvenient to type the script name into the Quick Script window each time you want to run your Tracker suite. Let's add a menu item to the Suites menu. (Actually, you can add to any of the customizable menus, but the suite menu is the recommend location.)

You already know how to edit menus, so we'll just zip through it quickly. Open the Suites menu with the Option key held down. Pick the place you want this suite added, position the bar cursor on the previous item, press Return, and type "Tracker" (or any other name that will remind you of the suite's purpose). Now click on the script button and type:

menu.addSuite(@Activities)

Notice that the menu.addSuite verb requires the address of the table whose menu you wish to add. Because the suites sub-table is part of the Frontier path table stored in system.misc.paths, you need not supply a full address to its location in the Object Database.

Testing Launch from Menu

Select the "Tracker" (or whatever you called it) option from the Suites menu and ensure that it displays the right menu and that the menu options all work as expected.

Saving the Modified Root

Save the root file again if you want to keep your changes. Congratulations! You have added a suite to your Frontier environment and learned some basics about scripting Frontier's outliner.

Contents Page | Previous Section | Next Section -- Agents
HTML formatting by Steven Noreyko January 1996, User Guide revised by UserLand June 1996