Frontier Scripting Tutorial
About This Tutorial
What Does Frontier Do?
Keywords, Handlers, Verbs, and Calls
Loops, Variables, Parameters, and Conditionals
The Handler Rule
Returns, Addresses, and Dereferencing
Scope and With
Strings and Files
Outlines and Tables
Running, Debugging, and Getting Help
Datatypes
A Real-Life Problem
String Parsing and Substitution
Manipulating Files and Folders
Final Touches
|
|
Let's script
As promised, we conclude the tutorial by bringing to bear what we have learned to write a genuine, working, useful script.
To be honest, we are not the first people to write this script; Dave Winer has already written it. But he wrote it as an example of something you could have written with very little Frontier training.
He also wrote it to wow the audiences at Internet World in Los Angeles, in March 1997; and it did. This is a script with plenty of power, plenty of razzle-dazzle, and plenty of practical utility.
If you like, you can read Dave's speech that he gave as part of the demonstration at the Expo. The demonstration itself was part of Gil Amelio's keynote speech, so part of its purpose was to make the Macintosh look good. The speech is at http://www.scripting.com/davenet/stories/amelioDemoScript.html.
If you're reading this tutorial on-line, you'll need to download this material. It's available as a Windows Zip file or a Macintosh binhex file.
If you downloaded this tutorial as a whole, or if it came with Frontier, it should contain the AmelioWeb source as a folder called "AmelioWeb." If it's not included with this tutorial, you can download it above.
This gives you, besides Dave's version of the script we'll be developing, the objects on which it operates. You need this so that we're talking about the same things as we progress through the development of the script.
Defining the problem
The problem is as follows:
- We have a number of textfiles; basically, these are email messages, or derived from email messages; they contain no HTML.
- We wish to generate Web pages from the email messages, in accordance with the general look of our Web site.
- We should generate a final Web page which is a table-of-contents with links to the other pages.
Now, those of you have gone through the Web Tutorial or are familiar with Frontier's Web site management capabilities are probably thinking of using those capabilities to perform this task. But no. We are going to perform this job as a one-off, developing the script from the ground up.
In essence, we'll be writing a mini-version of Frontier's own Web site management routines. We will take advantage of only one pre-existing Web site routine: the verb html.processMacros (in the suites table). This will perform the basic HTML generation familiar to users of Frontier's Web facilities: it will put <p> where it comes across two return-characters in a row, cause any URLs to become live links, and Latin-encode any high-ASCII characters in our text.
Otherwise, everything that this script is to do, we ourselves will cause it to do, step by step.
More detail
Let's look at the problem in some more detail.
Open, with any word-processor, the first of the files in the Source File folder, face_time.txt. This has the features that we will assume all the textfiles have.
Two return-characters in a row are significant for separating paragraphs; a single return-character is not, since this represents merely the wrapping introduced by the file's once having been an email message. The first paragraph is assumed to be the title; the second paragraph is a byline; the third paragraph will be assumed to be a kind of significant lead, to which we will give some special formatting just to make the page look good, and we will also use it as a subtitle in the table-of-contents page.
The HTML template into which we are to copy this material has already been written for us, based on the look of Apple's Web site. You can open and examine the template, which is called template.html, in a word-processor if you like; but let me summarize for you what it contains. In effect, it goes like this:
<HTML>
<HEAD>
<TITLE><<title>></TITLE>
blah blah blah
</HEAD>
<BODY BGCOLOR="white" TEXT="#000000">
blah blah blah
<TABLE BORDER=0 WIDTH=500>
<TR>
<TD VALIGN=top WIDTH="100"><br>
<img src="images/gil.gif" BORDER="0"
WIDTH="90" HEIGHT="90" ALT="Next Link">
</TD>
<TD VALIGN=top ALIGN="LEFT" WIDTH="385">
<HR>
<H1><I><<title>><BR><BR></I></H1>
<H4><<subtitle>></H4>
<HR>
<<bodytext>>
</td>
</tr>
</table>
blah blah blah
</BODY>
</HTML>
Parsing the template
The template describes a header area and a footer area, and between them, a table. This table has two columns: the first holds a picture of Gil Amelio looking appropriately feisty, and the second is to contain our text.
Into the template we have inserted markers showing where the various pieces of each email are to go. These markers are delimited with "<<" and ">>", since we may reasonably assume that these collocations of characters do not occur for any other purpose in the template.
So, we have put "<<title>>" in two places: between the <TITLE> tags, to generate the titlebar of the browser's window, and after the first horizontal rule in the text part of the table, with <H1> and <I> formatting, to give a visible title. The "<<subtitle>>", the lead paragraph, follows the visible title, in <H4> formatting. Then, after a second horizontal rule, "<<bodytext>>" shows where the balance of the email's contents should be placed.
(There is, in fact, also a "<<url>>" marker in the <META> material at the top, but we'll ignore this for purposes of the tutorial.)
Batch processing
The problem is now very easily defined. Let us ignore the matter of the table-of-contents page for the moment. Then we are faced with a batch-processing problem: we must do what amounts to the same thing to a bunch of files in a folder.
But this, as we know, is trivial; thanks to Frontier's "fileloop" construct, if we can do something to one file, we can do it to them all.
For each email file, what we want to do is this. We want to read the template into memory, as a string, and we want to read an email file into memory, also as a string. We want to parse the email string into its title, its subtitle, and its bodytext; we also want to figure out what the URL of the resulting file is going to be.
Then we want to find the markers in the template string, and substitute for each one the corresponding stuff we just parsed from the email string. Finally, we want to write the template string out as a new textfile.
Basically, that's all there is to it. We already know enough about the basic structures of Frontier scripts to develop a program to do this; verbs that we don't know, we can discover with helps discussed in the previous chapter. Let's get started.
|