[ Home | Prev | Next ]

Chapter 9: continued


Recently Changed Files

The next desktop script we'll look at is called "Recently Changed Files." As its comment informs us, it creates a folder and then places into that folder aliases of files that have changed since a user-selected date.

Here is what the script looks like in one view, where we've collapsed some of its lines for easier reading:

We first set up a date -- here it's October 1, 1991 but you could obviously change it -- and then we use that as the default answer as we ask the user to define which files are to be gathered. We make sure that this value is a date by converting it with the date verb. (Note that it would have been better programming style to declare "s" as a local. Frontier will treat an undefined variable as local unless there is an Object Database cell with that name. Since you probably haven't memorize the names of every cell in all the "global" tables of the Object Database, we suggest you use the "local" keyword.)

The next two sets of lines define local scripts called recentlyChanged and doGather. We'll come back to them shortly when they are called from the main script.

Before we start gathering aliases of all the recently changed files, we create a new folder called "Recently Changed Files", inside the folder from which the script is run. If a folder of this name already exists, we delete it before creating a new one. Note the "if not" technique to trap the rare case that file.newFolder returns false to indicate that it failed to create a new folder. (Perhaps the desktop script is on a CD or other locked volume.) It's a good habit to anticipate errors and design your code to handle them.

Now the script initializes a variable called ctgathered (the "ct" is a mnemonic for "count") to zero and then calls the local script doGather with the name of the folder in which it is running as an argument. Let's examine the doGather local script.

After defining a local variable, the script loops over all the files in the path. It then takes different actions depending on whether the "file" is a folder or a file, much as we saw with the CountWordFiles script in the last section. Let's open those two sets of lines; the doGather script now looks like this:

As you can see, if doGather encounters a folder, it first checks to make sure this isn't the folder we're in the process of creating (or the result could get very messy!) and then calls itself recursively, just as we saw with CountWordFiles. If the item it encountered is a file, the script displays the name of the file in Frontier's Main Window as a progress indicator, then calls the recentlyChanged local script. That simple script looks like this:

This script consists of one executable line that compares the date the file was last modified against the date the user has indicated we should use as a base date for gathering files. If the file's date of last modification is later than the date the user provided, this routine will return an answer of "true" to doGather. Otherwise, it returns false.

Now you can go back to doGather and see that it checks the return value from recentlyChanged. If the value is true, it creates a new alias for the file, places the alias into the destination folder we're building and then increments by 1 the counter that keeps track of how many files we gather.

When all of the files and folders have been examined, the script looks at the value of the variable ctgathered.

If at least one file has been found, it opens the folder so the user can see the results of gathering all of these aliases. Otherwise, it deletes the folder and informs the user that it couldn't find any files to gather.

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