VISUAL QUICKPRO GUIDE ADOBE AIR (ADOBE INTEGRATED RUNTIME) WITH AJAX Larry Ullman Peachpit Press Visual QuickPro Guide Adobe AIR (Adobe Integrated Runtime) with Ajax Larry Ullman Peachpit Press 1249 Eighth Street Berkeley, CA 94710 510/524-2178 510/524-2221 (fax) Find us on the Web at: www.peachpit.com To report errors, please send a note to: [email protected] Peachpit Press is a division of Pearson Education. Copyright © 2008 by Larry Ullman Editor: Rebecca Gulick Copy Editor: Anne Marie Walker Production Coordinator: Myrna Vladic Compositor: Debbie Roberti Indexer: Rebecca Plunkett Cover Production: Louisa Adair Technical Reviewer: Prayank Swaroop Notice of rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact [email protected]. Notice of liability The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press 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 instructions contained in this book or by the computer software and hardware products described in it. Trademarks Visual QuickPro Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Adobe AIR and Adobe Integrated Runtime are registered trademarks of Adobe Systems, Inc., in the United States and in other countries. Macintosh and Mac OS X are registered trademarks of Apple Computer, Inc. Microsoft and Windows are registered trademarks of Microsoft Corp. Other product names used in this book may be trademarks of their own respective owners. Images of Web sites in this book are copyrighted by the original holders and are used with their kind permission. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. ISBN-13: 978-0-321-52461-4 ISBN-10: 0-321-52461-6 9 8 7 6 5 4 3 2 1 Printed and bound in the United States of America Dedication: To the Kavounis family Special thanks to: Top-notch editor and champion of books and writers, Rebecca Gulick. Anne Marie Walker, for her excellent atten- tion to detail and for improving writing that is often too vague. Debbie Roberti and Myrna Vladic, for put- ting together the puzzle that is this book out of the pieces I created. Rebecca Plunkett, for creating the index. Everyone else at Peachpit Press, naturally, for all of the work they do! Prayank Swaroop, for jumping in and provid- ing a most helpful technical review. Nicole, for helping with the kids so that I can write these books. Jessica, Zoe, and Sam, for making it so much fun not to be sitting at my computer writing and programming. Table of Contents Introduction iii Chapter 1: Running AIR Applications 1 Installing the Runtime ......................... 2 Installing an Application ....................... 4 Running an Application ........................ 7 Chapter 2: Creating an Application 9 T Installing the SDK ............................. 10 a b Updating Your Path on Windows .............. 11 l e Updating Your Path on Mac OS X .............. 13 o Creating the Project’s Structure ................ 16 f C Creating the HTML File ....................... 18 o Creating the XML File ......................... 19 n t Testing the Application ....................... 23 e n Creating a Certificate .......................... 25 t s Building the Application ...................... 27 Chapter 3: AIR Deelopment Tools 29 Using Aptana Studio .......................... 30 Using Dreamweaver ........................... 37 Creating Digital Signatures .................... 40 Chapter 4: Basic Concepts and Code 45 Technological Background .................... 46 Using AIRAliases.js ............................ 49 JavaScript Frameworks ........................ 51 ActionScript Libraries ......................... 54 Handling Events .............................. 56 The XMLHttpRequest Object .................. 59 Chapter 5: Debugging 65 Using JavaScript Dialogs ....................... 66 Using Trace ................................... 68 Using the AIR Introspector .................... 70 Other Debugging Techniques .................. 72 Contents Chapter 6: Making Windows 75 Creating a New Window ....................... 76 Creating a New Native Window ................ 80 Customizing Windows ........................ 83 Accessing a New Native Window .............. 86 Creating Full-screen Windows ................. 90 Handling Window Events ..................... 93 Creating a New Look .......................... 96 Moving and Resizing Windows ............... 100 Chapter 7: Creating Menus 103 Menu Terminology ........................... 104 Creating a Menu ............................. 106 Handling Menu Events ....................... 111 OS-specific Menus ........................... 115 Adding Keyboard Equivalents ................ 118 s t Changing a Menu Item’s State ................ 123 n e t Chapter 8: Importing and Exporting Data 127 n o Copying ..................................... 128 C f Cutting ...................................... 133 o Pasting ...................................... 137 e l Working with Different Formats .............. 141 b a Drag and Drop In ............................ 146 T Drag and Drop Out ........................... 151 Chapter 9: Files and Directories 155 Fundamentals ............................... 156 File and Directory Browsing .................. 159 Accessing File Information ................... 164 Reading Directories .......................... 168 Deleting Files and Directories ................. 172 Copying and Moving ......................... 177 Chapter 10: Working with File Content 183 Reading from Files ........................... 184 Writing to Files .............................. 189 An Asynchronous Example ................... 195 Using Binary Data ............................ 201 Chapter 11: Working with Databases 205 Connecting to a Database .................... 206 Creating a Database .......................... 209 Inserting Records ............................ 213 Handling Errors .............................. 218 Selecting Records ............................ 221 Updating and Deleting Records ............... 227 i Contents Chapter 12: Database Techniques 235 Distributing Databases ....................... 236 Using Prepared Statements ................... 240 Fetching Records in Groups .................. 245 Performing Transactions ..................... 249 Improving Performance ...................... 260 Debugging Techniques ....................... 261 Chapter 13: Networking 263 The URLRequest Class ....................... 264 Receiving Data ............................... 268 Parsing Data ................................. 271 Transmitting Data ........................... 275 Downloading Files ........................... 280 Downloading Large Files ..................... 283 Uploading Files .............................. 287 T a Chapter 14: Using Other Media 291 b l Playing Sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 e o Playing Long Sounds ......................... 294 f Playing Streaming Sounds .................... 298 C o Controlling Sounds ........................... 303 n t Displaying PDFs ............................. 308 e n Handling XML Data. . . . . . . . . . . . . . . . . . . . . . . . . . 312 t s Chapter 15: Security Techniques 319 The AIR Security Model ...................... 320 Using Non-Application Sandbox Content ..... 325 Using the Sandbox Bridge .................... 328 Storing Encrypted Data ...................... 339 Validating Data .............................. 343 Best Security Practices ....................... 344 Chapter 16: Deploying Applications 345 More Application Descriptor File Options ..... 346 Using Custom Icons .......................... 349 Seamless Installations ........................ 351 More Application Ideas ....................... 354 Updating an Application ..................... 358 Index 366 ii i Introduction Adobe AIR (Adobe Integrated Runtime) With so many ways to create desktop appli- is a fantastic new technology that greatly cations, the natural question is: Why should expands the ways in which you can cre- I use Adobe AIR? The first and most compel- ate desktop applications. Traditionally, ling reason is that using Adobe AIR is easy. In programs like Microsoft Word, the Firefox all likelihood you’ll just apply the knowledge Web browser, and Adobe Reader have been you already have. In the worst-case scenario, n written in a programming language like C or if you have limited to no experience with o i C++. Learning such technologies, while not HTML and JavaScript, rest assured that few t c hard, takes some effort, and making graphi- technologies are as approachable as these. u d cal applications, let alone cross-platform The learning curve for using Adobe AIR is o r apps, using them is an even larger hurdle. therefore short but the upside is huge. t n Now, with Adobe AIR you can use whatever I A second but very strong reason to use Adobe Web development know-how you have— AIR is that it automatically generates cross- be it Adobe Flash, Adobe Flex, or standard platform applications. The programs you cre- Ajax (HTML and JavaScript)—to create fully ate will run equally well on Windows, Mac OS functional desktop applications that will X, and Linux regardless of the operating sys- run equally well on Windows, Mac OS X, tem on which they were written. Adobe AIR and Linux. was designed specifically with this in mind, This book, which focuses solely on the Ajax and there are but few areas in which operating (HTML and JavaScript) approach, covers system-specific steps need to be taken. everything you need to know to begin creat- A third consideration to note is that with ing useful Adobe AIR applications today. Adobe AIR you’re creating graphical appli- With a minimum of technical jargon and lots cations: programs that are visible, that run of practical examples, this easy-to-follow text outside of any console window or terminal is the perfect introduction for how you can application, and that can take full advantage apply your Web development skills in new of the user’s mouse and keyboard. This may ways. Whether you’re creating programs for not sound revelatory to you, but when using your own use, developing company software, other technologies (like C or C++) to make an or repurposing a Web site to broaden its application, creating a graphical application reach, Adobe AIR is the right tool for the job. viii IInnttrroodduuccttiioonn as opposed to a command-line utility isn’t u HTML and JavaScript are understood by that simple. a larger audience. The Adobe AIR applications you develop can u Far more applications can be used to generate HTML and JavaScript code. u Access files and directories on the user’s computer There are 16 chapters in all, and they can pretty much be read in any order you like. u Integrate a client-side database I would, however, highly recommend that u Securely store data in an encrypted format you initially read the first four chapters in order. They cover the most basic informa- u Contain custom windows and menus tion, knowledge that the other chapters will u Interact with network resources, like assume you already have. Web sites and servers In keeping with the approach I take to any u Tap into the computer’s clipboard, sup- subject, I hope you’ll find that the content in porting cut, copy, paste, plus drag in and this book, while accurate and appropriate, out functionality is never too complicated or overloaded with technical jargon. Also, a real emphasis has u Play sounds and videos been placed on using practical, real-world u Display PDFs examples. With few exceptions, most of the W demonstrations portray actions that desktop h In short, an application written in AIR can a applications would actually perform. t do pretty much anything you can think of! Y o Because there are limits to what a book can If you still need convincing, consider that u discuss, not everything that’s possible in AIR ’l the tools required for creating and running l is covered here. As stated earlier, the book N AIR applications are free and supported by does not discuss how to write AIR applica- e an excellent company, Adobe. If the way e tions using Flash or Flex (I don’t think a d in which Adobe handled the invention, good book could actually cover multiple promotion, and distribution of the Portable AIR development methods). Beyond that, a Document Format (PDF) is any gauge, the small subset of topics has been omitted, for future looks bright for Adobe AIR. example, taking command-line arguments About This Book or using digital rights management (DRM) for media files. Rest assured that I only made This book covers everything you need to such omissions for subjects that the vast know to develop desktop applications using majority of readers will not need to know Adobe AIR. Although there are three primary and that are also adequately covered in the technologies that you can use with AIR— online documentation (a fact that can’t be Ajax, Flash, and Flex—this book focuses said for every topic). solely on just the Ajax (which is to say HTML What You’ll Need and JavaScript) method. I’ve chosen to only use Ajax code because: Fortunately, the requirements for developing u HTML and JavaScript are easier to learn Adobe AIR applications with Ajax are quite than Flash and Flex (in my opinion). manageable. In fact, you don’t even need to spend any money! To run an AIR application, iixx