AIR Tip 7 – Using Command Line Arguments

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)




18 Responses to “AIR Tip 7 – Using Command Line Arguments”

  1. Bubbila says:

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

  2. Kiran says:

    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

  3. David Tucker says:

    @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?

  4. Bubbila says:

    Sweet resource. Will recommend…

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

  6. [...] 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 [...]

  7. Swami Kevala says:

    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?

  8. David Tucker says:

    @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.

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

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

  11. Sasi says:

    Hi David,

    Thanks for the post. Is there any possibility to excute a CLI command from AIR. I need to do a thing like this .
    pdf2swf.exe input.pdf output.swf
    I need to pass the path of the input.pdf and excute this command.
    Also tell me Is there anyway available in AIR to convert set of PDF files to SWFs using Adobe AIR

    Thanks in advance,
    Sasi

  12. John Dimm says:

    Nice and to-the-point.

    Any tips on running the resulting app from the command line?

    I had trouble:

    john-dimms-macbook-pro:Desktop jdimm$ /Applications/json2aso.app/Contents/MacOS/json2aso foo.json
    2009-02-19 16:27:40.695 json2aso[6771:10b] NSDocumentController Info.plist warning: The values of CFBundleTypeRole entries must be ‘Editor’, ‘Viewer’, ‘None’, or ‘Shell’.

    For some reason, moving the executable out of its install directory resolved that problem:

    john-dimms-macbook-pro:Desktop jdimm$ cp /Applications/json2aso.app/Contents/MacOS/json2aso .
    john-dimms-macbook-pro:Desktop jdimm$ ./json2aso foo.json
    john-dimms-macbook-pro:Desktop jdimm$

    Now I’m getting output. However, there is an annoying message that pops up and requires a click to dismiss:

    Adobe AIR

    This installation of this application is damaged. Try re-installing or contacting the publisher for assistance.

    Any idea how to avoid that?

    Also, I was getting the error described here:

    I was getting the error mentioned here:

    http://www.justria.com/2008/11/19/if-flexbuilder-complains-about-error-creating-air-file-305-error-10-solution/

    “error creating AIR file: 305: ERROR, 10″

    So I made this change to json2aso-app.xml:

    The namespace in your example was 1.0.M6. That didn’t work for me.

    Thanks again!

  13. Online says:

    Пинайте своего хостера – сайт с трудом открылся :(

  14. ajitpal Singh says:

    Hello David.
    Actually i want to know, can we open commandprompt using air application and pass arugumets to them. Like opening cmd.exe and passing – (version) to it as argument.

  15. M says:

    @John Dimm
    The error you’re getting happens with all AIR apps unless you use the proper invocation (the same you’d use for OS X apps : open) like this:

    machine: ~user$ cd path to app bundle
    machine: ~user$ open -a app_name

    Question: how are you getting output? I would like to know if there’s anyway to print output as in to stdout? You know, like any standard command line tool?

  16. Kaushal says:

    Nice video, I am looking for solution similar to it.

    Hi David,

    I have question, my application has canvas, which allow user to put images anywhere inside it, and when user hit save button, it should pass row data of canvas to PHP, and then PHP will run AIR application over server, pass row data to it, so AIR application generate same canvas (place images at the location where user has placed) and save whole canvas as an image using ImageSnapshot.captureImage() and it has 300 dpi.

    Is it possible and if yes then can you please guide me.

    Thanks

  17. Vandana says:

    Hi David,
    Thanks for the post, great help.

    I am trying to achieve SSO with AIR. I have some kind of SSO working with Flex app.
    I am authenticating against AD with Kerberos based on SPNEGO. I have a CAS server setup on Tomcat and so the Cas login page shows up when I am authenticating for the first time.

    But I want that my AIR client should not ask the user to authenticate if the user has already logged on to his/her machine authenticating against AD.
    Any guidelines would be of great help.
    Thanks in Advance.

    Vandana

  18. Michael says:

    Yet another great developer that has abandoned his thread and left a lot of great questions unanswered.

    Here’s a biggie (already asked and unanswered): Can you pass arguments TO the command line from within the AIR application?

Leave a Reply