AIR Tip 7 - Using Command Line Arguments

January 23rd, 2008

AIR Tip 7: This tutorial will walk you through the process of using command line arguments with your AIR application. This tutorial is current for AIR Beta 3.

One of the useful ways that AIR applications is to interact with the operating system, is that is can receive command line arguments. These arguments are passed to the AIR application in the form of an InvokeEvent [ActionScript | JavaScript].

Receiving an InvokeEvent

To receive an InvokeEvent, you must add an event listener to the NativeApplication instance for your application. This can be defined in MXML or Actionscript for you Flex based AIR application, Actionscript for your Flash based AIR application, and JavaScript in your HTML / JavaScript based AIR application. All three methods are displayed in Code Example 1.

 

mxml:
  1. <mx:WindowedApplication
  2.     xmlns:mx="http://www.adobe.com/2006/mxml"
  3.     layout="vertical"
  4.     invoke="onInvoke(event)">
  5.  
  6.     ...
  7.    
  8. </mx:WindowedApplication>

ACTIONSCRIPT:
  1. NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);

JAVASCRIPT:
  1. air.NativeApplication.nativeApplication.addEventListener(air.InvokeEvent.INVOKE, onInvoke);

Code Example 1 - Setting Up Your Application to Receive InvokeEvents

Handling the InvokeEvent

When you receive the InvokeEvent, the command line arguments will be passed in the property arguments as an array [ ActionScript | JavaScript ]. AIR does not distinguish between commands and arguments in the command line. If you need to include this capability, you will have to include it yourself.

Example

In today's example, you will build a sample AIR application that will respond to command line arguments. The example will be done in Flex Builder 3, but the techniques could easily be modified to work with Flash or HTML/JavaScript (if you have any specific implementation questions, please leave a comment).

Video (Click to View)

Other Uses for InvokeEvent

InvokeEvent is also used when an application is called to open a file by the operating system. The filename of the file is passed to the AIR application as an argument in an InvokeEvent. This is extremely important if you have registered your own filetype. When a user double-clicks on a file with your custom file type, the InvokeEvent will be dispatched. If you have not configured your application to listen for this event - your application will do nothing. I will cover custom file types in more detail in a future tutorial.

Application Code
Download (3 kb)
AIR Application (289 kb)

Reference
Developer Guide - Capturing Command Line Arguments (HTML / JavaScript)
Developer Guide - Capturing Command Line Arguments (Flex / ActionScript)
Developer Guide - Capturing Command Line Arguments (Flash)

AIR | Comments | Trackback | Del.icio.us | Digg | Technorati Jump to the top of this page

10 comments on “AIR Tip 7 - Using Command Line Arguments”

  1. 01

    Sweet, just what I have been looking for. Well pleased…

    Bubbila at February 11th, 2008 around 4:02 pm
    Jump to the top of this page
  2. 02

    hi David,
    You seem to be an expert with AIR. Do you know how Single Sign On/ SSL Certificates could be used with Adobe AIR?
    Thanks
    Kiran

    Kiran at February 12th, 2008 around 10:53 am
    Jump to the top of this page
  3. 03

    @Kiran - to use a Single Sign On system with an AIR application will require some server-side object to connect to. For example, if you have a ColdFusion CFC that can handle authentication - you can interact with that service via a Webservice or HTTPService call. (You can call a Webservice or HTTPService over HTTPS to handle this authentication). Does this answer your question?

    David Tucker at February 12th, 2008 around 11:06 am
    Jump to the top of this page
  4. 04

    Sweet resource. Will recommend…

    Bubbila at February 15th, 2008 around 12:35 pm
    Jump to the top of this page
  5. 05

    [...] AIR Tip 7: Using Command Line Arguments (AIR 1.0) [...]

    雨飞blog » David Tucker的air tips at March 5th, 2008 around 8:08 pm
    Jump to the top of this page
  6. 06

    [...] on Install (AIR Beta 3) AIR Tip 6: Launching an Application from the Browser (AIR Beta 3) AIR Tip 7: Using Command Line Arguments (AIR Beta 3) AIR Tip 8: Serializing Objects (AIR Beta 3) To assist you in your AIR Development, I [...]

    Jump to the top of this page
  7. 07

    Hi David,

    Great video. One thing I didn’t get though. Can you run your AIR application in batch? i.e. without having to first run the application. I just want to allow the option of using only the command line. Do you know what I mean?

    Swami Kevala at March 31st, 2008 around 11:43 pm
    Jump to the top of this page
  8. 08

    @Swami - You can run your AIR application from the command line - and if you wanted - you could configure the application to exit once it has finished processing the arguments. I am not sure if this answers your question - so let me know if you need any more direction on this.

    David Tucker at April 1st, 2008 around 2:27 am
    Jump to the top of this page
  9. 09

    [...] 来源:AIR Tip 7: Using Command Line Arguments [...]

    Jump to the top of this page
  10. 10

    [...] 原文:AIR Tip 7: Using Command Line Arguments [...]

    » AIR使用命令行 » WEB实用技术文摘 at May 12th, 2008 around 7:38 pm
    Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.

Badges

View David Tucker's profile on LinkedIn
Inside RIA Badge

Community Posts