|
AppleScript Tutorial 2 - Customizing Scripts |
|
2004-05-31 |
http://www.barefeetware.com /applescript/tutorial/02/ |
|
Description |
This tutorial, the second in the series, examines and customizes some prepared scripts to demonstrate how scripts are structured and modified. |
|
Enhancement Pack |
This tutorial uses the scripts in the Enhancement Pack for AppleWorks. If you dont already have them, download and install the Enhancement Pack. |
|
AppleScript |
The index of this AppleScript tutorial series. |
|
Tutorial 1 |
If you have not already completed tutorial 1, go back to it now. |
|
Tutorial 3 |
Go on to start learning about scripting dictionaries as you begin to learn how to write your own scripts. |
|
Duplicating ScriptsThis second tutorial should give you answers to:
Well continue to look at some scripts that were prepared earlier, since this lesson focuses on customizing existing scripts, not yet writing new ones.
- How do I open, duplicate and save a script?
- What does a script look like?
- How can I change a property of a script?
- How do I make a script into an application that I can double click in the Finder?
Creating Sub-MenusYou can use one script as the basis for another script. By duplicating the original, altering the duplicate and renaming it, you can easily create a new, slightly different script in the Scripts menu.Exercise 1: Duplicating the Small Caps Script
Lets duplicate the Small Caps script. (So we can later modify the duplicate to behave slightly differently to the original).
- In AppleWorks Scripts menu, select Open Scripts Folder.
- In the list of Scripts that appears, you should see the Small Caps script, in the Text folder. Click once on it to select it.
- Duplicate the file (eg by selecting Duplicate in the File menu).
- Rename the Small Caps copy file to be Small Caps 60%, as shown in Figure 1.
![]()
Figure 1: Two Small Caps Scripts.
- In AppleWorks, check that the Scripts menu now contains Small Caps 60% as well as the original Small Caps (see Figure 2). If it does not show the new script (it probably will for AppleWorks 5 but wont for AppleWorks 6), quit and relaunch AppleWorks and check the Scripts menu again. The new Small Caps 60% script does the same job as its original, but well change it soon.
![]()
Figure 2: A Duplicated and Renamed Script.
Examining a ScriptAs you add more scripts, your Scripts menu can get quite long. As youve already seen, the Scripts menu lets you organize your scripts into separate sub-menus (such as Text or Spreadsheet), by using folders within the Scripts folder.Exercise 2: Building a Small Caps Sub-Menu
AppleWorks 6 unfortunately only permits one level of Scripts sub-menu, which means that only the first level of folders within the Scripts folder is recognized. So you cant, for instance, create a new sub-menu within the existing Text sub-menu. But you can create new folders in the Scripts folder to become one level sub-menus.
Try this exercise to create your own sub-menu within the Scripts menu.
- If not still showing, open the AppleWorks Scripts folder.
- Make a new folder (eg using the Finder File menus New Folder command).
- Name the new folder Small Caps.
- Move the Small Caps 60% file into the new Small Caps folder.
- Rename the Small Caps 60% file to be just 60% (ie remove Small Caps , including the space, from the file name). Duplicate it and name the duplicate file 80%. The folder structure should now should look like Figure 3.
![]()
Figure 3: New Small Caps Folder Containing Renamed Scripts
- Return to AppleWorks and click on the Small Caps sub-menu in the Scripts menu, which should look like the example in Figure 4 (if not, quit and relaunch AppleWorks). Dont select any command in the menu for the moment.
![]()
Figure 4: Small Caps Sub-Menu in the Scripts Menu
PropertiesAs a script user, you only need to know what the script does and how to run it, such as selecting it in the Scripts menu. Up to this point in the tutorial series, the exercises have covered using scripts. Now, this tutorial deals with examining the contents of a script and customizing how it works. But you still dont need to type any code.Exercise 3: Opening the Small Caps 60% Script
A script is saved in a special AppleScript file format. When run, AppleScript executes the instructions contained within the script. To view and edit these instructions, you need a script editor, the most common of which is Apples Script Editor, supplied as part of the AppleScript software.
All of the examples in this tutorial will show the Apples Script Editor, unless noted otherwise. All of the font formatting shown here (such as italic gray text for comments), uses the default settings for Script Editor.
As shown in Figure 5, the script window contains two sections (also called window panes) the description and the script text. The description displays freeform text that explains the purpose of the script, but is ignored by AppleScript. The script text section of the window contains the instructions that AppleScript follows to achieve the desired result.
The italicized text are comments that are ignored by AppleScript but can be read by the user when the script is opened. Comments provide more detail to a scripter (the person who writes scripts) as to what each part of the script does.
Lets take our first look at the contents of a script.
- If not still showing, open the AppleWorks Scripts folder. (You can do this by selecting Open Scripts Folder in AppleWorks Scripts menu.)
- Open the 60% file you created in the Small Caps folder. You can open it just by double-clicking on its icon. (If your computer does not launch Script Editor when you open a script, it is probably not on your system. You should reinstall the AppleScript software from your Mac OS CD).
- If you cannot see the Description pane section at the bottom, drag the divider up from the bottom to reveal the collapsed pane. (Script Editor windows in Mac OS 9 and earlier appear slightly different, but with the same components.)
- Have a close look at the contents of the script. It should look like Figure 5. If the script text on your screen is all one font format (such as purple plain text), then it probably needs to be compiled in order to format the text according to purpose. Well do that in the next exercise.
![]()
Figure 5: Viewing a Script in Script Editor.
- Leave the script open for the next exercise. Dont worry about the instructions in the script text, for now. The scripts that you write in following tutorials will be much simpler natural language.
Applets and DropletsThe script text usually starts with a list of its properties, as shown in Figure 5. Each property of a script can be text, a number, or a reference to a file, among others. For the end user, a property provides an idea of the settings used in the script and makes it easy to alter the scripts behavior. If you open a script and see a property declaration that youd like to change, you simply edit it and save the script.Exercise 4: Adjusting a File Property for a Library
Scripts often include properties that load other scripts before being saved. Such properties are called libraries, and usually invoke the load script command, as shown in Figure 5 for the anyLib property. If you install a script on a new computer, then modify it, when you compile the script, AppleScript checks the syntax (confirms that the script text makes sense) and needs to locate the included library files. If it cant, then you need to change the path to match the location on your computer. This should be clearer after you complete the next exercise.
Lets modify a script to force AppleScript to check the syntax and locate the library files. Most scripts in the Enhancement Pack include properties that rely on the librariesPath to locate some script files.Exercise 5: Changing the Small Caps Reduction Factor Property
- You should still have the 60% Small Caps script open from the previous exercise.
- Click at the end of any line in the script text and type a space (without replacing anything). This minor change marks the script syntax as uncompiled. (In old versions of Script Editor, an uncompiled script is evidenced by the Check Syntax button becoming enabled.)
- Click the Compile button (or the Check Syntax button in old Script Editors) or hit the Enter key. If your copy of AppleWorks is not installed in exactly the place expected by the script, or your hard disk is named differently, then the script editor will display the error message shown in Figure 6.
![]()
Figure 6: Indication That the librariesPath Property Needs Correcting
- If you do see the error in Figure 6, then change the text inside the quotes after property : librariesPath to match the location of the Scripts Support:Libraries folder on your computer. For instance, if you have AppleWorks 6 installed in the default location on your startup disk called My Disk, then you should correct the line to be:
property librariesPath : "My Disk:Applications:AppleWorks 6:AppleWorks Essentials:Scripts Support:Libraries:"
- Try the Compile button again to verify. When compiling no longer displays an error, your alteration is successful. Keep the script open for the next exercise.
When you used the Small Caps menu item earlier in Tutorial 1, it made the lowercase letters in each word 80% of the size of the uppercase letters. As you can see in Figure 5, the first property of the Small Caps script is the reductionFactor. As its name implies, this property affects the reduction factor of the trailing letters in each word. Lets change it.
- In the 60% Small Caps script window, change the reductionFactor from 0.8 to 0.6 by replacing the 8 with a 6. That is, change the script from:
property reductionFactor : 0.8
to:
property reductionFactor : 0.6
- Compile the script (by clicking the Compile button).
- Close the script and save the changes.
- In AppleWorks, create a word processing document, type some words that include some upper case letters, select those words, and select Small Caps -> 60% in the Scripts menu. The lowercase letters should be converted to uppercase but 60% the size, similar to Figure 7.
![]()
Figure 7: Small Caps at 60% Reduction Factor
The scripts we have used so far are more precisely called compiled scripts. You can also save a script as an application called a script application or an applet. Like any other application, you can run applets by double-clicking on them or by adding and clicking them in the Dock (or selecting them from the Apple menu in Mac OS 7-9 or earlier). Some applets can also accept files dragged and dropped onto them we call these files droplets.Exercise 5: Saving As an Applet
The icons for compiled scripts, applets and droplets appear in Figure 8, and a sample of the Finders Get Info window for a compiled script appears in Figure 9.
![]()
Figure 8: Compiled Script, Applet and Droplet Icons for Mac OS X and Earlier Systems
![]()
Figure 9: Get Info Window for a Compiled Script
Compiled scripts and applets (including droplets) behave differently. If you double-click on a compiled script, it opens in your script editor, ready to be edited. Double-clicking on an applet runs the applet. (To edit an applet you have to open it from within the script editor or drag and drop the applet onto the script editor.)
(In AppleWorks 6, compiled scripts and applets in the AppleWorks Scripts folder will both appear in the Scripts menu. In AppleWorks 5, only compiled scripts are displayed.)
The List Disks & Space script, installed by the Enhancement Pack in the AppleWorks Scripts menu, creates a spreadsheet of information about disks in the Finder. It may be useful to be able to run this script without having to first launch AppleWorks. So, lets save it as an applet on the desktop.Supplementary Exercises
- In AppleWorks Scripts folder, in the Document folder, open the List Disks & Space script. It should open in Script Editor.
- If the script is not compiled, compile it, correcting the librariesPath property for your disk, if necessary.
- In Script Editors File menu, select Save As.
- Change the location to the Desktop, or another convenient place on your computer.
- In the Format pop-up menu, select Application (or Applet if using Mac OS 9 or earlier). The Save As dialog box should look something like Figure 10.
![]()
Figure 10: Save As an Application
- Click the Save button to save the script as an applet on the desktop. Close the script window.
- Switch to the Finder. Double click the new List Disks & Space applet that you just saved (probably on the Desktop). It should display its description, as shown in Figure 11.
![]()
Figure 11: Applet Run Window
- Click the Run button. After a few seconds, the applet should create a new spreadsheet in AppleWorks containing information about the disks mounted in the Finder, then automatically quit. To see the new spreadsheet, switch to AppleWorks. It should look similar to Figure 12.
![]()
Figure 12: Result of List Disks & Space
Tidying UpTry this supplementary task:
- Duplicate the 60% script you created earlier to create a third script that reduces the text to 40% of its original size (ie change the reductionFactor property to 0.4). When you are done, your Small Caps sub-menu should offer three choices: 40%, 60%, and 80%.
ConclusionYou dont need anything created in this tutorial chapter for the subsequent exercises. So, unless you want to keep them, you can:
- Trash the Small Caps folder (and the 60% and 80% scripts inside it) from AppleWorks Scripts folder. Do not trash the original Small Caps script in the Text folder.
- Close any tutorial related windows still open in Appleworks or Script Editor, without saving.
- Trash the List Disks & Space applet from the desktop.
Next...So now you should have some idea of what scripts look like, how to change the Scripts menu, and how to make basic modifications.
In the next tutorial you will start to write scripts from scratch, after examining the dictionary of instructions that an application understands.
© 1998 - 2004 BareFeetWareIn the next tutorial you will examine the dictionary of instructions that an application understands, so you can go on to write scripts of your own.
Please email us any queries about this page.