Trang 1
The most complete book available covering AppleScript 2 and Mac OS X automation Learn AppleScript The Comprehensive Guide to Scripting and Automation on Mac OS X THIRDEDITION HamishSanderson | HanaanRosenthal ForewordbyDr.WilliamCook www.allitebooks.com
Trang 3
Learn AppleScript The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition ■ ■ ■ Hamish Sanderson and Hanaan Rosenthal Download from Wow! eBook <www.wowebook.com> i www.allitebooks.com
Trang 4
Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition Copyright © 2010 by Hamish Sanderson and Hanaan Rosenthal All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2361-0 ISBN-13 (electronic): 978-1-4302-2362-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher and President: Paul Manning Lead Editor: Douglas Pundick Technical Reviewer: David A. Coyle Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow- Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editor: Bill McManus Compositor: MacPS, LLC. Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. ii Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 5
Contents at a Glance ■Contents at a Glance................................................................................................................ iii ■Contents.....................................................................................................................................v ■Foreword . ...............................................................................................................................xix ■About the Authors. ..................................................................................................................xxi ■About the Technical Reviewer ...............................................................................................xxii ■Acknowledgments . ...............................................................................................................xxiii ■Introduction . .........................................................................................................................xxiv Part I: Welcome to AppleScript.................................................................................................... 1 ■Chapter 1: Introducing AppleScript .......................................................................................... 3 ■Chapter 2: AppleScript in Principle ........................................................................................ 15 ■Chapter 3: AppleScript in Practice ......................................................................................... 33 Part II: Understanding AppleScript ............................................................................................ 51 ■Chapter 4: Writing Scripts in AppleScript Editor .................................................................... 53 ■Chapter 5: Understanding How Application Scripting Works ................................................. 77 ■Chapter 6: Learning to Work with AppleScript Objects ........................................................ 143 ■Chapter 7: Working with Text ............................................................................................... 163 ■Chapter 8: Working with Numbers ....................................................................................... 215 ■Chapter 9: Working with Dates............................................................................................. 241 ■Chapter 10: Working with Lists and Records ....................................................................... 267 ■Chapter 11: Storing Objects in Variables.............................................................................. 293 ■Chapter 12: More on Commands .......................................................................................... 319 ■Chapter 13: More on Operators and Coercions..................................................................... 369 ■Chapter 14: Making Decisions Using Conditionals and Loops.............................................. 389 ■Chapter 15: Making Decisions When Dealing with Errors .................................................... 413 ■Chapter 16: Interacting with the User .................................................................................. 437 ■Chapter 17: Working with Files ............................................................................................ 471 iii Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 6
■ CONTENTS AT A GLANCE ■Chapter 18: Organizing Your Code with Handlers ................................................................ 505 ■Chapter 19: Organizing Your Code with Script Objects ........................................................ 541 Part III: Putting AppleScript to Work ....................................................................................... 585 ■Chapter 20: Scripting the File System .................................................................................. 587 ■Chapter 21: Scripting Apple Applications............................................................................. 623 ■Chapter 22: Extending AppleScript with Scripting Additions ............................................... 675 ■Chapter 23: AppleScript Amenities....................................................................................... 711 ■Chapter 24: Scripting iWork and Office ................................................................................ 743 ■Chapter 25: Scripting Data and Databases........................................................................... 793 ■Chapter 26: Scripting Adobe InDesign.................................................................................. 817 ■Chapter 27: Interacting with the Unix Command Line .......................................................... 863 ■Chapter 28: Using Smile: The AppleScript Integrated Production Environment ................................................................................................. 897 ■Chapter 29: Tips and Techniques for Improving Your Scripts .............................................. 927 ■Chapter 30: Creating Cocoa Applications with AppleScriptObjC .......................................... 969 ■Index................................................................................................................................... 1019 iv Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 7
Contents ■Contents at a Glance................................................................................................................ iii ■Contents.....................................................................................................................................v ■Foreword . ...............................................................................................................................xix ■About the Authors. ..................................................................................................................xxi ■About the Technical Reviewer ...............................................................................................xxii ■Acknowledgments . ...............................................................................................................xxiii ■Introduction . .........................................................................................................................xxiv Part I: Welcome to AppleScript.................................................................................................... 1 ■Chapter 1: Introducing AppleScript .......................................................................................... 3 Automating with AppleScript: From Small Tasks to Large . ...................................................................................3 Automating Small Tasks ....................................................................................................................................3 Automating Large Tasks ....................................................................................................................................4 When to Automate (or Not)?. .............................................................................................................................5 The Golden Triangle: Scriptable Applications, the AppleScript Language, and the Scripts You Write . .................6 What Is a Scriptable Application? ......................................................................................................................6 What Is the AppleScript Language?. .................................................................................................................8 What Is a Script?................................................................................................................................................9 Your First AppleScript: Hello World!......................................................................................................................10 Summary . ............................................................................................................................................................13 ■Chapter 2: AppleScript in Principle ........................................................................................ 15 The Four Key Concepts of Application Scripting. .................................................................................................16 How Applications Represent Information As Objects. .....................................................................................16 How Applications Manipulate Objects Using Commands . ..............................................................................18 How Applications Organize Their Objects into Object Models . .......................................................................19 How Applications Describe Their Objects and Commands in Dictionaries . ....................................................21 The Four Key Concepts of the AppleScript Language. .........................................................................................22 How AppleScript Represents Information As Objects . ....................................................................................23 How AppleScript Manipulates Objects Using Commands, Operators, and Variables ......................................24 How AppleScript Organizes Code Using Handlers and Script Objects. ...........................................................26 v Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 8
■ CONTENTS Tying It All Together: What Makes AppleScript Special . ......................................................................................28 The English-like Syntax . .................................................................................................................................28 Built-in Application Scripting Support . ...........................................................................................................29 Attaching Scripts to Applications.....................................................................................................................29 The AppleScript Community . ..........................................................................................................................30 Summary . ............................................................................................................................................................31 ■Chapter 3: AppleScript in Practice ......................................................................................... 33 The Project: Tidy Desktop .....................................................................................................................................33 Planning the Script . .............................................................................................................................................34 Writing the Script. ................................................................................................................................................35 Creating the Folder Name . ..............................................................................................................................35 Interacting with the File System ......................................................................................................................39 Final Thoughts . ....................................................................................................................................................46 Improving Reliability ........................................................................................................................................46 Adding Features...............................................................................................................................................47 Documenting Your Code . ................................................................................................................................48 Summary . ............................................................................................................................................................49 Part II: Understanding AppleScript ............................................................................................ 51 ■Chapter 4: Writing Scripts in AppleScript Editor .................................................................... 53 Working with Script Windows. .............................................................................................................................53 Compiling a Script............................................................................................................................................54 Running and Stopping a Script . ......................................................................................................................55 Recording a Script ...........................................................................................................................................55 Viewing the Result of Running a Script . .........................................................................................................56 Viewing the Events Sent by a Running Script. ................................................................................................56 Adding a Description to a Script ......................................................................................................................57 Viewing Application Dictionaries ..........................................................................................................................57 Other Useful AppleScript Editor Features . ...........................................................................................................58 Using the Library Window. ..............................................................................................................................59 Navigating to a Handler in a Script ..................................................................................................................60 Viewing the Event Log History . .......................................................................................................................61 Scripting the AppleScript Editor.......................................................................................................................61 Using Context Menu Scripts. ...........................................................................................................................62 Viewing the Bundle Contents Drawer . ............................................................................................................62 Setting the Script Formatting Preferences . ....................................................................................................63 Saving Scripts. .....................................................................................................................................................65 Text-Based Scripts...........................................................................................................................................66 Compiled Scripts..............................................................................................................................................66 Script-Based Applications (Applets) . ..............................................................................................................69 Other Tools for Writing and Running AppleScripts . .............................................................................................72 The System-wide Script Menu. .......................................................................................................................72 Third-Party AppleScript Editors . .....................................................................................................................74 Developing GUI Applications . ..........................................................................................................................76 Command-Line Tools . .....................................................................................................................................76 Summary . ............................................................................................................................................................76 ■Chapter 5: Understanding How Application Scripting Works ................................................. 77 A Quick Application Scripting Example.................................................................................................................78 Understanding Objects..........................................................................................................................................79 How Objects Are Categorized by Class . ..........................................................................................................79 Introducing Object Properties . ........................................................................................................................81 vi Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 9
■ CONTENTS Introducing Object Elements. ..........................................................................................................................84 Understanding Commands. ..................................................................................................................................86 Introducing Command Names . .......................................................................................................................87 Introducing Command Parameters . ................................................................................................................87 Commands Must Be Targeted at Objects.........................................................................................................89 Commands Can Return Results or Report Errors. ...........................................................................................90 Understanding the Application Object Model. ......................................................................................................91 How Objects Can Contain Other Objects. ........................................................................................................92 How AppleScript Refers to Application Objects . .............................................................................................93 Exploring a Typical Object Model. ...................................................................................................................98 Understanding Application Dictionaries. ............................................................................................................103 Introducing AppleScript Editor’s Dictionary Viewer . .....................................................................................104 How Classes Are Documented. .....................................................................................................................107 How Commands Are Documented . ...............................................................................................................118 Application Dictionaries Don’t Tell You Everything! . .....................................................................................120 More on application Objects . .............................................................................................................................123 Creating application Objects. ........................................................................................................................123 The Standard Properties of application Objects . ..........................................................................................126 How AppleScript Compiles tell application ... Blocks . ..................................................................................127 More on Constructing References . ....................................................................................................................128 Referring to Properties...................................................................................................................................129 Referring to Elements ....................................................................................................................................129 Referring to Insertion Locations.....................................................................................................................138 Summary . ..........................................................................................................................................................139 ■Chapter 6: Learning to Work with AppleScript Objects ........................................................ 143 How AppleScript Represents Information As Objects . .......................................................................................144 What Kinds of Objects Does AppleScript Provide? . ......................................................................................144 Creating New Objects ....................................................................................................................................145 Getting Information from Objects. .................................................................................................................145 How AppleScript Works with Objects . ...............................................................................................................146 Manipulating Objects with Commands . ........................................................................................................146 Manipulating Objects with Operators . ..........................................................................................................148 Coercing Objects............................................................................................................................................153 Storing Objects in Variables. .........................................................................................................................154 Working with Boolean Objects. ..........................................................................................................................157 Boolean Operators .........................................................................................................................................158 Boolean-Related Coercions. ..........................................................................................................................160 Summary . ..........................................................................................................................................................161 ■Chapter 7: Working with Text ............................................................................................... 163 Introducing Text. ................................................................................................................................................163 A Brief History of Text ....................................................................................................................................164 Introducing Text in AppleScript 2.0. ..............................................................................................................169 How to Write Literal Strings in AppleScript . ......................................................................................................170 The Properties of Text.........................................................................................................................................174 The class Property . .....................................................................................................................................174 The length Property . ...................................................................................................................................174 The quoted form Property. .........................................................................................................................174 The id Property . ...........................................................................................................................................174 The Elements of Text ..........................................................................................................................................175 Characters . ...................................................................................................................................................175 vii Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 10
■ CONTENTS Words. ...........................................................................................................................................................178 Paragraphs. ...................................................................................................................................................179 Text Ranges ...................................................................................................................................................179 Operators and Commands . ................................................................................................................................181 Joining Strings...............................................................................................................................................181 Comparing Strings .........................................................................................................................................182 Checking for Strings Within Strings. .............................................................................................................184 Considering and Ignoring Attributes . ............................................................................................................185 The count Command. ....................................................................................................................................189 The offset Command. ....................................................................................................................................189 Coercing to and from Text . ...........................................................................................................................190 Working with Text Item Delimiters......................................................................................................................192 Splitting Strings with Text Item Delimiters . ..................................................................................................192 Combining List Items into a Single String. ....................................................................................................194 Finding and Replacing Text . .........................................................................................................................196 How AppleScript Text Works in Tiger . ...............................................................................................................197 The Many Different Classes of Text . .............................................................................................................198 Working with the Various Text Classes. ........................................................................................................198 The ASCII number and ASCII character Commands. .....................................................................................200 Example Projects . ..............................................................................................................................................201 Defining a Find-and-Replace Command. ......................................................................................................202 Transferring Meeting Arrangements from Mail to iCal . ................................................................................204 Summary . ..........................................................................................................................................................213 ■Chapter 8: Working with Numbers ....................................................................................... 215 Introducing Integers and Reals . .........................................................................................................................215 Operators and Commands . ................................................................................................................................216 Math Operations ............................................................................................................................................217 Comparison Operations . ...............................................................................................................................221 Introducing Operator Precedence . ................................................................................................................224 The round Command......................................................................................................................................225 The random number Command .....................................................................................................................227 Coercing to and from Numbers. ....................................................................................................................229 Example Projects . ..............................................................................................................................................231 Defining a Round-to-Decimal-Places Command . .........................................................................................231 Blackjack! . ....................................................................................................................................................233 Kitchen Timer.................................................................................................................................................237 Summary . ..........................................................................................................................................................240 ■Chapter 9: Working with Dates............................................................................................. 241 Introducing Dates. ..............................................................................................................................................241 Understanding Date and Time Formats . .......................................................................................................242 Forming Dates On-the-Fly. ............................................................................................................................244 Specifying a Time Relative to a Date . ...........................................................................................................246 The Properties of Dates ......................................................................................................................................247 The class Property . .....................................................................................................................................247 The year Property . .......................................................................................................................................247 The month Property . .....................................................................................................................................247 The day Property . .........................................................................................................................................248 The weekday Property. .................................................................................................................................248 The hours Property . .....................................................................................................................................249 The minutes Property. .................................................................................................................................249 viii Download from Wow! eBook <www.wowebook.com>www.allitebooks.com
Trang 11
■ CONTENTS The seconds Property. .................................................................................................................................249 The time Property . .......................................................................................................................................249 The date string Property. .........................................................................................................................250 The short date string Property . ............................................................................................................250 The time string Property. .........................................................................................................................250 Setting Date Properties..................................................................................................................................250 Operators and Commands . ................................................................................................................................252 Comparison Operators . .................................................................................................................................252 Math Operators ..............................................................................................................................................254 The current date Command......................................................................................................................257 The time to GMT Command. ......................................................................................................................258 Example Projects . ..............................................................................................................................................258 Deleting Old Files...........................................................................................................................................259 Alarm Clock. ..................................................................................................................................................263 Summary . ..........................................................................................................................................................266 ■Chapter 10: Working with Lists and Records ....................................................................... 267 Introducing Lists and Records . ..........................................................................................................................267 What Is a List? ...............................................................................................................................................267 What Is a Record?..........................................................................................................................................268 How Are Lists and Records Used in Scripts?. ...............................................................................................269 Working with Lists . ............................................................................................................................................269 The Properties of a List..................................................................................................................................270 The Elements of a List ...................................................................................................................................271 Operators and Commands . ...........................................................................................................................274 Processing List Items with a Repeat Loop. ...................................................................................................279 Sorting Items in a List....................................................................................................................................281 Working with Records.........................................................................................................................................282 Getting and Setting the Properties of a Record . ...........................................................................................284 Operators and Commands . ...........................................................................................................................286 Example Project: Measuring Word Frequency in TextEdit . ................................................................................287 Planning the Script ........................................................................................................................................287 Developing the Script.....................................................................................................................................288 Conclusion . ...................................................................................................................................................291 Summary . ..........................................................................................................................................................292 ■Chapter 11: Storing Objects in Variables.............................................................................. 293 Creating Variables. .............................................................................................................................................293 Declaring Your Variables................................................................................................................................294 Choosing Your Variable Names. ....................................................................................................................295 Working with Local Variables . ...........................................................................................................................299 Understanding Local Variable Scope . ...........................................................................................................300 Passing Values to and from User-Defined Handlers . ....................................................................................303 Working with Properties and Global Variables. ..................................................................................................305 Understanding Property and Global Variable Scope . ....................................................................................305 How Properties (and Global Variables) Can Retain Values Between Runs. ...................................................306 When to Use Properties and Global Variables. ..............................................................................................307 Using AppleScript’s Built-in Variables . ..............................................................................................................310 Variables Containing Predefined Values . ......................................................................................................311 Variables Containing Objects of Special Interest . .........................................................................................313 Summary . ..........................................................................................................................................................318 ■Chapter 12: More on Commands .......................................................................................... 319 ix Download from Wow! eBook <www.wowebook.com>
Trang 12
■ CONTENTS Understanding How and Where Commands Are Defined. ..................................................................................319 Application-Defined Commands . ..................................................................................................................319 Scripting Addition Commands . .....................................................................................................................322 AppleScript Commands . ...............................................................................................................................323 User-Defined Commands. .............................................................................................................................324 Picking the Right Target for Your Commands. ...................................................................................................325 The Five Standard AppleScript Commands . ......................................................................................................327 The get Command. ........................................................................................................................................327 The set Command. ........................................................................................................................................327 The copy Command . .....................................................................................................................................328 The count Command. ....................................................................................................................................330 The run Command . .......................................................................................................................................330 Commands That All Applications Understand. ...................................................................................................331 The run Command . .......................................................................................................................................331 The launch Command . ..................................................................................................................................332 The open Command. .....................................................................................................................................332 The activate Command . ................................................................................................................................333 The quit Command. .......................................................................................................................................333 The Standard Suite of Application Commands . .................................................................................................334 The get Command. ........................................................................................................................................334 The set Command. ........................................................................................................................................335 The make Command. ....................................................................................................................................336 The exists Command . ...................................................................................................................................338 The count Command. ....................................................................................................................................338 The move Command . ....................................................................................................................................339 The duplicate Command . ..............................................................................................................................340 The delete Command. ...................................................................................................................................341 The print Command . .....................................................................................................................................342 The save Command . .....................................................................................................................................343 The close Command . ....................................................................................................................................344 More on Working with get, set, and copy Commands . ......................................................................................345 When Do Implicit get Commands Occur? . ....................................................................................................345 When Are Explicit get Commands Necessary? . ............................................................................................347 Telling the Difference Between AppleScript and Application set Commands................................................350 Getting and Setting Multiple Values in a Single Statement . .........................................................................351 Using copy As an Application Command . .....................................................................................................352 Changing How Commands Are Handled . ...........................................................................................................354 Considering and Ignoring Application Responses. ........................................................................................354 Considering and Ignoring Other Attributes. ...................................................................................................357 Controlling the Timeout Delay for Long-Running Commands........................................................................357 Sending Commands to Remote Applications. ....................................................................................................359 Enabling Remote Apple Events . ....................................................................................................................359 Talking to Remote Machines . .......................................................................................................................361 Authenticating Access to Remote Machines . ...............................................................................................362 Compiling Scripts Offline ...............................................................................................................................363 Using the choose remote application Command . .........................................................................................364 Targeting Remote Applications by User and Process IDs . ............................................................................364 Launching Remote Applications . ..................................................................................................................366 Summary . ..........................................................................................................................................................366 ■Chapter 13: More on Operators and Coercions..................................................................... 369 Understanding the a reference to Operator . ......................................................................................................369 x Download from Wow! eBook <www.wowebook.com>
Trang 13
■ CONTENTS Introducing Unit Type Conversions .....................................................................................................................373 A Summary of AppleScript Coercions . ...............................................................................................................374 Coercing Class and Constant Names . ...........................................................................................................375 Coercing Numbers and Text . ........................................................................................................................376 Coercing File Objects .....................................................................................................................................377 Coercing Lists ................................................................................................................................................378 A Summary of AppleScript Operators . ...............................................................................................................379 Boolean Logic Operators. ..............................................................................................................................379 The Concatenation Operator . ........................................................................................................................379 Math Operators ..............................................................................................................................................380 Comparison Operators . .................................................................................................................................381 Containment Operators. ................................................................................................................................382 The Coercion Operator ...................................................................................................................................383 The a reference to Operator. .........................................................................................................................383 Operator Precedence in Detail . ..........................................................................................................................384 Understanding Precedence. ..........................................................................................................................385 Understanding Forms of Association . ...........................................................................................................386 Understanding when You Should Use Parentheses . .....................................................................................387 Summary . ..........................................................................................................................................................388 ■Chapter 14: Making Decisions Using Conditionals and Loops.............................................. 389 Choosing Your Course with Conditional Blocks . ................................................................................................389 Understanding the Basic Conditional Statement . .........................................................................................390 The Different Flavors of Conditional Statements . .........................................................................................391 Offering an Alternative Ending with the else Clause......................................................................................391 Offering Multiple Choices with else if Clauses . ............................................................................................392 Tips for Writing Conditional Statements . ......................................................................................................393 Running an Assembly Line with Repeat Loops . .................................................................................................394 The Different Flavors of Repeat Loops. .........................................................................................................395 Repeating Forever..........................................................................................................................................396 Repeating n Times . .......................................................................................................................................397 Repeating with Numbers in a Range . ...........................................................................................................397 Repeating with Items in a List . .....................................................................................................................400 Repeating While/Until a Condition Is Met. .....................................................................................................404 Tips for Writing Repeat Statements. .............................................................................................................406 Summary . ..........................................................................................................................................................410 ■Chapter 15: Making Decisions When Dealing with Errors .................................................... 413 Understanding Compilation Errors......................................................................................................................414 Simple Typos .................................................................................................................................................414 Unbalanced Statements. ...............................................................................................................................414 Unbalanced Parentheses and Unescaped Quotes . .......................................................................................414 Unrecognized Keywords . ..............................................................................................................................415 Reserved Word Mishaps . ..............................................................................................................................415 Invalid Date Literals .......................................................................................................................................416 Understanding Runtime Errors. ..........................................................................................................................416 How Runtime Errors Work in AppleScript . ....................................................................................................416 Understanding the Psychology of an Error.....................................................................................................419 Understanding the Anatomy of an Error.........................................................................................................419 Trapping Runtime Errors. ..............................................................................................................................420 Generating Runtime Errors . ..........................................................................................................................425 Tips for Handling Runtime Errors. .................................................................................................................428 Understanding Common Error Numbers . ...........................................................................................................431 xi Download from Wow! eBook <www.wowebook.com>
Hiển thị 13 trang đầu. Tải tài liệu để xem đầy đủ.
Bình luận (0)
Chưa có bình luận nào. Hãy là người đầu tiên!