AppleScript Studio® Programming for the Absolute Beginner JERRY LEE FORD, JR. © 2007 Thomson Course Technology, a division of Thomson Learning Publisher and General Manager, Inc. All rights reserved. No part of this book may be reproduced or Thomson Course Technology PTR: transmitted in any form or by any means, electronic or mechanical, Stacy L. Hiquet including photocopying, recording, or by any information storage or Associate Director of Marketing: retrieval system without written permission from Thomson Course Sarah O’Donnell Technology PTR, except for the inclusion of brief quotations in a review. Manager of Editorial Services: Heather Talbot The Thomson Course Technology PTR logo and related trade dress are trademarks of Thomson Course Technology, a division of Thomson Marketing Manager: Learning Inc., and may not be used without written permission. Mark Hughes AppleScript, AppleScript Studio, Finder, iTunes, Mac OS, and Xcode Acquisitions Editor: are trademarks of Apple Computer, Inc., registered in the U.S. and Mitzi Koontz other countries. Marketing Coordinator: All other trademarks are the property of their respective owners. Adena Flitt Important: Thomson Course Technology PTR cannot provide software Project Editor: support. Please contact the appropriate software manufacturer’s tech- Jenny Davidson nical support line or Web site for assistance. Technical Reviewer: Thomson Course Technology PTR and the author have attempted John Flynt throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the PTR Editorial Services manufacturer. Coordinator: Erin Johnson Information contained in this book has been obtained by Thomson Course Technology PTR from sources believed to be reliable. However, Interior Layout Tech: because of the possibility of human or mechanical error by our Digital Publishing Solutions sources, Thomson Course Technology PTR, or others, the Publisher Cover Designer: does not guarantee the accuracy, adequacy, or completeness of any Mike Tanamachi information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be par- Indexer: ticularly aware of the fact that the Internet is an ever-changing entity. Larry Sweazy Some facts may have changed since this book went to press. Proofreader: Educational facilities, companies, and organizations interested in Gene Redding multiple copies or licensing of this book should contact the Publisher for quantity discount information. Training manuals, CD-ROMs, and portions of this book are also available individually or can be tailored for specific needs. ISBN-10: 1-59863-303-1 ISBN-13: 978-1-59863-303-0 eISBN-10: 1-59863-781-9 Library of Congress Catalog Card Number: 2006904399 Printed in the United States of America 07 08 09 10 11 PH 10 9 8 7 6 5 4 3 2 1 Thomson Course Technology PTR, a division of Thomson Learning Inc. 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To my wonderful children, Alexander, William, and Molly, and my beautiful wife, Mary. A CKNOWLEDGMENTS T his book represents the hard work and effort of many individuals. I would like to thank Mitzi Koontz,, this book’s acquisitions editor. Thanks also go out to John Flynt, who served as the book’s technical editor. I’d be remiss if I did not thank Jenny Davidson for lending her considerable talents as both project and copy editor. Finally, I’d like to thank everyone else at Thomson Course Technology for all their contributions and hard work. A A BOUT THE UTHOR J erry Lee Ford, Jr. is an author, educator, and an IT professional with over 17 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, au- tomation engineer, and security analyst. Jerry has a master’s degree in Business Administration from Virginia Commonwealth University in Richmond, Virginia. He is the author of 18 books and co-author of 2 additional books. His published works include Beginning REALbasic, Perl Programming for the Absolute Beginner, Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner, VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Learn VBScript in a Weekend, Microsoft Windows Shell Scripting for the Absolute Beginner, Learn JavaScript in a Weekend, and Microsoft Windows XP Professional Admin- istrator’s Guide. He has over five years’ experience as an adjunct instructor teaching networking courses in Information Technology. Jerry lives in Richmond, Virginia, with his wife, Mary, and their children William, Alexander, and Molly. Table of Contents Introduction........................................................... xii Why AppleScript Studio?................................................................................................. xiii Who Should Read This Book?.......................................................................................... xiv What You Need to Begin.................................................................................................... xv How This Book Is Organized............................................................................................ xvi Conventions Used in This Book...................................................................................... xix Part I Introducing AppleScript Studio...............................1 Chapter 1 AppleScript Studio Basics.........................................3 Project Preview: The Knock-Knock Application............................................................. 4 Introducing AppleScript Studio......................................................................................... 6 A Little AppleScript Studio History.......................................................................... 6 A Quick Overview of AppleScript Studio Components....................................... 6 A Few Words About Object-Oriented Programming..................................................... 8 AppleScript: AppleScript Studio’s Programming Language....................................... 9 When to Write AppleScript...................................................................................... 11 When to Develop AppleScript Studio Applications........................................... 11 Working with the Script Editor............................................................................... 12 Accessing Application Dictionaries........................................................................ 15 Back to the Knock-Knock Application............................................................................ 17 Designing the Game................................................................................................... 18 Step 1: Creating a New AppleScript Studio Application................................... 19 Step 2: Creating a Graphical User Interface......................................................... 21 Step 3: Modifying Control Attributes and Attaching Controls to Event Handlers......................................................................................................................... 25 Step 4: Editing Your AppleScript............................................................................. 28 Step 5: Building and Running Your Mac OS X Application.............................. 30 Summary................................................................................................................................ 30 vi AppleScript Studio Programming for the Absolute Beginner Chapter 2 The AppleScript Studio Application Development Environment............................................................. 31 Project Preview: The AppleScript Studio Fortune Teller Game............................... 32 Project Templates................................................................................................................ 33 Setting Xcode Preferences................................................................................................. 36 Working with the Xcode Project Window.................................................................... 37 The Project Window Toolbar.................................................................................... 38 Groups & Files.............................................................................................................. 38 The Project Windows Detail View........................................................................... 40 The Editor Window..................................................................................................... 41 The Info Window......................................................................................................... 46 Integration with the Interface Builder.......................................................................... 48 The Terminology Browser.................................................................................................. 48 Debugging Capabilities...................................................................................................... 49 The Apple Developer Reference Library......................................................................... 50 Back to the AppleScript Studio Fortune Teller Game................................................ 51 Designing the AppleScript Studio Fortune Teller Game.................................. 51 Step 1: Creating a New Project................................................................................. 52 Step 2: Creating the User Interface......................................................................... 52 Step 3: Attaching Controls to Event Handlers..................................................... 55 Step 4: Adding a Little Programming Logic......................................................... 56 Step 5: Testing the Execution of the AppleScript Studio Fortune Teller Application.................................................................................................................... 57 Summary................................................................................................................................ 58 Chapter 3 Building a Graphical User Interface................... 59 Project Preview: The Typo-Matic Typing Test Game................................................... 60 Create Graphical User Interfaces..................................................................................... 63 Cocoa Controls............................................................................................................. 65 MainMenu.nib.............................................................................................................. 67 MainMenu.nib – MainMenu..................................................................................... 67 Window.......................................................................................................................... 68 Cocoa Interface Elements.................................................................................................. 68 Cocoa Menus................................................................................................................. 68 Cocoa Controls and Indicators................................................................................ 74 Cocoa Text Controls.................................................................................................... 80 Cocoa Windows............................................................................................................ 83 Cocoa Data Views........................................................................................................ 93 Cocoa Container Views.............................................................................................. 94 Back to the Typo-Matic Typing Test Game.................................................................... 95 Designing the Game................................................................................................... 95 Step 1: Creating a New Project................................................................................. 96 Contents vii Step 2: Creating the User Interface......................................................................... 96 Step 3: Customizing Menus and Control Attributes, and Associating Controls with Event Handlers.................................................................................. 97 Step 4: Adding a Little Programming Logic....................................................... 100 Step 5: Testing the Execution of the Typo-matic Typing Test Game Application.................................................................................................................. 104 Summary.............................................................................................................................. 105 Part II Learning How to Write AppleScripts..................107 Chapter 4 Working with Values, Variables, and Classes. 109 Project Preview: The Star Trek Trivia Game............................................................... 109 AppleScript—The Technology......................................................................................... 112 AppleScript—The Scripting Language.......................................................................... 112 Language Extensibility............................................................................................. 112 Language Features.................................................................................................... 113 Managing Data Using Variables..................................................................................... 116 Storing Values in Variables..................................................................................... 116 Retrieving Variable Values...................................................................................... 117 Naming Variables.............................................................................................................. 118 Case Sensitivity.......................................................................................................... 119 Undefined Variables................................................................................................. 120 Working with Different Variable Classes.................................................................... 121 The String Class......................................................................................................... 121 The Real Class............................................................................................................. 121 The Integer Class....................................................................................................... 121 The Date Class............................................................................................................ 122 The Boolean Class...................................................................................................... 124 Back to the Star Trek Trivia Game................................................................................ 124 Designing the Game................................................................................................. 124 Step 1: Creating a New Project............................................................................... 125 Step 2: Creating the User Interface....................................................................... 125 Step 3: Customizing Control Properties.............................................................. 128 Step 4: Adding a Little Programming Logic....................................................... 129 Step 5: Testing the Execution of the Star Trek Trivia Game.......................... 138 Application Code Review......................................................................................... 139 Summary.............................................................................................................................. 142 viii AppleScript Studio Programming for the Absolute Beginner Chapter 5 Applying Conditional Logic and Building Loops........................................................................ 145 Project Preview: The Guess a Number Game............................................................. 146 Implementing Conditional Logic.................................................................................. 149 The if Statement........................................................................................................ 149 The if…else Statement.............................................................................................. 150 The if…else if Statement.......................................................................................... 151 Nested if Statements................................................................................................ 152 Different Types of Comparisons.................................................................................... 153 Building Compound Tests............................................................................................... 154 Iterative Processing........................................................................................................... 155 Creating an Endless Loop........................................................................................ 156 Iterating a Set Number of Times........................................................................... 157 Looping While a Condition Proves true.............................................................. 157 Looping Until a Condition Proves false............................................................... 158 Controlling Loop Execution with a Counter...................................................... 159 Processing List Contents.......................................................................................... 159 Back to the Guess a Number Game............................................................................... 160 Step 1: Creating a New Project............................................................................... 160 Step 2: Creating the User Interface....................................................................... 161 Step 3: Customizing Control Properties.............................................................. 163 Step 4: Adding a Little Programming Logic....................................................... 165 Step 5: Testing the Execution of the Guess a Number Game........................ 171 Summary.............................................................................................................................. 171 Chapter 6 Working with Strings, Lists, and Records......... 173 Project Preview: Alexander’s Tall Tale......................................................................... 174 Manipulating Strings........................................................................................................ 176 Assigning Strings to Variables............................................................................... 176 Getting Information About a String.................................................................... 176 Concatenating Strings............................................................................................. 177 Controlling String Format...................................................................................... 177 Advanced Comparisons........................................................................................... 178 Working with Characters, Words, and Paragraphs......................................... 180 Defining and Accessing Lists.......................................................................................... 181 Working with Lists.................................................................................................... 181 Retrieving Elements from a List............................................................................ 182 Making Changes to Lists.......................................................................................... 183 Storing and Retrieving Data Using Records............................................................... 184 Creating and Accessing Records............................................................................ 184 Modifying Record Property Values....................................................................... 185 Working with a List of Records............................................................................. 185 Contents ix Back to Alexander’s Tall Tale.......................................................................................... 186 Step 1: Creating a New Project............................................................................... 187 Step 2: Creating the User Interface....................................................................... 187 Step 3: Customizing Control Properties.............................................................. 188 Step 4: Adding a Little Program Code.................................................................. 191 Step 5: Testing the Execution of Alexander’s Tall Tale Game....................... 195 Summary.............................................................................................................................. 195 Chapter 7 Organizing Scripts with Handlers..................... 197 Project Preview: The Rock, Paper, Scissors Game...................................................... 198 Working with Event Handlers....................................................................................... 200 Creating Custom Event Handlers......................................................................... 201 Defining and Calling Handlers.............................................................................. 201 Passing Arguments to Handlers............................................................................ 202 Returning a Result from an Event Handler....................................................... 204 Understanding Scope....................................................................................................... 205 Local Scope.................................................................................................................. 205 Global Scope............................................................................................................... 206 Back to the Rock, Paper, Scissors Game....................................................................... 208 Step 1: Creating a New Project............................................................................... 209 Step 2: Creating the User Interface....................................................................... 209 Step 3: Customizing Control Properties.............................................................. 211 Step 4: Adding a Little Programming Logic....................................................... 212 Step 5: Testing the Execution of the Rock, Paper, Scissors Game................ 220 Summary.............................................................................................................................. 221 Part III Advanced Topics.....................................................223 Chapter 8 Working with Files and Folders......................... 225 Project Preview: The Slot Machine Game.................................................................... 225 Mac and POSIX Paths........................................................................................................ 228 Full and Relative Paths..................................................................................................... 230 Establishing a File Reference.......................................................................................... 231 Working with Text Files................................................................................................... 233 Accessing Files and Folders..................................................................................... 233 Reading from and Writing to Files....................................................................... 234 Creating Small Database Files........................................................................................ 238 Creating and Saving the Database File................................................................ 238 Retrieving and Accessing Database Records...................................................... 239 Back to the Slot Machine Game..................................................................................... 242 Step 1: Creating a New Project............................................................................... 243
Description: