[ Home | Prev | Next ]

Chapter 2: continued


A Sample Frontier Scripting Session

We know how eager you are to get your hands on Frontier and actually do something. So, let's take a few moments to walk through an abbreviated but realistic scripting session with Frontier. Don't worry if, along the way, some of the concepts seem a little fuzzy; later chapters will clarify the ideas that we will only have time to touch on here.

Most Macintosh owners we know suffer from hard disk clutter. They accumulate multiple copies of files they use frequently, temporary files that never seem to go away, unintentional backups of various kinds, and the kind of digital clutter that turns a pristine hard disk into someone's attic. Often, they don't realize the problem they've created until they save a document they've been working on for the past two hours without saving it and get the dreaded "disk full" message.

A script that would let you know what percentage of your disk is still available would help you avoid such situations. Let's build one in the UserTalk language. Fortunately, UserTalk includes a couple of verbs that are going to make this script quite easy to write.

In true Frontier fashion, we'll let the user decide what kind of safety margin is appropriate for disk-full situations. We can store this information in the Object Database so it's easy to view and change. Frontier provides a special place for you to add your own data (and scripts!), the "people" table. When you ran Frontier for the first time, you were asked for your name and initials. Frontier then created a sub-table with your initials. For this manual, we use the initials "ME". When we refer to the people.ME table, remember to look in the people table whose name has your initials.

Creating a Safety Margin Entry

Open the people.ME table by choosing "Open people.ME" from the Custom menu. Assuming this is your first time to open this table, it should look like Figure 2-23.


Figure 2-23. Newly Initialized people.ME Table

Press Command-Return to create a new table entry. Frontier leaves you in a position to name the newly created object. Call it "diskSafetyMargin." (Note that we've created the variable as if it were one word long. Frontier permits the use of multi-word variable names but we don't want to get into the slight complication in scripting that using such variables introduces.)

Tab to the "Value" column and type the number 10. Press the Tab key or simply click in another part of the table to complete your entry. Notice that Frontier looks at the value you entered and determines that you want to store a number in this cell, so it provides the word "Number" in the "Kind" column of your new entry.

Now press Enter to leave text editing mode and return to table mode. The entire row where your new variable is stored will be selected, as in Figure 2-24.

Don't worry that the new variable isn't sorted correctly with the rest of the entries. When you close this window and re-open it, Frontier will take care of that for you. (If you're the kind of person who really worries about such things, click on the "Sort" popup menu and choose "by Name" from the resulting popup. You can also just click on the "Name" label in the Table window. Frontier will restore sorted order to the table.)


Figure 2-24. Completed Variable Entry

Writing the Script, Step 1

Now we have a variable against which to compare our disk's percentage of free space, so let's write the script that will do the comparison.

Select New Script from the Table menu. In the dialog, type "diskPctFree." Now double-click on the item marker next to this newly created script entry. Frontier will zoom open a script-editing window.

The first thing we have to do is help Frontier identify the disk we want it to examine. The file.getSystemDisk verb will tell Frontier the name of the disk from which we boot our Macintosh. Type the following line exactly as it appears here:


Writing the Script, Step 2

Now we want our script to calculate the percentage of free space on this hard disk. Press Return. Frontier gives you another item marker in the script-editing window. Type the following line into the script-editing window, exactly as it appears here:

This statement uses three Frontier verbs, which are, briefly:

By dividing these two numbers, we get a result like ".2187." So we multiply the result by 100 to get it into a format like "21.87." This is the percentage of free space on the disk. Note that UserTalk lets you "nest" any verb (function) inside any other verb. The return value of the innermost verb is used as the parameter to the outer verb.

Writing the Script, Step 3

Next, we compare the value of this calculation against the safety margin value we set earlier. If the disk's free space is within limits, we display one message; if not, we display another. Here are the rest of the lines in the script:

Notice the indentation of the second and fourth lines. This structure is important. After you type the first of the above lines (which will become the third line in your script), press Return and then the Tab key to indent one level. When you enter that line and press Return, be sure to press Shift-Tab to return to the top level of the outline again. The same process will format the third and fourth lines correctly, as shown.

Compiling and Error-Checking the Script

Click on the "Compile" button in the script-editing window. If any errors result, click on the "Go To" button in the error window (see Figure 2-25). Frontier will take you to the point where the error occurred. Check your script to make sure it exactly matches what we have shown you.


Figure 2-25. Error Information Window and "Go To" Button

Here's the entire script for reference purposes:

Once you've successfully compiled your script, click on the "Run" button. One of the two messages should appear in your Main Window.

Congratulations! You've written your first Frontier script and it's a pretty useful little critter besides!

Connecting to a Menu

This new script seems somewhat inconvenient to use because you have to open your people.ME table, find the script, open it, and click on "Run." Actually, there are two other, somewhat faster ways to run the script.

You can type the script name into the Quick Script window, followed by a pair of parentheses, and it will run (see Figure 2-26). (Remember that all verbs require parentheses, even if they don't have any parameters.)


Figure 2-26. Running Your Script from the Quick Script window

You can also position the cursor anywhere in the line where the script is stored in the people.ME table and press Command-slash to run the script.

Both of these methods, while more convenient than opening the script itself, still require you to do more work than you should have to. After all, it's your script, right? So let's attach it to a new item in the Custom menu.

Hold down the Option key and pull down the Custom menu. With the Option key still held down, select "Open Examples" and release the mouse button. When the menubar editor opens, the bar cursor (highlight) should be on "Open Examples." Press Return. Frontier gives you a new menu entry with an item marker. Type the new menu's name. We'll use "Check My Disk" but you can call it anything you want.

Once you've named the menu entry, click on the "Script" button to open an empty script-editing window. Type the following line of UserTalk code exactly as it appears here:

Close the script-editing window and the menubar editor window. (This might be a good time to save your root file if you want to keep this menu item around. Press Command-S or choose the "Save" option from the File menu.) Click on the Main Window to remove any message that may be lingering from previous work we've done; it will then contain the usual status information. Now select "Check My Disk" from the Custom menu. In a moment, the appropriate message will appear in Frontier's Main Window.

Congratulations! You have successfully created a variable in the Object Database, written a useful script that refers to the variable, and customized Frontier's menubar to put the script at your fingertips. No matter how complex your scripts get -- and Frontier will let you write some extremely complex and powerful applications -- this process is almost identical.

Contents Page | Previous Section | Next Chapter -- Writing UserTalk Scripts.


HTML formatting by Steven Noreyko January 1996, User Guide revised by UserLand June 1996