Applescript: a Beginner's Guide" & Tab & Tab & "Chapter 14"
Total Page:16
File Type:pdf, Size:1020Kb
AppleScript: A Beginner’s Guide About the Author Guy Hart-Davis is the author of Mac OS X Leopard QuickSteps, How to Do Everything: iPod, iPhone, & iTunes, HTML, XHTML & CSS QuickSteps, and several other equally fine computer books. About the Technical Editor Greg Kettell is a Windows programmer by day, but by night loves his Mac. Greg has served as an author, contributing author, and/or technical editor for an ever-increasing number of technical books. AppleScript: A Beginner’s Guide Guy Hart-Davis New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-163955-2 MHID: 0-07-163955-1 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-163954-5, MHID: 0-07-163954-3. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. To contact a representative please e-mail us at [email protected]. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. This book is dedicated to Teddy, who helped develop some of the sample scripts. This page intentionally left blank Contents at a Glance 1 Grasping the Essentials of AppleScript ................................... 3 2 Up to Speed with AppleScript Editor ..................................... 11 3 Creating Your First Script ................................................ 27 4 Working with Variables, Classes, Operators, and Coercions ............. 61 5 Working with Text, Numbers, and Dates .................................. 81 6 Working with the Finder, Files, and Folders .............................. 101 7 Making Decisions in Your Scripts ......................................... 129 8 Using Dialog Boxes to Get User Input ..................................... 137 9 Repeating Actions in Your Scripts ......................................... 179 10 Debugging and Handling Errors .......................................... 195 vii viii AppleScript: A Beginner’s Guide 11 Running Scripts Automatically ............................................ 219 12 Automating iTunes and iPhoto ............................................ 249 13 Automating Apple Mail ................................................... 271 14 Automating Microsoft Word .............................................. 291 15 Automating Microsoft Excel .............................................. 337 16 Automating Microsoft Entourage ......................................... 377 Index ...................................................................... 411 Contents ACKNOWLEDGMENTS ........................................................... xix INTRODUCTION .................................................................. xxi PART I Getting Started with AppleScript 1 Grasping the Essentials of AppleScript ................................... 3 Knowing What AppleScript Is and What You Can Do with It ........................ 4 What You Can Do with Scripts ................................................ 4 Why AppleScript Is Easy to Learn ............................................. 5 Understanding What Scripts Are .................................................... 5 What a Script Is ............................................................... 5 Where Scripts Are Stored ..................................................... 5 How You Create Scripts ....................................................... 6 How You Run Scripts ......................................................... 6 Understanding Objects, Keywords, Commands, and Properties ...................... 7 What Objects Are ............................................................. 7 What Keywords Are .......................................................... 7 What Commands Are ......................................................... 8 What Properties and Values Are ............................................... 9 ix x AppleScript: A Beginner’s Guide 2 Up to Speed with AppleScript Editor ..................................... 11 Launching AppleScript Editor ...................................................... 12 Meeting the AppleScript Editor Window ............................................ 13 Setting Up AppleScript Editor for Working Comfortably ............................. 16 Choosing General Preferences ................................................. 16 Choosing Editing Preferences ................................................. 18 Choosing Formatting Preferences ............................................. 20 Choosing History Preferences ................................................. 22 Choosing Plug-ins Preferences ................................................ 23 Putting the Script Menu on the Menu Bar in Leopard ................................ 23 Running a Script from the Script Menu .............................................. 25 3 Creating Your First Script ................................................ 27 Opening AppleScript Editor ......................................................... 28 Creating tell Statements ............................................................ 29 Try This: Opening a Finder Window Showing the Documents Folder ................ 30 Saving a Script ..................................................................... 31 Try This: Saving Your Script ........................................................ 31 Creating tell Blocks ................................................................. 32 Creating a tell Block Manually ................................................ 32 Try This: Using a tell Block ......................................................... 32 Using the Tell Application Pop-Up Menu ..................................... 33 Adding Comments to Your Code .................................................... 34 Creating End-of-Line Comments .............................................. 35 Try This: Commenting Out a Line .................................................. 36 Creating Block Comments .................................................... 36 Try This: Creating a Comment Block ............................................... 37 Recording Actions into a Script ..................................................... 37 Try This: Recording Actions: Repositioning and Resizing the Finder Window . 38 Examining the Recorded Code ..................................................... 39 Activating an Application ....................................................