01_778257 ffirs.qxp 6/9/06 9:29 AM Page i Beginning Mac OS® X Tiger™ Dashboard Widget Development Fred Terry 01_778257 ffirs.qxp 6/9/06 9:29 AM Page i Beginning Mac OS® X Tiger™ Dashboard Widget Development Fred Terry 01_778257 ffirs.qxp 6/9/06 9:29 AM Page ii Beginning Mac OS®X Tiger™ Dashboard Widget Development Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-471-77825-7 ISBN-10: 0-471-77825-7 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/SX/QW/QW/IN Library of Congress Cataloging-in-Publication Data: Terry, Fred. Beginning Mac OS X Tiger dashboard widget development / Fred Terry. p. cm. Includes index. ISBN-13: 978-0-471-77825-7 (paper/website : alk. paper) ISBN-10: 0-471-77825-7 (paper/website : alk. paper) 1. Mac OS. 2. Operating systems (Computers) 3. Macintosh (Computer)—Programming. 4. HTML(Document markup language) I. Title. QA76.76.O63T437 2006 005.4'4682—dc22 2006015521 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESEN- TATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACYOR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLYDISCLAIM ALLWARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR APARTICULAR PURPOSE. NO WARRANTYMAYBE CREATED OR EXTENDED BYSALES OR PRO- MOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERYSITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONALSERVICES. IF PROFESSIONALASSISTANCE IS REQUIRED, THE SERVICES OF ACOMPETENT PROFESSIONALPERSON SHOULD BE SOUGHT. NEITHER THE PUB- LISHER NOR THE AUTHOR SHALLBE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGA- NIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS ACITATION AND/OR APOTENTIALSOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAYPROVIDE OR RECOMMENDATIONS IT MAYMAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAYHAVE CHANGED OR DIS- APPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks:Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trade- marks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Mac OS and Tiger are trademarks or registered trademarks of Apple Computer, Inc. in the US and other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. 01_778257 ffirs.qxp 6/9/06 9:29 AM Page iii About the Author Fred Terryhas been involved in the computer industry since making a left-hand turn into it from Ph.D. work in medieval languages and literature. He has written a number of software manuals and articles and has worked as a systems and network administrator, web developer, programmer, and quality assurance engineer. In addition to his ongoing love affairs with AppleScript and Perl, his current pro- gramming infatuations are Ruby and Ajax. Currently, Fred is a project manager for the Information Management Group at Burns & McDonnell. He has a B.A. in English from Southwestern Oklahoma State University and an M.A. in English from Oklahoma State University. He lives in Lawrence, Kansas, with his family and dog. Fred can be contacted at [email protected]. For Leesa, who has stood by me through career changes and side projects without losing her patience or humor Acknowledgments No book is produced in a vacuum. I want to thank the developers who let me include their widgets for the example chapters in the latter half of the book: Andrew Welch, Nick Rogers, Jesus de Meyer, Jason Yee, and Simon Whitaker. I can’t thank Nick Rogers enough for being a racquetball partner, letting me bounce code off of him, and performing the technical edit. Cleve Devault needs my thanks for giving me some space to work on this book. I also want to thank my agent, Laura Lewin; my acquisitions editor, Katie Mohr; and, most important of all, my development editor, Rosanne Koneval. This book wouldn’t have made it over all the hurdles without their capable guidance. My thanks to Nick Sayre and Andy Rhoades for allowing me to reprint the “Nick vs. Andy” strip (Figure 7-9). Additional “Nick vs. Andy” strips can be found at http://nicksayre.com/. As any writer would say, I have to extend my greatest thanks to my family. My wife, Leesa; my daughter, Sommer; and my son, Keegan, put up with the constant distraction of this book. They are happier than I that it’s finally done. 01_778257 ffirs.qxp 6/9/06 9:29 AM Page iv Credits Acquisitions Editor Graphics and Production Specialists Katie Mohr Claudia Bell Carrie A. Foster Development Editor Joyce Haughey Rosanne Koneval Barbara Moore Barry Offringa Technical Editor Heather Ryan Nick Rogers Alicia B.South Copy Editor Quality Control Technician Maarten Reilingh Leeann Harney Editorial Manager Project Coordinator Mary Beth Wakefield Jennifer Theriot Production Manager Proofreading and Indexing Tim Tate Techbooks Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert 02_778257 ftoc.qxp 6/9/06 9:29 AM Page v Contents Introduction xiii Part I: An Introduction to Widgets 1 Chapter 1: Tiger, Dashboard, and Widgets 3 OS X Tiger 3 Dashboard and Widgets 4 Managing Dashboard’s Widgets 8 Installing Widgets 9 Installing Local Widgets 9 Downloading and Installing 11 Reloading Widgets 13 What Widgets Are Not 13 Summary 14 Exercises 14 Chapter 2: The Basics of Widgetry 15 What a Widget Is Composed Of 15 HTML,CSS,and JavaScript 17 HTML 18 CSS 19 JavaScript 21 Property Lists 23 Widget Properties 24 Creating Property Lists 26 Icons and Images 30 How to Bundle Your Widget 31 Summary 33 Exercises 33 02_778257 ftoc.qxp 6/9/06 9:29 AM Page vi Contents Chapter 3: Widget Development Environment 35 An Editor and a Browser 35 TextEdit and Safari 36 BBEdit 38 Using an Integrated Development Environment 39 Widgetarium 40 Xcode 41 Starting Xcode 41 Building a Plugin 42 Enabling Dashboard Developer Mode 47 Debugging Tools 49 Using Safari as Your Debugger 49 Third-Party Debugging Tools 50 Summary 53 Exercises 53 Chapter 4: Creating a Widget 55 HTML Beginnings 55 Beginning the Project 55 Begin with HTML 57 Adding a Background 59 Creating Widget Structure with CSS 61 <div> Regions 63 Doing the Work with JavaScript 69 Building the Widget 72 Making Sure It Works 75 Summary 75 Exercises 76 Chapter 5: Debugging and Testing 77 First Steps 77 Debugging Tips 77 Syntax and Logic Errors 78 Widget Errors 79 Debugging Tricks 79 Use a Helpful Editor 79 Simplify 79 Log Files 80 Printing Debug Information with Alert Statements 81 vi 02_778257 ftoc.qxp 6/9/06 9:29 AM Page vii Contents Using a Debugger 83 Debugging Tools 83 Stepping Through a Widget 83 Testing Your Widget 86 Testing Tips 86 Don’t Test Your Own Stuff 86 Break Your Widget 86 Keep a Log 86 What Now? 87 Fixing Bugs 87 Summary 88 Exercises 88 Part II: Providing User Interaction 89 Chapter 6: Giving a Widget Preferences 91 Adding Preferences to Your Widget 91 How to Show Preferences 93 Creating a Back Side Panel 94 Widget Resources 95 Flipping a Widget 95 The Widget’s HTML File 95 The Widget’s CSS File 97 The Widget’s JavaScript File 99 Testing Your Changes 101 Adding Preferences 102 Adding Preference Selection 102 Saving and Reading Preferences 108 Summary 111 Exercises 111 Chapter 7: Widget Events 113 The Dashboard Activation Event 113 Activation Properties 114 System Resources 117 Removing Widget Preferences 119 Setting Widget Focus 123 Dragging a Widget 124 Control Regions 125 Summary 127 Exercises 127 vii 02_778257 ftoc.qxp 6/9/06 9:29 AM Page viii Contents Chapter 8: Adding to the Widget Interface 129 Resizing 129 When to Resize the Widget 132 How to Resize a Widget 132 The HTML File 135 The CSS File 136 The JavaScript File 140 Moving the Close Box 145 Scrolling 146 When to Add Scrolling 146 The HTML File 146 The CSS File 147 The JavaScript File 148 Summary 151 Exercises 151 Chapter 9: Adding Cut, Copy, and Paste to Your Widget 153 Pasteboard 153 Pasteboard Events 153 Pasteboard Handlers 154 Adding Pasteboard Handlers 156 Summary 157 Exercises 157 Chapter 10: Adding Drag and Drop to the Widget 159 Drag-and-Drop Events 160 Dragging and Dropping from the Finder 160 Using Drag and Drop 161 HTML 162 CSS 163 JavaScript 164 Dragging Between Widgets 166 Summary 167 Exercises 167 Chapter 11: Access Keys 169 Using Access Keys 169 File System Access 170 viii