No part of this publication or the
software described in it may be
reproduced, stored in a retrieval
system, or transmitted, in any form
or by any means, mechanical,
electronic, photocopying, recording,
or otherwise, without prior written
permission of Apple Computer, Inc.
Printed in the United States
of America.
The Apple logo is a trademark of
Apple Computer, Inc. Use of the
“keyboard” Apple logo (OptionShift-K) for commercial purposes
without the prior written consent of
Apple may constitute trademark
infringement and unfair competition
in violation of federal and state laws.
No licenses, express or implied, are
granted with respect to any of the
technology described in this book.
Apple retains all intellectual
property rights associated with the
technology described in this book.
This book is intended to assist
application developers to develop
applications only for Apple
Macintosh computers.
Apple Computer, Inc.
20525 Mariani Avenue
Cupertino, CA 95014
408-996-1010
Apple, the Apple logo, AppleTalk,
HyperCard, HyperTalk, LaserWriter,
and Macintosh are trademarks of
Apple Computer, Inc., registered
in the United States and other
countries.
AppleScript, Finder, Geneva
and System 7 are trademarks of
Apple Computer, Inc.
Adobe Illustrator and PostScript are
trademarks of Adobe Systems
Incorporated, which may be
registered in certain jurisdictions.
FrameMaker is a registered
trademark of Frame Technology
Corporation.
Helvetica and Palatino are registered
trademarks of Linotype Company.
FileMaker is a registered trademark
of Claris Corporation.
ITC Zapf Dingbats is a registered
trademark of International Typeface
Corporation.
Microsoft is a registered trademark
of Microsoft Corporation.
Simultaneously published in the
United States and Canada.
LIMITED WARRANTY ON MEDIA
AND REPLACEMENT
If you discover physical defects in the
manuals distributed with an Apple
product, Apple will replace the manuals
at no charge to you, provided you return
the item to be replaced with proof of
purchase to Apple or an authorized
Apple dealer during the 90-day period
after you purchased the software. In
addition, Apple will replace damaged
manuals for as long as the software is
included in Apple’s Media Exchange
program. See your authorized Apple
dealer for program coverage and details.
In some countries the replacement
period may be different; check with
your authorized Apple dealer.
ALL IMPLIED WARRANTIES ON
THIS MANUAL, INCLUDING
IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A
PART ICULAR PURPOSE, ARE
LIMITED IN DURATION TO NINETY
(90) DAYS FROM THE DATE OF THE
ORIGINAL RETAIL PURCHASE OF
THIS PRODUCT.
Even though Apple has reviewed this
manual, APPLE MAKES NO
WARRANTY OR REPRESENTATION,
EITHER EXPRESS OR IMPLIED, WITH
RESPECT TO THIS MANUAL, ITS
QUALITY, ACCURACY, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS
MANUAL IS SOLD “AS IS,” AND
YOU, THE PURCHASER, ARE
ASSUMING THE ENTIRE RISK AS TO
ITS QUALITY AND ACCURACY.
IN NO EVENT WILL APPLE BE
LIABLE FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES
RESULTING FROM ANY DEFECT OR
INACCURACY IN THIS MANUAL,
even if advised of the possibility of such
damages.
THE WARRANTY AND REMEDIES
SET FORTH ABOVE ARE EXCLUSIVE
AND IN LIEU OF ALL OTHERS, ORAL
OR WRITTEN, EXPRESS OR IMPLIED.
No Apple dealer, agent, or employee is
authorized to make any modification,
extension, or addition to this warranty.
Some states do not allow the exclusion
or limitation of implied warranties or
liability for incidental or consequential
damages, so the above limitation or
exclusion may not apply to you. This
warranty gives you specific legal rights,
and you may also have other rights
which vary from state to state.
Contents
Figures and Tablesxiii
PrefaceAbout This Guide
Audiencexv
Organization of This Guidexvi
Sample Applications and Scriptsxvii
For More Informationxviii
Getting Startedxviii
Scripting Additionsxviii
Other AppleScript Dialectsxviii
Scriptable Applicationsxviii
Conventions Used in This Guide xix
Part 1Introducing AppleScript
xv
1
Chapter 1AppleScript, Scripts, and Scriptable Applications
What Is AppleScript?3
What Can You Do With Scripts?5
Automating Activities5
Integrating Applications 7
Customizing Applications 7
Who Runs Scripts, and Who Writes Them?9
Special Features of AppleScript10
What Applications Are Scriptable?11
3
iii
Chapter 2Overview of AppleScript
How Does AppleScript Work?14
Statements14
Commands and Objects 17
Dictionaries18
Values20
Expressions 21
Operations21
Variables22
Script Objects23
Scripting Additions23
Dialects24
Other Features and Language Elements24
If (Simple Statement) 192
If (Compound Statement)193
Repeat Statements 194
Repeat (forever)197
Repeat (number) Times198
Repeat While199
Repeat Until200
Repeat With (loopVariable) From (startValue) To (stopValue)201
Repeat With (loopVariable) In (list)202
Exit204
Try Statements 204
Kinds of Errors205
How Errors Are Handled206
Writing a Try Statement206
Try207
Signaling Errors in Scripts210
Error210
Considering and Ignoring Statements 213
Considering/Ignoring214
With Timeout Statements 217
With Timeout218
With Transaction Statements219
With Transaction219
183
Chapter 8Handlers
Using Subroutines 221
Types of Subroutines223
Scope of Subroutine Calls in Tell Statements224
Checking the Classes of Subroutine Parameters 225
viii
221
Recursive Subroutines225
Saving and Loading Libraries of Subroutines226
Subroutine Definitions and Calls228
Subroutines With Labeled Parameters229
Subroutine Definition, Labeled Parameters229
Subroutine Call, Labeled Parameters230
Examples of Subroutines With Labeled Parameters232
Subroutines With Positional Parameters235
Subroutine Definition, Positional Parameters235
Subroutine Call, Positional Parameters236
Examples of Subroutines With Positional Parameters238
The Return Statement239
Return 240
Command Handlers 241
Command Handler Definition241
Command Handlers for Script Applications243
Run Handlers243
Open Handlers246
Handlers for Stay-Open Script Applications247
Idle Handlers248
Quit Handlers249
Interrupting a Script Application’s Handlers250
Calling a Script Application251
Scope of Script Variables and Properties 252
Scope of Properties and Variables Declared at the Top Level
of a Script254
Scope of Properties and Variables Declared in a Script Object258
Scope of Variables Declared in a Handler263
Chapter 9Script Objects
About Script Objects265
Defining Script Objects267
Sending Commands to Script Objects268
Initializing Script Objects 269
Inheritance and Delegation 271
265
ix
Defining Inheritance271
How Inheritance Works272
The Continue Statement277
Using Continue Statements to Pass Commands to Applications280
The Parent Property and the Current Application281
Using the Copy and Set Commands With Script Objects283
Appendix A The Language at a Glance
Commands289
References294
Operators296
Control Statements299
Handlers301
Script Objects303
Variable and Property Assignments and Declarations303
Predefined Variables304
Constants305
Placeholders307
289
Appendix B Scriptable Text Editor Dictionary
About Text Objects 313
Elements of Text Objects 314
Special Properties of Scriptable Text Editor Text Objects314
Text Styles 315
AppleScript and Non-Roman Script Systems317
Scriptable Text Editor Object Class Definitions318
Application 318
Character 321
Document/Window 323
File 328
Insertion Point 329
Paragraph 331
Selection 334
Text336
313
x
Text Item 339
Text Style Info 341
Window 342
Word 342
Scriptable Text Editor Commands 345
Copy347
Cut 348
Data Size349
Duplicate349
Make 350
Move 351
Open 351
Paste 351
Revert 352
Save 353
Select 354
Scriptable Text Editor Errors355
Appendix C Error Messages
Operating System Errors 358
Apple Event Errors 359
Apple Event Registry Errors 361
AppleScript Errors 362
Glossary
Index
363
371
357
xi
Figures and Tables
Chapter 1
Chapter 2
Chapter 3
Chapter 4
AppleScript, Scripts, and Scriptable Applications
Figure 1-1Changing text style with the mouse and with a script4
Figure 1-2A script that performs a repetitive action6
Figure 1-3A script that copies information from one application to another8
Figure 1-4Different ways to run a script9
Overview of AppleScript
Figure 2-1How AppleScript works15
Figure 2-2How AppleScript gets the Scriptable Text Editor dictionary20
13
3
Values33
Figure 3-1Value class definition for lists34
Figure 3-2Coercions supported by AppleScript69
Table 3-1AppleScript value class identifiers 39
Commands
Figure 4-1Command definition for the Move command77
Figure 4-2The Scriptable Text Editor document “simple”95
71
Chapter 5
Table 4-1Standard application commands defined in this chapter 85
Table 4-2AppleScript commands defined in this chapter86
Objects and References
Figure 5-1The Scriptable Text Editor’s object class definition for
paragraph objects121
Figure 5-2The Scriptable Text Editor document “simple”137
Table 5-1Reference forms126
Table 5-2Boolean expressions and tests in Filter references142
Figure 8-1Scope of property and variable declarations at the top level
Figure 8-2Scope of property and variable declarations at the top level
Figure 8-3Scope of variable declarations within a handler263
Script Objects
Figure 9-1Relationship between a simple child script and its parent273
Figure 9-2Another child-parent relationship273
Figure 9-3A more complicated child-parent relationship274
The Language at a Glance
Table A-1Command syntax290
Table A-2Reference form syntax294
Table A-3Container notation in references296
Table A-4Operators297
Table A-5Control statements300
Table A-6Handler definitions and calls302
Table A-7Script objects303
Table A-8Assignments and declarations304
Table A-9Predefined variables 305
Table A-10Constants defined by AppleScript305
Table A-11Placeholders used in syntax descriptions308
221
of a script254
of a script object258
265
289
Appendix B
xiv
Scriptable Text Editor Dictionary
Figure B-1Bounds and Position properties of a Scriptable Text Editor
window327
Table B-1Variations from standard behavior in Scriptable Text Editor versions
of standard application commands345
Table B-2Other Scriptable Text Editor commands347
313
P R E F A C E
About This Guide
The AppleScript Language Guide: English Dialect is a complete guide to the
English dialect of the AppleScript language. AppleScript allows you to create
sets of written instructions—known as scripts—to automate and customize
your applications.
Audience0
This guide is for anyone who wants to write new scripts or modify
existing scripts.
Before using this guide, you should read Getting Started With AppleScript to
learn what hardware and software you need to use AppleScript; how to install
AppleScript; and how to run, record, and edit scripts.
To make best use of this guide, you should already be familiar with at least one
of the following:
■
another scripting language (such as HyperTalk, the scripting language for
HyperCard, or a scripting language for a specific application)
■
a computer programming language (such as BASIC, Pascal, or C)
■
a macro language (such as a language used to manipulate spreadsheets)
If you’re not already familiar with the basics of scripting and programming
(such as variables, subroutines, and conditional statements such as If-Then),
you may want additional information to help you get started. You can find a
variety of introductory books on scripting and programming—including books
specifically about AppleScript—in many bookstores.
Macintosh software developers who want to create scriptable and recordable
applications should refer to Inside Macintosh: Interapplication Communication.
xv
P R E F A C E
Organization of This Guide0
This guide is divided into two parts:
■
Part 1, “Introducing AppleScript,” provides an overview of the AppleScript
language and the tasks you can perform with it.
■ Part 2, “AppleScript Language Reference,” provides reference descriptions
of all of the features of the AppleScript language.
Part 1 contains these chapters:
■ Chapter 1, “AppleScript, Scripts, and Scriptable Applications,” introduces
AppleScript and its capabilities.
■ Chapter 2, “Overview of AppleScript,” provides an overview of the
elements of the AppleScript language.
Part 2 contains the following chapters:
xvi
■ Chapter 3, “Values,” describes the classes of data that can be stored and
manipulated in scripts and the coercions you can use to change a value
from one class to another.
■ Chapter 4, “Commands,” describes the types of commands available in
AppleScript, including application commands, AppleScript commands,
scripting addition commands, and user-defined commands. It also includes
descriptions of all AppleScript commands and standard application
commands.
■ Chapter 5, “Objects and References,” describes objects and their
characteristics and explains how to refer to objects in scripts.
■ Chapter 6, “Expressions,” describes types of expressions in AppleScript,
how AppleScript evaluates expressions, and operators you use to
manipulate values.
P R E F A C E
■ Chapter 7, “Control Statements,” describes statements that control when and
how other statements are executed. It includes information about Tell, If,
and Repeat statements.
handlers, and the scope of variables and properties in handlers and
elsewhere in a script. It includes the syntax for defining and calling
subroutines and error handlers.
■ Chapter 9, “Script Objects,” describes how to define and use script objects. It
includes information about object-oriented programming techniques such as
using inheritance and delegation to define groups of related objects.
At the end of the guide are three appendixes, a glossary of AppleScript terms,
and an index.
■ Appendix A, “The Language at a Glance,” is a collection of tables that
summarize the features of the AppleScript language. It is especially useful
for experienced programmers who want a quick overview of the language.
■ Appendix B, “Scriptable Text Editor Dictionary,” defines the words in the
AppleScript language that are understood by the Scriptable Text Editor
sample application.
■ Appendix C, “Error Messages,” lists the error messages returned
by AppleScript.
Sample Applications and Scripts0
A sample application, the Scriptable Text Editor, is included with AppleScript.
The Scriptable Text Editor is scriptable; that is, it understands scripts written in
the AppleScript language. It also supports recording of scripts: when you use
the Record button in the Script Editor (the application you use to write and
modify scripts), the actions you perform in the Scriptable Text Editor generate
AppleScript statements for performing those actions. Scripts for performing
tasks in the Scriptable Text Editor are used as examples throughout this guide.
xvii
P R E F A C E
For More Information0
Getting Started0
See the companion book Getting Started With AppleScript to learn what
hardware and software you need to use AppleScript; how to install
AppleScript; and how to run, record, and edit scripts.
Scripting Additions0
Scripting additions are files that provide additional commands you can use in
scripts. A standard set of scripting additions comes with AppleScript. Scripting
additions are also sold commercially, included with applications, and
distributed through electronic bulletin boards and user groups.
For information about using the scripting additions that come with AppleScript,
see the companion book AppleScript Scripting Additions Guide: English Dialect.
xviii
Other AppleScript Dialects0
A dialect is a version of the AppleScript language that resembles a particular
language. This guide describes the English dialect of AppleScript (also
called AppleScript English). This dialect uses words taken from the English
language and has an English-like syntax. Other dialects can use words from
other human languages, such as Japanese, and have a syntax that resembles
a specific human language or programming language.
For information about a specific dialect, see the version of the AppleScriptLanguage Guide for that dialect.
Scriptable Applications0
Not all applications are scriptable. The advertising and packaging for an
application usually mention if it is scriptable. The documentation for a
scriptable application typically lists the AppleScript words that the application
understands.
P R E F A C E
Conventions Used in This Guide 0
Words and sample scripts in monospaced font are AppleScript language
elements that must be typed exactly as shown. Terms are shown in boldface
where they are defined. You can also find these definitions in the glossary.
Here are some additional conventions used in syntax descriptions:
language element
Plain computer font indicates an element that you
must type exactly as shown. If there are special symbols
(for example, + or &), you must also type them exactly
as shown.
placeholderItalic text indicates a placeholder that you must replace
with an appropriate value. (In some programming
languages, placeholders are called nonterminals.)
[optional]Brackets indicate that the enclosed language element or
elements are optional.
(a group)Parentheses group together elements. If parentheses are
part of the syntax, they are shown in bold.
[optional]...Three ellipsis points (. . .) after a group defined by
brackets indicate that you can repeat the group of
elements within brackets 0 or more times.
(a group). . .Three ellipsis points (. . .) after a group defined by
parentheses indicate that you can repeat the group
of elements within parentheses one or more times.
a|b|cVertical bars separate elements in a group from which
you must choose a single element. The elements are
often grouped within parentheses or brackets.
xix
P A R T ONE
Introducing AppleScript1
CHAPTER 1
Figure 1-0
Listing 1-0
Table 1-0
AppleScript, Scripts, and
Scriptable Applications1
This chapter introduces the AppleScript scripting language. It answers
these questions:
■ What is AppleScript?
■ What are scripts?
■ Who runs scripts, and who writes them?
■ How is AppleScript different from other scripting mechanisms?
■ What can you do with scripts?
■ What applications are scriptable?
What Is AppleScript?1
AppleScript is a scripting language that allows you to control Macintosh
computers without using the keyboard or mouse. AppleScript lets you use
series of written instructions, known as scripts, to control applications and the
desktop. Figure 1-1 shows the difference between changing the text style of a
paragraph with the mouse and performing the same task with a script.
What Is AppleScript?3
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Figure 1-1Changing text style with the mouse and with a script
Changing the style of text with the mouse
Changing the style of text with a script
4What Is AppleScript?
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
The script shown at the bottom of Figure 1-1 is written in AppleScript English,
which is a dialect of the AppleScript scripting language that resembles English.
This guide describes AppleScript English and how you can use it to write
scripts. Other dialects, such as AppleScript Japanese and AppleScript French,
are designed to resemble other human languages. Still others, such as the
Programmer’s Dialect, resemble other programming languages. For information about dialects other than AppleScript English, see the guide for the dialect
you want to use. For information about installing dialects, see Getting Started
With AppleScript.
All AppleScript dialects share many features with other scripting, programming,
and macro languages. If you’ve used any of these languages, you’ll find
AppleScript dialects very easy to learn and use.
AppleScript comes with an application called Script Editor that you can use to
create and modify scripts. You can also use Script Editor to translate scripts
from one AppleScript dialect to another.
What Can You Do With Scripts?1
AppleScript lets you automate, integrate, and customize applications. The
following sections provide examples.
Automating Activities1
Scripts make it easy to perform repetitive tasks. For example, if you want
to change the style of the word “AppleScript” to bold throughout a document
named Introduction, you can write a script that does the job instead of
searching for each occurrence of the word, selecting it, and changing it from
the Style menu.
Figure 1-2 shows the script and what happens when you run it.
What Can You Do With Scripts?5
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Figure 1-2A script that performs a repetitive action
Introduction before running script
Make AppleScript Bold script
Introduction after running script
6What Can You Do With Scripts?
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Integrating Applications 1
Scripts are ideal for performing tasks that involve more than one application.
A script can send instructions to one application, get the resulting data, and
then pass the data on to one or more additional applications. For example, a
script can collect information from a database application and copy it to a
spreadsheet application. Figure 1-3 shows a simple script that gets a value
from the Count cell of an inventory database and copies it to the Inventory
column of a spreadsheet.
In the same way, a script can use one application to perform an action on data
from another application. For example, suppose a word-processing application
includes a spelling checker and also supports an AppleScript command to
check spelling. You can check the spelling of a block of text from any other
application by writing a script that sends the AppleScript command and the
text to be checked to the word-processing application, which returns the results
to the application that runs the script.
If an action performed by an application can be controlled by a script, that
action can be also performed from the Script Editor or from any other
application that can run scripts. Every scriptable application is potentially a
toolkit of useful utilities that can be selectively combined with utilities from
other scriptable applications to perform highly specialized tasks.
Customizing Applications 1
Scripts can add new features to applications. To customize an application, you
add a script that is triggered by a particular action within the application, such
as choosing a menu item or clicking a button. Whether you can add scripts to
applications is up to each application, as are the ways you associate scripts
with specific actions.
What Can You Do With Scripts?7
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Figure 1-3A script that copies information from one application to another
8What Can You Do With Scripts?
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Who Runs Scripts, and Who Writes Them?1
To run a script is to cause the actions the script describes to be performed.
Everyone who uses a Macintosh computer can run scripts. Figure 1-4 illustrates
two ways to run a script.
Figure 1-4Different ways to run a script
Double-clicking a script application’s icon
Clicking the Run button
If the script is a script application on the desktop, you can run it by doubleclicking its icon. You can also run any script by clicking the Run button in the
Script Editor window for that script.
Who Runs Scripts, and Who Writes Them?9
CHAPTER 1
AppleScript, Scripts, and Scriptable Applications
Although everyone can run scripts, not everyone needs to write them. One
person who is familiar with a scripting language can create sophisticated
scripts that many people can use. For example, management information
specialists in a business can write scripts for everyone in the business to use.
Scripts are also sold commercially, included with applications, and distributed
through electronic bulletin boards and user groups.
Special Features of AppleScript1
AppleScript has a number of features that set it apart from both macro
programs and scripting languages that control a single program:
■ AppleScript makes it easy to refer to data within applications. Scripts can
use familiar names to refer to familiar objects. For example, a script can refer
to paragraph, word, and character objects in a word-processing document
and to row, column, and cell objects in a spreadsheet.
■ You can control several applications from a single script. Although many
applications include built-in scripting or macro languages, most of these
languages work for only one application. In contrast, you can use AppleScript
to control any of the applications that support it. You don’t have to learn a
new language for each application.
■ You can write scripts that control applications on more than one computer. A
single script can control any number of applications, and the applications
can be on any computer on a given network.
■ You can create scripts by recording. The Script Editor application includes a
recording mechanism that takes much of the work out of creating scripts.
When recording is turned on, you can perform actions in a recordable
application and the Script Editor creates corresponding instructions in the
AppleScript language. To learn how to turn recording on and off, refer to
Getting Started With AppleScript.
■ AppleScript supports multiple dialects, or representations of the AppleScript
language that resemble various human languages and programming
languages. This guide describes the AppleScript English dialect. You can use
Script Editor to convert a script from one dialect to another without
changing what happens when you run the script.
10Special Features of AppleScript
Loading...
+ 376 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.