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 /

Exploring the new file capabilities in Adobe AIR 2

by Christian Cantrell

Christian Cantrell
  • blogs.adobe.com/cantrell

Created

17 November 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR desktop file management Flex

Requirements

Prerequisite knowledge

This article is intended for developers who are comfortable with ActionScript 3 and who have at least a basic understanding of TCP sockets.

User level

Intermediate

Required products

  • Adobe AIR

One of the biggest advantages of running an AIR desktop application over a browser-based web application is the richness of functionality that comes with installing an application on your desktop. For example, AIR applications can create notifications, change the dock or system tray icon, and, of course, access the file system.

AIR 1.0 provided a very rich set of file APIs that enabled developers to read and write from the file system; move, copy, and delete files; traverse directories; open native file picker dialog boxes; and more. AIR 1.1 added the spaceAvailable property to the File class in order to enable developers to determine how much space is available on a given volume. AIR 2 expands the file APIs even further by adding the ability to:

  • Open files with their default registered application.
  • Flag files as "downloaded" so that some operating systems will caution users before opening them for the first time.
  • Get notified of mounted and unmounted storage volumes, and query those volumes for information.

This article covers the following topics:

  • Launching a file's default application
  • Flagging a file as downloaded
  • Listening for mounted volumes and accessing volume information
  • Sample application: FileTile

Launching a file's default application

AIR 2 adds the function openWithDefaultApplication to the File class. Calling openWithDefaultApplication on a file opens whichever application the operating system associates with that file, and then activates that application. For example, if the file instance points to a Microsoft Word document, calling openWithDefaultApplication will cause Microsoft Word to open and load the referenced file, or calling openWithDefaultApplication on an image might cause that image to be opened with Photoshop or other default application for images.

You should be aware of the following caveats before using the openWithDefaultApplication function:

  • You cannot open an application without referencing a file that is associated with that application.
  • The openWithDefaultApplication function can only be called from code running inside the application sandbox. If it's called by code running outside the application sandbox, a security error is thrown.
  • The openWithDefaultApplication function does not provide any means to communicate with the launched process after it's open. If you need to launch an external process and subsequently communicate with it, see the new NativeProcess APIs in AIR 2.
  • The AIR runtime does not verify the file before it is passed to the associated application, so if the file has an incorrect extension, it's possible that the application that is launched to handle it will not be able to read it.
  • It is not possible to use the openWithDefaultApplication function to launch executables directly. In order to preserve the platform-independent nature of Adobe AIR applications, you must use the new NativeProcess APIs in AIR 2, combined with a native installer, to launch executables from AIR.
  • If you call openWithDefaultApplication on a file that does not have a default application associated with it, a runtime error is thrown. Be sure to catch this error in your code and handle it gracefully if your application allows for this scenario.
  • If you call openWithDefaultApplication on a directory, it will open in your default file explorer application (Finder, Windows Explorer, Nautilus, and so on).

The following code shows how to open a Keynote presentation on Mac OS X:

var presentation:File = File.documentsDirectory.resolvePath("Presentations/MAX_2009.key"); presentation.openWithDefaultApplication();
openWithDefaultApplication is platform-independent as well as user-independent, since it opens files with whichever application is associated with the referenced file type. For example, calling openWithDefaultApplication on an MP3 file might open iTunes on Mac OS X, Windows Media on Windows, or VLC on Linux.

Flagging a file as downloaded

AIR 2 also adds to the File class the downloaded property, which enables AIR applications to specify whether a file was downloaded over a network. On some operating systems, setting this property to true will result in the user getting a security notification the first time he or she tries to open the file. For example, when a user opens a file that was downloaded through Safari on Mac OS X, the operating system displays a dialog box that requires the user's confirmation before the file is actually opened. When saving files in an AIR application that were downloaded over a network (for example, in an FTP client), setting this property to true is considered a best practice.

Note: Files that are downloaded through the FileReference.download method are automatically marked as downloaded.

The downloaded property only has an effect on Windows XP SP2 and later (including Vista and Windows 7), and on Mac OS 10.5 (Leopard) and later. Currently, Linux does not support this property; therefore, setting it in an application running on Linux will have no effect.

The following code shows setting the downloaded property to true on a file that was downloaded from the network and saved locally:

var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, function(e:Event):void { var air:File = File.userDirectory.resolvePath("Downloads/AdobeAIR.dmg"); var fs:FileStream = new FileStream(); fs.open(air, FileMode.WRITE); fs.writeBytes(loader.data); fs.close(); air.downloaded = true; }); var req:URLRequest = new URLRequest("http://airdownload.adobe.com/air/mac/download/latest/AdobeAIR.dmg"); loader.load(req);

Listening for mounted volumes and accessing volume information

The final addition to the file system APIs in AIR 2 provides the ability to listen for the mounting and unmounting of storage volumes, and to query storage volumes for information. For example, if your application needs to know when a USB flash drive has been plugged in, a network drive has been mounted or a digital camera has been connected, you can listen for the StorageVolumeChangeEvent and have your application react accordingly.

To be notified of the mounting and unmounting of storage volumes, add an event listener to the StorageVolumeInfo singleton instance like this:

StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount); StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);

The StorageVolumeChangeEvent contains a reference to the StorageVolume that was just mounted (the storageVolume property is null if the StorageVolumeChangeEvent was fired in response to a volume being unmounted). The StorageVolume class provides several properties containing information on the storage volume such as:

  • fileSystemType
  • isRemovable
  • isWritable
  • drive
  • name
  • rootDirectory

Note: The drive property refers to the drive letter on Windows, and is null on Mac. The name property refers to the volume name on both Mac and Windows.

You don't always have to listen for the StorageVolumeChangeEvent to get information about mounted storage volumes; the getStorageVolumes function on the StorageVolumeInfo class returns a vector of mounted storage volumes. The following code demonstrates getting a list of all mounted storage volumes:

var volumes:Vector. = StorageVolumeInfo.storageVolumeInfo.getStorageVolumes(); for each (var volume:StorageVolume in volumes) { trace(volume.name, volume.rootDirectory.nativePath); }

FileTile: The new AIR 2 file system APIs in action

FileTile, shown in Figure 1, is a sample application I wrote to demonstrate the new AIR 2 file system APIs. It's a utility for browsing storage volumes like flash drives and digital cameras which expose themselves as storage volumes. You can install FileTile from Adobe Labs, or download the full source code from Google Code.

The FileTile application
Figure 1. The FileTile application

Where to go from here

This article covered all the new file capabilities in the AIR 2. For more information, refer to:

  • Demonstration of the new storage volume APIs in AIR 2
  • FileTile on Google Code
  • FileTile sample application in the Adobe AIR Developer Center

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Using the Adobe AIR 2 NativeProcess API to create a screen recorder
  • Customer story: AIR - NASDAQ Market Replay
  • Using the Salesbuilder Adobe AIR sample application
  • Creating a socket server in Adobe AIR 2
  • User experience considerations with SQLite operations
  • Reducing CPU usage in Adobe AIR
  • Building multilingual Flex applications on Adobe AIR
  • Resolving DNS records in Adobe AIR 2
  • Using drag-and-drop support of remote files in Adobe AIR 2
  • Retrieving a list of network interfaces in Adobe AIR 2

Tutorials & Samples

Tutorials

  • Retrieving a list of network interfaces in Adobe AIR 2
  • Writing multiscreen AIR apps
  • Using the AIR 2 NativeProcess API to create a screen recorder
  • Resolving DNS records in Adobe AIR 2

Samples

  • Using the Salesbuilder Adobe AIR sample application

Adobe AIR Forums

More
04/11/2012 Surround sound 5.1 with Air 3.2 on desktop app?
12/12/2011 Live Streaming H.264 Video on iOS using AIR
04/17/2012 HTMLLoader - Google Maps?
04/12/2012 Tabindex in forms on mobile?

Adobe AIR Blog

More
07/09/2012 Protected: Publishing Adobe AIR 3.0 for TV on Reference Devices
07/08/2012 Source Code: Adobe AIR 3.3 Retina Video Application
07/06/2012 Application specific File Storage on Adobe AIR based ios Application
07/04/2012 Recent Work - iPad/Android App: Inside My toyota

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