An agent is a script that runs as a background process at specified intervals. Frontier can have, in theory, any number of agent scripts running at the same time; other activities proceed as normal. Agents run as long as Frontier is running, whether or not Frontier is the frontmost application.
Agents are stored in the system.agents table. By default, agents run once per second. Many agents don't need to run nearly so often; Frontier provides a verb that will put an agent to "sleep" for a specified number of seconds:
When Frontier encounters this line in an agent script, it sets up a 60-second timer. When the timer expires, the script executes again. This verb is only allowed in agent scripts. If you run an agent script directly, Frontier will generate an error.
Let's take a look at an agent script. Jump to system.agents.MinutesSinceShip; you should see something like the following.
Agents scripts have standard script editing windows. Click on the "Run" button in this window. The script will execute once and then you'll see a Frontier Error Info window:
Even though the script is stored in the proper table and running in the background as an agent, when you run it directly, that "instance" of the script is not running as an agent. Close the error window and the script's editing window. Now go to the Main Window and select "MinutesSinceShip" from the popup menu (the down arrow on the left side). The agent's message will appear in the window. If you are patient enough to wait, you will see that the Main Window is indeed updated every minute.
Although you can have multiple agents running, only one can display messages in the Main Window, the one selected in the popup. The other agents still run but their messages quietly disappear.
Example: Appointment Reminder
Let's build a useful agent script. This simple script will "wake up" every minute and check your scratchpad table at a location called nextAppointment. If the time it finds there matches the present time (or if it's later), it will sound the Macintosh speaker and display a dialog reminding you of the appointment.
First, create a new item in your scratchpad table, scratchpad.nextAppointment. To save you the trouble of entering the current date and time in the proper format, here's a cool trick. Tab to the value column for this item and type:
Press the enter key or just click the mouse in another cell. Frontier is smart enough to recognize that clock.now () is a script, so it runs it and places the result in the cell.
To run as an agent, a script has to reside in the table system.agents, so open that table and create a new script named "myReminder." Use the New Script command in Frontier's Main menu. Here's the code for this script:
The first line compares the date-and-time value stored at the nextAppointment spot in the scratchpad table to the present date-and-time. If it's later than the appointed time, Frontier sounds the speaker and displays a dialog informing you that it's time for your next appointment. If it's not time yet, the agent goes to sleep for 60 seconds and then checks again.
Does the agent work? Click "Compile" to activate it. If you have the normal StatusMessage agent selected in the Main Window's popup, it will show you the current time. Switch to your people table and edit the value of nextAppointment so it's a few seconds ahead. Be sure to press enter or click outside the cell to "commit" the change to the Object Database; otherwise Frontier still remembers the previous value. If everything is set up correctly, you should see the dialog when the actual time reaches the appointment time.
You could use this agent as a starting point for a script that would check multiple appointments in a table in the Object Database, perhaps to remind you when it is time to leave for a meeting or to eat your lunch.
Agents have all kinds of potential uses, such as backing up your hard disk to a network volume late at night, automatically saving your root file every hour, or watching a folder for new files to process. Use your imagination; you'll probably come up with many interesting applications.
© Copyright 1996-97 UserLand Software. This page was last built on 5/7/97; 1:11:08 PM.
It was originally posted on 9/24/96; 9:43:57 AM.
Internet service provided by Conxion.