ebook img

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X PDF

1105 Pages·2010·21.52 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X

CYAN YELLOW SPOT MATTE MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Companion eBook Available A ppleScript is an English-like, easy-to-understand scripting language built The most complete book available S LE into every Mac. AppleScript can automate hundreds of AppleScriptable L T TI applications, performing tasks both large and small, complex and simple. e covering AppleScript 2 and Mac OS X automation D E T Learn AppleScript: The Comprehensive Guide to Scripting and Automation on a A REL Mac OS X, Third Edition has been completely updated for Mac OS X Snow r Leopard. It’s all here, with an emphasis on practical information that will n help you solve any automation problem, from the most mundane, repeti- tive tasks to highly integrated workflows of complex systems. This edition includes major contributions from expert AppleScripters Emmanuel Levy, A Harald Monihart, Ian Piper, Shane Stanley, Barry Wainwright, and Craig Wil- liams and includes a foreword by AppleScript inventor, William Cook. p You’ll learn the following in this book: • How to script professional productivity applications—Apple iWork, Microsoft Office, FileMaker Pro, and Adobe InDesign p • How to automate iTunes, Mail, iCal, and other popular lifestyle applications in Mac OS X l • How AppleScript represents information as objects—including, e numbers, strings, lists, and records • How to manipulate those objects using commands and operators and how to store them in variables S • How to manipulate the Mac OS X file system • How to harness the power of the Unix command line in Mac OS X c Learn AppleScript • How to perform sophisticated text processing techniques using r regular expressions • How to create your own Cocoa applications with the new i AppleScriptObjC bridge The Comprehensive Guide to Scripting p Whether you are a first-time scripter who wants to automate tasks on your Mac, an experienced AppleScripter looking to develop proficient, profes- sional, or guru-level knowledge and skills, or a professional Mac OS X de- t and Automation on Mac OS X veloper wishing to understand this powerful and pervasive technology, this book is for you! THIRD THIRD EDITION EDITION RoSan Hamish Sanderson  |  Hanaan Rosenthal sed ne COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION ISBN 978-1-4302-2361-0 tharson Foreword by Dr. William Cook l 54999 US $49.99 Shelve in Macintosh/Programming SOURCE CODE ONLINE User level: www.apress.com Beginner–Advanced 9 781430 223610 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 2.0625" 1104 page count Learn AppleScript The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition ■ ■ ■ Hamish Sanderson and Hanaan Rosenthal i 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 [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], 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 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(cid:1) ■Chapter 1: Introducing AppleScript .......................................................................................... 3(cid:1) ■Chapter 2: AppleScript in Principle ........................................................................................ 15(cid:1) ■Chapter 3: AppleScript in Practice ......................................................................................... 33(cid:1) Part II: Understanding AppleScript ............................................................................................ 51(cid:1) ■Chapter 4: Writing Scripts in AppleScript Editor .................................................................... 53(cid:1) ■Chapter 5: Understanding How Application Scripting Works ................................................. 77(cid:1) ■Chapter 6: Learning to Work with AppleScript Objects ........................................................ 143(cid:1) ■Chapter 7: Working with Text ............................................................................................... 163(cid:1) ■Chapter 8: Working with Numbers ....................................................................................... 215(cid:1) ■Chapter 9: Working with Dates............................................................................................. 241(cid:1) ■Chapter 10: Working with Lists and Records ....................................................................... 267(cid:1) ■Chapter 11: Storing Objects in Variables.............................................................................. 293(cid:1) ■Chapter 12: More on Commands .......................................................................................... 319(cid:1) ■Chapter 13: More on Operators and Coercions..................................................................... 369(cid:1) ■Chapter 14: Making Decisions Using Conditionals and Loops.............................................. 389(cid:1) ■Chapter 15: Making Decisions When Dealing with Errors .................................................... 413(cid:1) ■Chapter 16: Interacting with the User .................................................................................. 437(cid:1) ■Chapter 17: Working with Files ............................................................................................ 471(cid:1) iii ■ 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(cid:1) ■Chapter 20: Scripting the File System .................................................................................. 587(cid:1) ■Chapter 21: Scripting Apple Applications............................................................................. 623(cid:1) ■Chapter 22: Extending AppleScript with Scripting Additions ............................................... 675(cid:1) ■Chapter 23: AppleScript Amenities....................................................................................... 711(cid:1) ■Chapter 24: Scripting iWork and Office ................................................................................ 743(cid:1) ■Chapter 25: Scripting Data and Databases........................................................................... 793(cid:1) ■Chapter 26: Scripting Adobe InDesign.................................................................................. 817(cid:1) ■Chapter 27: Interacting with the Unix Command Line.......................................................... 863(cid:1) ■Chapter 28: Using Smile: The AppleScript Integrated Production Environment ................................................................................................. 897(cid:1) ■Chapter 29: Tips and Techniques for Improving Your Scripts.............................................. 927(cid:1) ■Chapter 30: Creating Cocoa Applications with AppleScriptObjC .......................................... 969(cid:1) ■Index................................................................................................................................... 1019 iv Contents ■Contents at a Glance................................................................................................................ iii(cid:1) ■Contents.....................................................................................................................................v(cid:1) ■Foreword ................................................................................................................................xix ■About the Authors...................................................................................................................xxi(cid:1) ■About the Technical Reviewer ...............................................................................................xxii(cid:1) ■Acknowledgments ................................................................................................................xxiii(cid:1) ■Introduction ..........................................................................................................................xxiv(cid:1) Part I: Welcome to AppleScript.................................................................................................... 1(cid:1) ■Chapter 1: Introducing AppleScript .......................................................................................... 3(cid:1) Automating with AppleScript: From Small Tasks to Large ....................................................................................3(cid:1) Automating Small Tasks ....................................................................................................................................3(cid:1) Automating Large Tasks ....................................................................................................................................4(cid:1) When to Automate (or Not)?..............................................................................................................................5(cid:1) The Golden Triangle: Scriptable Applications, the AppleScript Language, and the Scripts You Write ..................6(cid:1) What Is a Scriptable Application? ......................................................................................................................6(cid:1) What Is the AppleScript Language?..................................................................................................................8(cid:1) What Is a Script?................................................................................................................................................9(cid:1) Your First AppleScript: Hello World!......................................................................................................................10(cid:1) Summary .............................................................................................................................................................13(cid:1) ■Chapter 2: AppleScript in Principle ........................................................................................ 15(cid:1) The Four Key Concepts of Application Scripting..................................................................................................16(cid:1) How Applications Represent Information As Objects......................................................................................16(cid:1) How Applications Manipulate Objects Using Commands ...............................................................................18(cid:1) 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(cid:1) How AppleScript Represents Information As Objects.....................................................................................23(cid:1) How AppleScript Manipulates Objects Using Commands, Operators, and Variables ......................................24 How AppleScript Organizes Code Using Handlers and Script Objects............................................................26(cid:1) v ■ CONTENTS Tying It All Together: What Makes AppleScript Special.......................................................................................28(cid:1) The English-like Syntax ..................................................................................................................................28(cid:1) Built-in Application Scripting Support ............................................................................................................29(cid:1) Attaching Scripts to Applications.....................................................................................................................29(cid:1) The AppleScript Community ...........................................................................................................................30(cid:1) Summary .............................................................................................................................................................31(cid:1) ■Chapter 3: AppleScript in Practice ......................................................................................... 33(cid:1) The Project: Tidy Desktop .....................................................................................................................................33(cid:1) Planning the Script ..............................................................................................................................................34(cid:1) Writing the Script.................................................................................................................................................35(cid:1) Creating the Folder Name...............................................................................................................................35(cid:1) Interacting with the File System......................................................................................................................39(cid:1) Final Thoughts .....................................................................................................................................................46(cid:1) Improving Reliability ........................................................................................................................................46(cid:1) Adding Features...............................................................................................................................................47(cid:1) Documenting Your Code .................................................................................................................................48(cid:1) Summary .............................................................................................................................................................49(cid:1) Part II: Understanding AppleScript ............................................................................................ 51(cid:1) ■Chapter 4: Writing Scripts in AppleScript Editor .................................................................... 53(cid:1) Working with Script Windows..............................................................................................................................53(cid:1) Compiling a Script............................................................................................................................................54(cid:1) Running and Stopping a Script .......................................................................................................................55(cid:1) Recording a Script ...........................................................................................................................................55(cid:1) Viewing the Result of Running a Script ..........................................................................................................56(cid:1) Viewing the Events Sent by a Running Script.................................................................................................56(cid:1) Adding a Description to a Script ......................................................................................................................57(cid:1) Viewing Application Dictionaries ..........................................................................................................................57(cid:1) Other Useful AppleScript Editor Features ............................................................................................................58(cid:1) Using the Library Window...............................................................................................................................59(cid:1) Navigating to a Handler in a Script ..................................................................................................................60(cid:1) Viewing the Event Log History ........................................................................................................................61(cid:1) Scripting the AppleScript Editor.......................................................................................................................61(cid:1) Using Context Menu Scripts............................................................................................................................62(cid:1) Viewing the Bundle Contents Drawer .............................................................................................................62(cid:1) Setting the Script Formatting Preferences .....................................................................................................63(cid:1) Saving Scripts......................................................................................................................................................65(cid:1) Text-Based Scripts...........................................................................................................................................66(cid:1) Compiled Scripts..............................................................................................................................................66(cid:1) Script-Based Applications (Applets) ...............................................................................................................69(cid:1) Other Tools for Writing and Running AppleScripts ..............................................................................................72(cid:1) The System-wide Script Menu........................................................................................................................72(cid:1) Third-Party AppleScript Editors ......................................................................................................................74(cid:1) Developing GUI Applications ...........................................................................................................................76(cid:1) Command-Line Tools......................................................................................................................................76(cid:1) Summary .............................................................................................................................................................76(cid:1) ■Chapter 5: Understanding How Application Scripting Works ................................................. 77(cid:1) A Quick Application Scripting Example.................................................................................................................78(cid:1) Understanding Objects..........................................................................................................................................79(cid:1) How Objects Are Categorized by Class...........................................................................................................79(cid:1) Introducing Object Properties .........................................................................................................................81(cid:1) vi ■ CONTENTS Introducing Object Elements...........................................................................................................................84(cid:1) Understanding Commands...................................................................................................................................86(cid:1) Introducing Command Names ........................................................................................................................87(cid:1) Introducing Command Parameters .................................................................................................................87(cid:1) Commands Must Be Targeted at Objects.........................................................................................................89(cid:1) Commands Can Return Results or Report Errors............................................................................................90(cid:1) Understanding the Application Object Model.......................................................................................................91(cid:1) How Objects Can Contain Other Objects.........................................................................................................92(cid:1) How AppleScript Refers to Application Objects ..............................................................................................93(cid:1) Exploring a Typical Object Model....................................................................................................................98(cid:1) Understanding Application Dictionaries.............................................................................................................103(cid:1) Introducing AppleScript Editor’s Dictionary Viewer ......................................................................................104(cid:1) How Classes Are Documented......................................................................................................................107(cid:1) How Commands Are Documented ................................................................................................................118(cid:1) Application Dictionaries Don’t Tell You Everything!......................................................................................120(cid:1) More on application Objects ..............................................................................................................................123(cid:1) Creating application Objects.........................................................................................................................123(cid:1) The Standard Properties of application Objects ...........................................................................................126(cid:1) How AppleScript Compiles tell application ... Blocks ...................................................................................127(cid:1) More on Constructing References .....................................................................................................................128(cid:1) Referring to Properties...................................................................................................................................129(cid:1) Referring to Elements ....................................................................................................................................129(cid:1) Referring to Insertion Locations.....................................................................................................................138(cid:1) Summary ...........................................................................................................................................................139(cid:1) ■Chapter 6: Learning to Work with AppleScript Objects ........................................................ 143(cid:1) How AppleScript Represents Information As Objects........................................................................................144(cid:1) What Kinds of Objects Does AppleScript Provide? .......................................................................................144(cid:1) Creating New Objects ....................................................................................................................................145(cid:1) Getting Information from Objects..................................................................................................................145(cid:1) How AppleScript Works with Objects ................................................................................................................146(cid:1) Manipulating Objects with Commands .........................................................................................................146(cid:1) Manipulating Objects with Operators ...........................................................................................................148(cid:1) Coercing Objects............................................................................................................................................153(cid:1) Storing Objects in Variables..........................................................................................................................154(cid:1) Working with Boolean Objects...........................................................................................................................157(cid:1) Boolean Operators .........................................................................................................................................158(cid:1) Boolean-Related Coercions...........................................................................................................................160(cid:1) Summary ...........................................................................................................................................................161(cid:1) ■Chapter 7: Working with Text ............................................................................................... 163(cid:1) Introducing Text.................................................................................................................................................163(cid:1) A Brief History of Text....................................................................................................................................164(cid:1) Introducing Text in AppleScript 2.0...............................................................................................................169(cid:1) How to Write Literal Strings in AppleScript .......................................................................................................170(cid:1) The Properties of Text.........................................................................................................................................174(cid:1) The class Property......................................................................................................................................174(cid:1) The length Property....................................................................................................................................174(cid:1) The quoted form Property..........................................................................................................................174(cid:1) The id Property ............................................................................................................................................174(cid:1) The Elements of Text ..........................................................................................................................................175(cid:1) Characters ....................................................................................................................................................175(cid:1) vii ■ CONTENTS Words............................................................................................................................................................178(cid:1) Paragraphs....................................................................................................................................................179(cid:1) Text Ranges ...................................................................................................................................................179(cid:1) Operators and Commands .................................................................................................................................181(cid:1) Joining Strings...............................................................................................................................................181(cid:1) Comparing Strings .........................................................................................................................................182(cid:1) Checking for Strings Within Strings..............................................................................................................184(cid:1) Considering and Ignoring Attributes .............................................................................................................185(cid:1) The count Command.....................................................................................................................................189(cid:1) The offset Command.....................................................................................................................................189(cid:1) Coercing to and from Text ............................................................................................................................190(cid:1) Working with Text Item Delimiters......................................................................................................................192(cid:1) Splitting Strings with Text Item Delimiters ...................................................................................................192(cid:1) Combining List Items into a Single String.....................................................................................................194(cid:1) Finding and Replacing Text ..........................................................................................................................196(cid:1) How AppleScript Text Works in Tiger ................................................................................................................197(cid:1) The Many Different Classes of Text ..............................................................................................................198(cid:1) Working with the Various Text Classes.........................................................................................................198(cid:1) The ASCII number and ASCII character Commands......................................................................................200(cid:1) Example Projects ...............................................................................................................................................201(cid:1) Defining a Find-and-Replace Command.......................................................................................................202(cid:1) Transferring Meeting Arrangements from Mail to iCal .................................................................................204(cid:1) Summary ...........................................................................................................................................................213(cid:1) ■Chapter 8: Working with Numbers ....................................................................................... 215(cid:1) Introducing Integers and Reals..........................................................................................................................215(cid:1) Operators and Commands .................................................................................................................................216(cid:1) Math Operations ............................................................................................................................................217(cid:1) Comparison Operations ................................................................................................................................221(cid:1) Introducing Operator Precedence.................................................................................................................224(cid:1) The round Command......................................................................................................................................225(cid:1) The random number Command .....................................................................................................................227(cid:1) Coercing to and from Numbers.....................................................................................................................229(cid:1) Example Projects ...............................................................................................................................................231(cid:1) Defining a Round-to-Decimal-Places Command ..........................................................................................231(cid:1) Blackjack! .....................................................................................................................................................233(cid:1) Kitchen Timer.................................................................................................................................................237(cid:1) Summary ...........................................................................................................................................................240(cid:1) ■Chapter 9: Working with Dates............................................................................................. 241(cid:1) Introducing Dates...............................................................................................................................................241(cid:1) Understanding Date and Time Formats ........................................................................................................242(cid:1) Forming Dates On-the-Fly.............................................................................................................................244(cid:1) Specifying a Time Relative to a Date ............................................................................................................246(cid:1) The Properties of Dates ......................................................................................................................................247(cid:1) The class Property......................................................................................................................................247(cid:1) The year Property ........................................................................................................................................247(cid:1) The month Property......................................................................................................................................247(cid:1) The day Property ..........................................................................................................................................248(cid:1) The weekday Property..................................................................................................................................248(cid:1) The hours Property......................................................................................................................................249(cid:1) The minutes Property..................................................................................................................................249(cid:1) viii

Description:
This third edition is updated to welcome a whole new generation of Mac users to AppleScript and how they can take control of their Mac. With more and more people choosing Macs, new demand and interest in Applescript is inevitable as people realize its power and usefulness. This new edition takes the
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.