Exploring the new file capabilities in Adobe AIR 2

来源:互联网 发布:net.sf.json 编辑:程序博客网 时间:2024/06/05 21:23

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

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:

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

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:

原创粉丝点击