ebook img

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

840 Pages·2004·22.927 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 AppleScript: A Comprehensive Guide to Scripting and Automation on Mac OS X

AppleScript: A Comprehensive Guide to Scripting and Automation on Mac OS X Hanaan Rosenthal -D!SIGNER TO DISIGMII• APress Media, LLC AppleScript: A Comprehensive Guide to Scripting and Automation on Mac OS X Copyright © 2004 by Hanaan Rosenthai Originally published by Apress in 2004 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 978-1-59059-404-9 ISBN 978-1-4302-5352-5 (eBook) DOI 10.1007/978-1-4302-5352-5 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. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. 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 lass or darnage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is freely available to readers at http: I /www. friendsofed. com in the Downloads section. Credits Lead Editor Productlon Manager Chris Mills Kari Brooks-Copony Technical Revlewer Productlon Editor Bill Cheeseman Ellie Fountain Editorlai Board Composltor Steve Anglin, Dan Appleman, Katy Freer Ewan Buckingham, Gary Cornell, Tony Davis, john Franklin, Proofreader jason Gilmore, Chris Mills, Liz Welch Dominic Shakeshaft, ]im Sumser lndexer john Collin Project Manager Sofia Marchant Artist April Milne Copy Edlt Manager Nicole LeCierc Cover Designer Kurt Krames Copy Editor Ami Knox Manufacturlng Manager Tom Debolski To}ohanne CONTENTS AT A GLANCE About the Author • • • • • • • • • • • • • • • • • • • • • • • • xxviii About the Technical Reviewer • • • • • • • • • • • • • • • • • • xxlx Acknowledgments • • • • • • • • • • • • • • • • • • • • • • • • • xxx Preface: What makes AppleScript unique and. what will make this book your favorite?. • • • • • • • • • • • • xxxi Resources • • • • • • • • • • • • • • • • • • • • • • • • • • • • xxxiii PART ONE: READ ME FIRST Chapter 1: lntroduction • • • • • • • • • • • • • • • • • • • • • • • 1 PART TWO: HOW TO APPLESCRIPT Chapter 2: Starting to script-aU over again ••••••••••• 13 Chapter 3: Values • • • • • • • • • • • • • • • • • • • • • • • • • • 55 Chapter 4: Doing the math: All about numbers • • • • • • • • • 117 CONTENTS AT A GLANCE Chapter 5: Pieking up dates • • • • • • • • . • • • • • • • • • • • 145 Chapter 6: Lists and records. • • • • • • • • • • • • • • • • • • • 187 Chapter 7: Giving commands • • • • • • • • • • • • • • • • • • • 217 Chapter 8: Variables • • • • • • • • • • • • • • • • • • • • • • • • 231 Chapter 9: Operations and coercion • • • • • • • • • • • • • • • 261 Chapter 10: Teaching your script to make decisions ••••••• 305 Chapter 11: The assembly line: Creating repeat IQops • • • • • 321 Chapter 12: User interaction • • • • • • • • • • • • • • • • • • • 345 Chapter 13: Working with files • • • • • • • • • • • • • • • • • • 429 Chapter 14: Working with the clipboard ••••••••••••• 497 Chapter 15: Turn errors in your favor •••••••••••••• 509 Chapter 16: Other control statements • • • • • • • • • • • • • • 529 Chapter 17: Defining and calling subroutines • • • • • • • • • • 535 Chapter 18: Script objects • • • • • • • • • • • • • • • • • • • • • 569 PART THREE: THE WILD WORLD OF APPLESCRIPT Chapter 19: AppleScript amenities in Mac OS X ••••••••• 595 Chapter 20: Scripting additions and extendibility •••••••• 615 vi CONTENTS AT A GLANCE Chapter 21: The fundamentals of automating applications. • • 637 Chapter 22: Debugging scripts . . . . . . . . . • • . . . • • • . 653 Chapter 23: A script by any other name. . . . . • • . . • . . • • 667 Chapter 24: Healthy scriptwriting practices . . • • . • • • • • . 677 Chapter 25: Scripting Apple apps . . . . . . . . . . • • • . • • • 697 Chapter 26: Scripting data and databases ••••.••••••. 717 Chapter 27: Automating media workflow •.•.•••••••• 741 Chapter 28: Smile: The AppleScript integrated production environment ..••.....••••••••••••. 749 Chapter 29: Automating UNIX applications • • . . • • • • • • . 785 Chapter 30: Scheduling scripts . . . . . . . . . . . • • • • • • • 793 Chapter 31: Controlling remote applications. . . • . . . . . . • 799 Chapter 32: The business of automation . . . . • • . . . . . . • 807 Index ............•................... 815 vii CONTENTS . . . . . . . . . . . . . . . . . About the Author • • • • • • • xxviii . . . . . . . . . . . . . . . . . . About the Technical Reviewer xx:ix Acknowledgments • • • • • • • • • • • • • • • • • • • • • • • • • xxx Preface: What makes AppleScript unique and . . . . . . . . what will make this book your favorite?. • xxxi ... Resources • • • • • • • • • • • • XXXIII PART ONE: READ ME FIRST Chapter 1: lntroduction • • • • • • • • • • • • • • • • • • • • • • • 1 What are scripts for? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Automating small things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Automating big things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Where's AppleScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 How are scripts written? ...................................... 5 Where do scripts reside and how do they run? ......................... 5 AppleScript file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The golden triangle: AppleScript language, application scriptability, and the scripts you write ............................................... 7 The better script model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Teaching the script new commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Packaging your commands into script objects . . . . . . . . . . . . . . . . . . . . . . . 9 Wrapping up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 PART TWO: HOW TO APPLESCRIPT Chapter 2: Starting to script-aU over again • • . . • • . • • • • 13 Script concentrate: just add water! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 More results with less talk .................................... 20 Variable speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 viii CONTENTS Values come in many dasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Text .............................................. 23 Number ............................................ 23 Boolean ............................................ 23 Tell me about it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 The many faces of tell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Continuation character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Comparing results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Objects you can tell things to . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Telling in blocks ....................................... 26 More on Script Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Event log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Result history ........................................... 28 Script description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Toolbar ............................................... 29 Recording scripts ......................................... 30 Spaces don't count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Understanding applications' scriptability ............................ 31 The application scripting dictionary ............................... 31 Classes in the dictionary ................................... 31 Commands in the dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 At their mercy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 The object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Objects and dass inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Classes and commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 What makes an object what it is? A Iook at object properties . . . . . . . . . . . . . . . . 36 Get properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Read only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Property, inheritance, and dass! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 How to talk to objects so they Iisten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Object names ........................................... 41 May I see some ID please? .................................... 41 Every ... whose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Index and range reference forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Relatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Objects and commands they understand . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Making your own commands .................................. 45 Handlers can deal with arguments ............................... 48 Handlers return results ...................................... 51 Chapter 3: Values . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Stringing characters ........................................ 57 What can we da with text? ................................. 57 Special string characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 ASCII's a sure thing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 A string operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Considering and ignoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 "My Dad" comes before "Your Dad" . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ix CONTENTS Sorting text . . . . . . . • • . • . • . . . . • • . • • • • • . • . • • • • . • • . • . • • . • 7l Breaking up strings . • . . • . . • . • . . . . . • . . . • • • • • . . . . • . • . . . • • • . 72 Characters . . . . . . . . . . . . . . • • . • • . . • • . . . • . • • • • • • • • • • • • • • • 73 Pieking the pieces . . . . . . . . . . • • • • . . . . . • . • • . • • . • • • • • • • • • • • 77 Chunk at a time . . . . . . . . . • • • • • • • . . • • • • • • • • • • • • • • • • • • • • • 79 Words ..........••.•••....••••••.•••••••••••••...•• 80 Paragraphs . . . . • . . • • • . • • • . . . • . • • • • • • . • • • • • . • • • . • • • . . . 83 Words and paragraphs in the real world • • • • . . . . . . • • • • . • • • • • • • • • • • 84 Mail to iCal script . . . . . . • • • • . . • • • • • • • • • . • . . . . • • • • • • • • • . • • 85 Text item delimiters . . . . . . . • . • • • • • . • • • • • • . • . • • • • . • • • • • • • • 93 Offset ...............••.•••••.••.••..••.••.....••• 108 Power wrap-up . . . . . • . • • . . . . • • • • • • • . • • • • • • • • . . . . • • • • • • • • • 11 0 Strings and variables . . . . . . . • . • . . . . • • • . . . • . • • . . . • • . • • • • • • 111 Escape character . . . . • • • . • . • . . . . . . • . • • • . • • • • • • • • • • • • • • . 111 lncluding tabs, returns, and spaces • . • . . . . • . • . . . • • • • . • • • • . • • • • . 111 ASCII numbers and characters . • • . • . . . • • • . • • . . • • • • • • • . • . . • • . . 112 String operators . . . . . . . . . . . . . • • • • • • • • • • . • . . . . . . • • . • • • • • 112 Comparing strings . . . . . . • • . • . . • • • • . • . . . • • . . . • . . . • • • • • • • • 112 Considering and ignoring . . • . . . . . . . . • . . • . • • • • • • • • . • • • • • • • • • 113 Length ..........••••••.••...••••••••••••••••••.••• 113 String parts . . . . . . . . . . • • . . . . . • . . . • . • • • • • • • • • • • • • • . • • • 113 Text itein delimiters . . . . . . . • . • . . • • • • • • • . • • • . • • . • • • . • • • • • . 113 Offset command . . . . . . . . . . . . . • • • . • . . • . • • • . . • • • • • • • • • • • 114 Details previously. . . . . . . . . . • • . • • • • . • • • • • . • • . • . • • • • • • • • • • 114 Chapter 4: Doing the math: All about numbers . • • . . . . . . 117 Massaging numbers ........•.•..••..•••..•••.•.••.••.•.•.• 118 Real big ............•..••..••••.••••••.•.•••.••••••••• 119 Rounding numbers . . . . . . . . . . . . . • . . • • . . • • . . . . • • • . • • . • . • • . . 119 Rounding up . . . . . . . . . . . . . • • • • • • • • • . • • • • • . • • • • • • . • . . . • 120 Rounding down . . . . . . . • . . . • • . . . • • . • • • • • • • . . . • • . • • • • . • . 120 Rounding toward zero • . • • • • . • • • . • . . . • • • • • . • . • • • • • . . • • • • • 120 Rounding to nearest or as taught in school • . • • • • • • • • • • • . • . . • • • • • • 120 Rounding to other increments •...•••••••••••••••••••.••••.•• 121 Rounding handler ....•.•. ·, ••.•••.••••.•• ; ••.••••••••••• 124 Scripting at random . . . . . • . . • • • • . . • . . . . . • . • . . • • • • . • • • • • • • • • 125 Random number result . • . • . • • . • • • . • . • . . . • • . • • • • • • • • • • • • • • 125 Parameters . . . . . . . . . . . . . . • . • • • • • • • • • • • . • . . . • • • • • • • • • • 125 Blackjack! . . . . . . . . . . . . . . • • • • . • • • • • • • • • • • • . • • • . . • • • • • 127 You do the math . . . . . . . . . . • . • • . • • • • • • • . • • . • • • • . • • • • • • • • • • 132 Comparing with logic . . • . • • . . . . • . • . • • . • • • • • • • • • • . . . • • • • • • 133 Basic math . . . . . . . . . • . • . . . . • . . • • • • • • • • • • • • . • • • • • • . • • • 134 mod and div . . . • . • . . . . • • . . • . . . . . . . . • • . • • • • . • • • • • • • • • • 136 Reversing numbers . • . . • . . • . • . • • . . . . • • • . . • • • • • • • • • • • • • . • 136 Fotder kitchen timer • • . . . • . • . • . . . • . . • . • • • • • • • • • • • • • • . . • • 137 Conclusion . . . . . . . . . . . . . . . • • . • • • • • • • • • • • • • • • . • • • • • • • • • • 139 Power wrap-up . . . . . . . . . . . . . . • . • • • . • • • • . • • . . • . • • • • • • . • • . . 139 X

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.