[ Home | Prev | Next ]

Chapter 8: continued


Example: Alias Maker

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. From the "On-Line Docs" subment of the "UserLand" menu, select "Frontier Verbs". 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:

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. You need not do so, of course.

You might want to try modifying this script so that it will also let the user find a folder to alias and place on the Apple Menu. That is perfectly legal under System 7, but this script won't allow that because we use file.getFileDialog. Be careful, though; just switching to file.getFolderDialog will make it impossible to alias files. You'll probably want to ask at the outset if the user wishes to create an alias of a folder or a file. Check out dialog.threeWay for this one.

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