frondoc picture

What is Frontier?
News & Updates

Web Tutorial
Scripting Tutorial
BBEdit Scripting

Mailing Lists
Sample Scripts
Verb Set
Frontier Site Outline


User's Guide
1 2 3 4 5 6
7 8 9 10 11

Apple File Edit
Main Open Suites
Web Window

Menubar Outline
Script Table WP

frondoc picture

Chapter 8: Scripting the Operating System

This chapter discusses one of the most powerful aspects of UserTalk: its ability to manipulate files, folders, and other system-level objects on the Macintosh.

The Verb Set

UserTalk includes a full complement of verbs to deal with the Macintosh Finder and System. These verbs can be divided into the following categories:

  • file verbs
  • Finder verbs
  • launch verbs
  • speaker verbs
  • system verbs

We'll look briefly at each of these verb types in the following sections. Details on each verb can be found in DocServer.

File Verbs

There are 88 file verbs in UserTalk. They enable you to:

  • copy, delete, move, rename, and create files and folders
  • find out if files and folders exist, when and by what application they were created, what version they are, when they were last modified, and how big they are
  • change file creator and type
  • compare two files
  • count the lines in a file
  • find text in a file's contents
  • display standard Macintosh file dialog boxes
  • find out how many bytes, files, or folders are in a folder
  • read and write text files
  • find out how many volumes are on-line, how big they are, how much information is stored on them, how much free space they have, and whether they are ejectable
  • break a full file path name into file, folder, and volume information
  • lock and unlock files and folders and find out if they are locked
  • create alias files, find out if a file is an alias, and follow aliases to their parents
  • copy a file to the System Folder

These are just some of the dozens of functions you can perform on Macintosh files, folders, and volumes with the file verbs.

Finder Verbs

The Finder is an application that comes with your Macintosh. (You'd be surprised how many people don't know that!) Finder verbs in UserTalk can:

  • change the way files and folders are viewed
  • open, close, grow, zoom, and move windows in the Finder
  • duplicate, move, drag, open, print, put away, and make aliases of files
  • restart or shut down the system
  • move Finder to the front of your application
  • show the Clipboard

All of these features work in System 7 or greater, the "Scriptable Finder" is not required. Frontier supports many additional features if you have the Scriptable Finder (bundled with System 7.5, available for earlier releases). Verbs that work with the original System 7 Finder are in the "FinderClassic" table. The new Finder table contains the original verbs plus about 20 new ones.

Launch Verbs

With the launch verbs in UserTalk, you can launch any object that appears in the Apple menu, any Control Panel, any application, or any code resource (such as an FKEY).

Speaker Verbs

You can set up sound parameters and activate the system's speaker with the Frontier speaker verbs.

System Verbs

System-level verbs deal for the most part with applications that are running. You can use Frontier verbs to:

  • find out if an application is running
  • find out how many applications are now running
  • retrieve the ID of any running application
  • find out which application is frontmost
  • move a particular application to the front
Many of these features are also available in the verbs that are specific to an application, e.g. uBASE.bringToFront instead of the more generic sys.bringAppToFront.

Using system oriented verbs in a script

One of the handiest things in Macintosh System 7 is the idea of an alias file. An alias file is a small file whose sole purpose is to point to another file. Any kind of Macintosh file - application, document, even Control Panel - can have one or more alias files. You can put an alias anywhere on the system and it will be able to find the original file.

Many people keep aliases of frequently used files in the Apple Menu Items folder in the System Folder so that they appear as items in the Apple menu. Carrying out this process has a surprising number of steps:

  1. Find the file you wish to alias, often digging through one or more folders in the Finder
  2. Choose "Make Alias" from the File menu
  3. Open the System Folder
  4. Locate and selecting the Apple Menu Items folder
  5. Drag the alias file into the Apple Menu Items folder
  6. Generally, rename the file
The UserTalk verb file.newAlias carries out these same tasks with one command. You simply tell the verb what file you wish to create an alias for and the name of the alias file, including the path name to the Apple Menu Items folder, and it takes care of the rest. Here's a typical use of this verb:

file.newAlias ("Disk:Apps:Hot App", "Disk:System Folder:Apple Menu Items:Hot App")

Pretty simple, right?

The problem, of course, is that this one-line script isn't very useful if you want to move another new alias file to the Apple menu. You'd have to re-type the full path and file names (or at least substantial parts of them) each time you wanted to do this task. Let's create a slightly more flexible version of this script.

Our script will ask the user to select a file. It will then ask for the name the alias should have in the Apple menu. Finally, it will create and copy the alias using the file.newAlias verb.

First, we need a line that will allow the user to select the file to alias. Choose the "Verb List" command in the "Open" menu. Scroll down to the file verbs and expand the outline (or do a search for "file.", including the period). Looking through the outline, you should see a verb called file.getFileDialog. It sounds like the right one. It takes three arguments: a prompt with which to guide the user, the address of the Object Database cell or local variable in which the user's answer is to be stored, and the type of file to be shown (using zero to indicate that any type is acceptable).

Here is a line of UserTalk code which will enable the user to pick a file of any type and put its full path name into a variable called f1 (which should first be declared as a local):

file.getFileDialog ("File to create alias for?", @f1, 0)

Next, we need a line that asks the user for the name of the file to create in the Apple Menu Items folder. We can use dialog.ask for this purpose (after creating a local variable "f2" to hold the new name):

dialog.ask("Save in Apple Menu under what name?", @f2)

Other than an escape valve for the user to cancel this operation, we are now ready to enter the code. Here is the full listing of the storeNewAlias script:

alias Picture

In good UserTalk style, we use the "on" keyword to define the script header. (To run or debug the script from the same window, add a "storeNewAlias ()" line at the end of the script as a "summit.") We declare two local variables, initializing them both in this case to empty strings. Next, we prompt the user for the file. Clicking the "Cancel" button in this dialog returns false, resulting in the script being exited and returning false to the calling script.

The verb file.fileFromPath extracts only the file name from the full path of the file chosen by the user; the result is used as the default response to the next dialog. Again, if the user clicks "Cancel," the script terminates and returns false to the calling script.

Finally, we use file.newAlias to create the alias of the file whose full path name is stored in f1 and store it in the right place on the system drive. Notice the use of the UserTalk verb file.getSystemFolderPath, which returns the path to the System Folder on the startup volume. This makes the script generic to all users' systems (including internationally).

When we're done, we beep the speaker and let the user know we've finished. Then we return true so the calling script will know all went well.

If creating aliases for the Apple Menu is something you do a lot, you might want to connect this new script to a menu command.

© Copyright 1996-97 UserLand Software. This page was last built on 5/7/97; 1:09:39 PM. It was originally posted on 9/24/96; 9:43:11 AM. Internet service provided by Conxion.