TOPIC
This article contains the Using AppleScript Documentation supplied with versions of AppleScript shipped as part of the Macintosh Operating System.
DISCUSSION
Contents Preface Welcome to AppleScript What is AppleScript? About this guide Balloon Help Where to go from here Section I Using the Script Editor Opening a script Recording a script Editing a script Checking the syntax of a script Formatting a script Saving a script Saving a script without compiling it Save options for script applications Saving a script as run-only Running a script Viewing AppleScript terms Using different scripting systems Using different AppleScript dialects Displaying the Result window Pasting an object reference into a script Section 2 Script Editor Command Reference File menu Edit menu Controls menu Font and Style menus Preface Welcome to AppleScript This manual is your introduction to the AppleScript application program. Use it to begin working with AppleScript. You can use AppleScript on Macintosh computers that use system software version 7.0 or later. If you're new to the Macintosh, read the information that came with your computer before you install the AppleScript application program. You'll need to know basic Macintosh operations and vocabulary to use AppleScript. What is AppleScript? When you use your Macintosh computer, you use the mouse and keyboard to open documents and application programs, move documents and file folders, enter information, print, and perform any other actions you want. AppleScript gives you a whole new way to work: You can give the computer a list of things you want it to do*a script*and let the computer do everything on your list. You can create a script by simply turning on the recorder and performing a set of actions. AppleScript keeps a list of what you do while the recorder is turned on. When you turn the recorder off, you can see the recorded script. When you run the script, your computer repeats your actions automatically. You can use the Finder and many application programs with AppleScript. Scripts can make the Finder or the program do many things for you. For example, a script might format a letter the way you want, save it in a particular folder, and print it. You can also use AppleScript to combine the capabilities of application programs. For example, a script might use the calculation capabilities of a spreadsheet and the formatting capabilities of a word processor to assemble an invoice. Scriptable application programs and the Scriptable Finder You can create scripts to control any scriptable application programs you have installed on your Macintosh. An application is scriptable when you can use AppleScript to control it. Many Macintosh programs are scriptable. If you're not sure your program is scriptable, contact the program's manufacturer for information. With system software version 7.5, the Finder (inside the System Folder) is scriptable. When you begin working with the Script Editor, you can use the Finder to try recording and playing a script. About this guide This guide tells you how to use the Script Editor to record scripts and how to change a script to do something different. The Script Editor is an application program you use to open and run scripts, make new scripts by recording or writing them, and save scripts. You don't need to know how to write computer programs to do anything described in this manual. Balloon Help You can use Balloon Help with the application program that come with AppleScript (the Script Editor). Choose Show Balloons from the Guide menu (near the right end of the menu bar). Note: When you point to any feature of the Script Editor application program, a balloon appears explaining the feature. Where to go from here For more information about using the Script Editor to work with scripts, open the file called "Using AppleScript part 2" to see Chapter 1 ("Using the Script Editor") and Chapter 2 ("Script Editor Command Reference"). If you wish to create more complex scripts, see an authorized Apple dealer for information about the AppleScript Scripter's Kit. The Scripter's Kit contains additional AppleScript software and manuals. Section 1 Using the Script Editor Use this chapter to find out how to use the Script Editor to: Opening a script You can use the Script Editor to open any script except those that have been saved as run-only scripts. (For more information about formats for saving scripts, see "Saving a Script" later in this chapter.) To open a script, follow these steps:
2. In the dialog box that appears, select the script you want to open and click Open. Recording a script You can use the Script Editor to record a series of actions. You can only record actions in a recordable application. For example, you can't record anything you do in SimpleText, because SimpleText isn't scriptable or recordable. In system software version 7.5, the Finder (inside the System Folder) is scriptable and recordable. An application is scriptable when you can use AppleScript to control it. It is recordable when you can use the recorder with it. An application can be scriptable without being recordable. Not all actions are recorded. If you move the mouse in circles, for example, it won't be recorded in your script. That's because moving the mouse doesn't result in a change in your document. The recorder records only things you do that change your document in some meaningful way. For example, typing a message in a text window is a meaningful change, because it makes something about the document different. Saving a file also results in a meaningful change. Clicking somewhere in the document doesn't result in a change in the document and isn't recorded. To start recording your actions as a script:
2. Click the Stop button. Editing a script You can edit a script much as you would edit any text document on your Macintosh computer. The editing actions you can take include:
Checking the syntax of a script You can use the Script Editor to check the syntax of a script. If the syntax is correct, the script is then compiled. When you record a script and don't make any changes to it, the Check Syntax button is not available. This is because the Script Editor does not record scripts with incorrect syntax. When you make a change to a recorded script, or when you write a script, you can use the Check Syntax button. When you write a script, all of the terms you type appear in the same font and size (the default is 10-point Courier). When you check the syntax, the Script Editor applies different fonts, sizes, styles, and colors to the different kinds of terms in your script. You can choose the formatting applied to the terms in your script. To find out how to make changes to script formatting, see the next section, "Formatting a Script." To check the syntax of a script: Click the Check Syntax button in the script window. The Script Editor identifies the first syntax error it finds for you by selecting the text that appears to contain the error. When the Script Editor finds a syntax error in a script, it does not apply any formatting to the script. If the syntax of the script is correct, the Script Editor compiles the script. Checking syntax won't find all the problems a script can have, but it will identify AppleScript expressions that are put together incorrectly. A script containing syntax errors can be saved only as text, not as a compiled script or script application. Formatting a script =================== Scripts have a number of different parts, including operators such as "+" and "=," keywords, comments, and more. The Script Editor keeps track of these different parts for you, and applies formatting to help you identify them. You can change the fonts, sizes, styles, and colors used for parts of scripts. The changes you make apply to all of your scripts, not just the active script. To change script formatting:
2. In the dialog box that appears, click a script element to select it. 3. Use the Font and Style menus to choose a font, size, style, and color for the script element you selected.
To format a script you must use the AppleScript Formatting command; you can't select part of a script and choose an item from the Font or Style menus. This is so that the formatting is consistent throughout the whole script. You can, however, use the Font and Style menus to format text in the description area of the script window. If you save the script as an application, the formatting you apply to its description appears in the script's startup screen. In addition to formatting your script by choosing fonts, sizes, styles, and colors, the Script Editor automatically indents some lines of your scripts. All lines within compound statements are indented. (A compound statement is one that takes up more than one line and includes other statements within it.) You can use the Tab key to indent lines in your scripts. If you use the Tab key in the middle of a line, however, the tab will be replaced by a space when you check syntax. To indent your scripts automatically, press Return at the end of each line. To force the next line of your script to begin at the left margin, hold down the Shift key and press Return. Lines in a script are sometimes too long to fit in the active window. You can make a line shorter by breaking it up into two lines with the continuation character. To insert a continuation character into a line in your script, hold down the Option key and press Return. A line broken into two or more lines with a continuation character is treated as a single line when you run your script. Saving a script You can save a script as one of three kinds of document: To save a script:
2. In the dialog box that appears, choose a location, specify a name, and choose a format for the script. 3. Click Save. Saving a script without compiling it When you save a script, it is usually compiled before it is saved. To save a script without compiling it: Save options for script applications When you save a script as an application, two additional buttons appear in the dialog box: Saving a script as run-only You can save a script in a format that can be run but cannot be opened in the Script Editor (or any other application). This is called a run-only script. To save a script as run-only: Running a script To run a script that's in the active script window: Click the Run button. You can also choose Run from the Controls menu. To stop a script that's running, press the Stop button (or hold down the command key and type a period). To run a script application Double-click the icon of the script application. You can force the script's startup screen to appear by holding down the Control key when you open the script application. Viewing AppleScript terms The Scriptable Finder and every scriptable application has its own dictionary, which is a set of AppleScript terms that you can use with it. You can open the Finder's dictionary or an application's dictionary in order to find out what terms are available and their syntax. To open a dictionary, follow these steps: A suite is a set of AppleScript terms that are related. The required suite is the most basic set of AppleScript terms. The standard suite is the set of terms that every scriptable application should support. Most scriptable applications (such as the Scriptable Finder also have their own suite of commands. The terms available in a dictionary are organized into suites. For example, the Finder Dictionary includes the required suite, the standard suite, and the Finder suite. Using different scripting systems You can use the Script Editor to write scripts for scripting systems other than AppleScript. A scripting system is software that lets you write scripts using a set of terms put together according to rules of syntax. AppleScript has one set of terms and syntactic rules. Other scripting systems offer different terms and rules. To use a different scripting system, you must first install the system. To find out how to install a specific scripting system, see the documentation that came with it. To use the Script Editor with an installed scripting system, choose the scripting system from the pop-up menu at the bottom of the Script Editor window. For more information about a scripting system you have on your Macintosh, see the information that came with the scripting system. Using different AppleScript dialects The examples in this guide are based on the English dialect of AppleScript. If you have the proper software installed on your Macintosh computer, you can use a different AppleScript dialect in the Script Editor. An AppleScript dialect is a version of AppleScript based on the words and syntax of a particular language (a human language or a computer language). The English dialect is based on the English language. AppleScript can resemble any language if there is an AppleScript dialect for that language. To choose a different AppleScript dialect, follow these steps:
2. In the dialog box that appears, choose a dialect from the pop-up menu if more than one exists. Displaying the Result window When a script runs, some of its expressions can produce an outcome or a result. For example, the expression "2+2" produces the result "4." When a script produces a result, it appears in the Result window. Some error messages also appear in the Result window. You can open the Result window at any time when you use the Script Editor. To open the Result window choose "Show Result" from the Controls menu. The Result window opens. If the open script ran and produced a result, the result appears in the window. Pasting an object reference into a script You can use the Paste Reference command (in the Edit menu) to paste an object reference into your script. An object reference is an AppleScript phrase that identifies an object in an application program. For example, the phrase "word 3 of document 1" identifies an object in the Scriptable Text Editor. To paste an object reference into your script, follow these steps: The reference phrase is pasted into your script at the location of the insertion point. For example, you can paste the reference phrase for your startup disk (a Finder object.) You can paste references only from applications that support this particular feature. An application can be scriptable and recordable without allowing pasting of object references. Section 2 Script Editor Command Reference
File menu
terms you can use with an application program you select.
|
Document Information | |
Product Area: | Apple Software |
Category: | AppleScript |
Sub Category: | General Topics |
Copyright © 2000 Apple Computer, Inc. All rights reserved.