Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe AIR

Teamwork: How to Make Adobe AIR Play Nice With Flash, ActionScript, and JavaScript

Wanna make cool apps with AIR? It helps if you know a little something about JavaScript, Flash, and ActionScript too. Author Larry Ullman makes introductions to all of these key apps in this sample chapter from his latest book.
This chapter is from the book

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.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020