Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center / AIR Quick Starts for HTML/JavaScript developers /

AIR application start-up options

by Jeff Swartz

Jeff Swartz  Adobe

Modified

10 June 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Adobe AIR

Requirements

Prerequisite knowledge

General experience building HTML-based applications is suggested. For more details on getting started with this Quick Start, refer to Building the Quick Start sample applications with HTML.

User level

Intermediate

Required products

  • Adobe AIR

Sample files

  • StartupOptionsHTML.zip
  • StartupOptionsHTML.air

The StartupOptions sample application, shown in Figure 1, demonstrates the following options on starting up an AIR application:

  • How to register an application to be the default application for a file type
  • How to set an application to start up automatically when the user logs in to the operating system
  • What to do if the application was started up by the user double-clicking a document associated with the application
Startup Options application
Figure 1. This sample application shows how to use various application start-up options.

Note: This is a sample application provided, as is, for instructional purposes.

Testing the application

To test this application, follow these steps:

  1. Install the application from the AIR file. (Testing the application in ADL will limit some functionality.)
  2. Run the application.

    Each time you invoke the application, the InvokeEvent log provides details of the InvokeEvent event (described later in this article).

  3. If the Register application to open foo files check box is not selected, select it.

    This application uses the "foo" file extension as a registered file type.

  4. Select the Start at login check box.
  5. Type some text in the File contents text box.
  6. Click the Save document button, select a file name and location for the document, and click the Save button.

    This saves a file of type "foo."

  7. Close the application.
  8. Double-click the document you saved in step 6.
  9. Close the application and log off of the operating system.
  10. Log into the operating system.

    The application starts up at system login.

Understanding the code

Note: For more information about using AIR classes, see the Adobe AIR API Reference for HTML Developers. For information on using the AIR HTML Localizer Framework, see the "Localizing AIR applications" chapter in Building Adobe AIR Applications.

Registering a file type for the application

The application descriptor file of the application, application.xml, includes a fileType element. This element defines options for a file type that can be associated with the application:

<fileType> <name>AIR.sample.file</name> <extension>foo</extension> <description>AIR start-up sample application file</description> <contentType>text/plain</contentType> <icon> <image16x16>icons/TestDocument_16.png</image16x16> <image32x32>icons/TestDocument_32.png</image32x32> <image48x48>icons/TestDocument_48.png</image48x48> <image128x128>icons/TestDocument_128.png</image128x128> </icon> </fileType>

This lets the application register as the default application to open files with the "foo" file extension. The operating system uses the description text to describe "foo" files (if the AIR application is set as the default application). The contentType element defines the MIME type for the file. (In this application, the file is a plain text file.) The icon element defines PNG files to use as the icon for "foo" files. (These PNG files are included in the source directory, and they must be packaged in the AIR file.)

The user interface of the application includes a Register application to open foo files check box. The init() function of the JavaScript in the index.html file checks to see if the application is already registered as the default application for the foo extension. If it is, it selects and disables this check box:

if (air.NativeApplication.nativeApplication.isSetAsDefaultApplication("foo")) { cbFileReg.checked = true; }

If the application is not the default handler for foo files, the setFileRegistration() function sets (or removes) the application from being the foo file handler, in response to the user clicking the Register application to open "foo" files check box:

if (cbFileReg.checked) { air.NativeApplication.nativeApplication.setAsDefaultApplication("foo"); } else { air.NativeApplication.nativeApplication.removeAsDefaultApplication("foo"); }

When the user installs the AIR application, if there is no other file registered to handle files with the "foo" exension, the AIR application becomes the default "foo" file handler automatically.

Setting the application to launch when the user logs into the operating system

The setStartup() function is a handler for the change event of the Start at login (cbStartAtLogin) check box. It sets the startAtLogin property of the NativeApplication object, based on the check box setting.

air.NativeApplication.nativeApplication.startAtLogin = cbStartAtLogin.checked;

When the startAtLogin property is set to true, the application launches automatically when the user logs into the operating system. It is a good practice to provide the user with a option to decide whether to have your application start at login (if the application has this feature).

Saving a custom file type

The foo file format is really just a text file that uses the "foo" file extension. The newFile() function lets the user select a path for the file:

function newFile() { file.browseForSave("Save a document."); file.addEventListener(air.Event.SELECT, saveFile); }

Once the user selects the file path, the saveFile() function saves the file to the chosen location. The file data is written as a UTF-encoded text file. The file content is based on the text in the fileContents textarea object.

function saveFile(event) { if (file.extension != "foo") { file = new air.File(file.nativePath + ".foo"); } var stream = new air.FileStream(); stream.open(file, air.FileMode.WRITE); stream.writeUTFBytes(fileContents.value); stream.close(); currentFile.value = file.nativePath; }

Determing how the application was invoked

The init() function sets an event handler for InvokeEvent events:

air.NativeApplication.nativeApplication.addEventListener(air.InvokeEvent.INVOKE, invokeHandler);

The InvokeEvent handler function (invokeHandler()) writes information on the application invocation, based on the properties of the InvokeEvent object.

log.value += new Date().toTimeString() + ": InvokeEvent.reason == " + event.reason + ""; log.value += " InvokeEvent.arguments.length == " + event.arguments.length + "\n"; for (i = 0; i < event.arguments.length; i++) { log.value += " InvokeEvent.arguments[" + i + "] == " + event.arguments[i] + "\n"; } if (event.arguments.length > 0) { openFile(event.arguments[0]); }

Data is displayed in the log textarea object. If the application is launched by the user double-clicking the AIR application icon, the InvokeEvent log displays the following information:

InvokeEvent.reason == standard InvokeEvent.arguments.length == 0
If the application is launched automatically when the user logs into the operating system, the InvokeEvent log displays the following information:
InvokeEvent.reason == login InvokeEvent.arguments.length == 0
If the application is launched when the user double-clicks a foo file (a file with the "foo" file extension), the InvokeEvent log displays the following information:
InvokeEvent.reason == standard InvokeEvent.arguments.length == 1 InvokeEvent.arguments[0] == [file path]

In this final case, where the application was invoked by the user double-clicking a file, the operating system passes the file path as an argument to the AIR application. The first element of the NativeApplication object's arguments array is the path to the file. The openFile() function uses this information to open the file and display its contents in the fileContents object:

currentFile.value = path; file = new air.File(path); var stream = new air.FileStream(); stream.open(file, air.FileMode.READ); fileContents.innerHTML = stream.readUTFBytes(stream.bytesAvailable); stream.close();

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement