Teamwork: How to Make Adobe AIR Play Nice With Flash, ActionScript, and JavaScript
- Technological Background
- Using AIRAliases.js
- JavaScript Frameworks
- ActionScript Libraries
- Handling Events
- The XMLHttpRequest Object
As the simple test case in Chapter 2, "Creating an Application," shows, with Adobe AIR you can make an executable desktop application using just HTML. The problem is that with HTML alone your program won't do anything useful. To make a functional application, you'll need to use additional technologies, starting with JavaScript.
Although JavaScript can produce some nice results, there's no reason to stop there. It's easy enough to use ActionScript and Flash, even if you're not terribly familiar with either. An introduction to these programs plus how to tie them into an HTML application is what you'll find in this chapter. You'll also learn the fundamentals of the XMLHttpRequest object, a standard JavaScript tool for communicating between any two pages. Before getting to all of that, this chapter provides some general technical knowledge and background. Taken all together, the material in this chapter goes through every basic concept and code snippet that you'll want to use in your Adobe AIR applications.
Technological Background
Adobe AIR provides the power to create desktop applications using just HTML, CSS, and JavaScript, but there are several other technologies and concepts involved. Because this book assumes very little about what you might already know, I'll briefly introduce each.
WebKit
The first technology involved is WebKit (www.webkit.org). AIR uses WebKit as its HTML rendering engine, which is to say that WebKit translates HTML code into the result you see in a Web browser (for example, turning <b>word</b> into word). WebKit also handles the JavaScript, Document Object Model (DOM), and Cascading Style Sheets (CSS).
The use of a single rendering engine is one of Adobe AIR's best features. Every Web browser uses one rendering engine or another, but not often the same one. This is why different browsers give slightly or even drastically different results with the same HTML, CSS, or JavaScript. If you're a seasoned Web developer, you've probably become used to adding extra bits of code, formatting, or hacks to make your site behave properly in all browsers. Thankfully, the AIR applications you write should look and function essentially the same on different computers (thanks to WebKit).
WebKit is also the engine of choice for Apple's Safari Web browser (in fact, for Apple's operating system as a whole). An HTML page that looks and functions like you want it to in Safari will reliably look and function the same as an AIR application. (Even though Safari is made by Apple, it also now runs on Windows.)
JavaScript
The second technology involved in making AIR applications is JavaScript. Certainly you've heard of and have, I hope, dabbled with JavaScript already. HTML, by definition, dictates the appearance of a Web page or AIR application. JavaScript adds the functionality (so-called client-side functionality, which takes place within the browser or application; server-side functionality uses PHP, ASP.NET, and other technologies).
This book does assume that you have some comfort with JavaScript. You don't need to be able to program JavaScript like a pro, but understanding how to write and call functions, declare and use variables, and so forth will make this book's instructions more accessible. I'm conversant with several languages and can honestly say that JavaScript is really easy to work with, particularly once you've done away with the browser-specific issues.
Object-oriented programming
Object-oriented programming (OOP) is a serious subject that requires years of experience and learning to master. This book doesn't teach OOP, but that's not a problem, because you can easily use OOP without any mastery at all. The premise of OOP is as follows.
You first define a class, which is a blueprint that maps out the variables and functions required to work with a certain thing. For example, a blueprint of a rectangle would have variables that store its length and width. It would also have functions that calculate its perimeter and its area. Confusing matters just a little bit, the variables in a class are normally called attributes or properties, and the functions are normally called methods.
To use a class, you can create a variable of that class type using the new keyword:
var r = new Rectangle();
Now the r variable is an object of type Rectangle (r is called an instance of Rectangle). To access the object's attributes and methods, use the dot syntax:
r.length = 20; var a = r.getArea(); // Call the method.
That's the basics of creating and using objects! Defining the classes is really the hard part, and in this book that'll always already be done for you (creating your own JavaScript classes is something you can do, it just won't be necessary for any of the examples in this book). But there will be some exceptions to how you use objects.
First, you won't always create an object initially. Some class functions can be used without creating an object, and some objects are created automatically for you. For example, a Web browser, or AIR HTML page, starts with a document object.
Second, the dot syntax can often be chained together, saving you steps by making the code a bit more complicated. For example, take this line of JavaScript that changes an item's CSS class:
document.getElementById('thing').className = 'newClassName';
This code starts by calling the getElementById() method of the document object. That method returns an element in the page that matches the given ID (thing in this example). Then the className attribute of the thing element is assigned a new value (of newClassName). This is just a shortcut way of writing:
var thing = document.getElementById('thing'); thing.className = 'newClassName';
I introduce all of this because JavaScript, among others, is an object-oriented language. Familiarity with these terms and the syntax will help minimize confusion as you start to work with JavaScript code (the Document Object Model also uses objects, as its name implies).
APIs
An API is an application programming interface, which is to say it's an easy way for one technology to interact with another technology. For example, Google provides several different APIs for interacting with its many services. With respect to AIR applications, two important tools are the AIR API and the Flash API. Both provide access to features not normally associated with browser-based JavaScript. The most important of these are
- File system access
- Working with sounds and images
- Support for native windows (which are different than standard JavaScript-created windows)
- Working with the computer's clipboard
- Interacting with databases
The AIR and Flash APIs are accessible in JavaScript through window.runtime (by comparison, lots of standard JavaScript starts with document, as in the previous code). For example, to access the computer's file system, you would start by creating a new object of the File type:
var fp = new window.runtime.flash.filesystem.File();
That line represents a JavaScript call to Flash functionality.
In your Adobe AIR applications you'll frequently go back and forth between conventional JavaScript and using the AIR and Flash APIs. Often, you won't need to think about the distinction at all, but there will be times that understanding that you're using the AIR or Flash API will be relevant.
Security model
Of the many new concepts you'll need to learn to fully adapt your existing Web development knowledge to creating desktop applications, none is more important than security. The Web browser has its own security model: Web pages are quite limited in what they can do with respect to the user's computer. Since AIR applications behave like standard programs, the rules are significantly different.
The AIR security model uses the concept of sandboxes: the realm in which an application can "play," which is to say where it can read data from or write data to. AIR builds on the Flash security model, which defines two sandboxes: one for the local filesystem (i.e., the user's computer) and another for remote computers (i.e., the Internet, other networked computers, etc.). AIR adds to these a third sandbox—application—which refers to the files and content that reside in the application's folder.
For example, the user installs an AIR application. When that program runs, it loads the main HTML page. This is the application sandbox. If that program retrieves content from the user's computer (i.e., from another directory), that's the local sandbox. If the program loads content from a Web site, that's the remote sandbox. The distinctions are important because they affect what a program can do with the content, as well as what security measures need to be taken. This topic is thoroughly covered in Chapter 15, "Security Techniques."
Universal Resource Identifiers
One of the most basic terms any Web developer knows is URL, which stands for Uniform Resource Locator (it used to mean Universal Resource Locator). If you want someone to access your site, you provide them with a URL, such as http://www.example.com. The http:// part of the URL is the protocol; common alternatives are https:// and ftp://. The www.example.com is the address; although, an IP address can also be used.
Naturally, your Adobe AIR applications will use URLs, but not every resource in an AIR application will be found online. Along with http://, https://, and ftp://, AIR supports:
- file://
- app:/
- app-storage:/
(There's also a plan to support feed:// and mailto:// in future versions of AIR.) Taken together, these are all Universal Resource Identifiers (URIs).
As you may already know, file:// is supported by the Web browser, too, as a way to open local or networked documents. But the other two listed URIs are new to Adobe AIR. The first, app:/ (notice there's only one slash), always refers to the directory where the AIR application is installed: the application's root. If you create an application whose main file is called index.html, no matter what operating system that application is installed on or where the user chose to install it, app:/index.html points to that main page. The second new URI, app-storage:/ (again, one slash), refers to the application's storage directory, which will be a folder on the user's computer, different than the application's root directory.
From a security perspective, content within app:/ has full privileges. In other words, this content can do the most harm to the user's computer! Any content an application loads from app-storage:/, http://, and the others, is more limited as to what it can do.