Accessing command line arguments in AIR!

A question came up over on the Apollo Coders group about how to access command line arguments in an AIR application. I know that there are other explanations + documentation out there on this topic, but I thought I'd regurgitate what I have learned.

In Flex Bulder create an AIR application. I named mine "CommandLine", and the CommandLine.mxml looked like this:


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
</mx:WindowedApplication>

It's an empty app that does nothing, as expected when it is first created. This application will do the brilliant act of displaying the command line arguments using an alert. So, let's add a script block and import the alert.


<mx:Script>
<![CDATA[
import mx.controls.Alert;
                
]]>

</mx:Script>

Arguments are attached the invokeEvent ckass. So, at the top level (WindowedApplication), let's listen to the invoke event:


<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" invoke="onInvoke(event)">

And we'll need to write an onInvoke function inside our script block:


private function onInvoke(event:InvokeEvent):void{
Alert.show(event.arguments.toString());
}

This function is named onInvoke. It accepts the InvokeEvent as an argument. The InvokeEvent has a property named arguments. Arguments is an array. This method converts it to a string and displays it.

Run the app and you'll see an empty alert box. Let's add some arguments. In Flex Builder, if you select the play drop down and go to "run...". The menu will look something like this:

And it will bring up this configuration screen:

You'll notice the config screen has a place for command line arguments. In this situation I typed test=test, test2=test1 . So far as Flex is concerned, Command line arguments are separated by a space; so I get two command line arguments: "test=test," and "test2=test1".

A few things that you might want to be aware of:

  • The invoke event is fired every time the application is launched, either by double clicking a shortcut or by double clicking the .exe file (or whatever your preferred method of choice is for launching an application). It does not matter if the application is already loaded, the invoke event will be re-run. You may want to write logic to prevent the app from initializing the command line arguments twice.
  • I'm not sure how to add command line arguments to the AIR Application shortcuts as part of the AIR installation process. Does anyone know? In my situation, the AIR app is being launched by another application, so that application handles creating and passing the command line arguments.
  • In it's current form, there is no concept of named arguments. So, the arguments in my above example are "Test=test". I do not have an argument named "test" with a value "test". I bet you could write a parser using regex and a Dictionary to generate your named arguments. Maybe I'll experiment with this at some future time.

The link I saved to the original thread is here although it does not appear to be working at the time of this writing. Maybe I got the link wrong. I currently have 11 things on my list of "writing ideas" and sometimes it takes a while for this stuff to peculate.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
David Tucker's Gravatar Great post. I also wrote about Command Line arguments in AIR:

http://www.davidtucker.net/2008/01/23/air-tip-7-us...

Thanks for always posting great stuff!
# Posted By David Tucker | 3/20/08 11:09 AM
Jeffry Houser's Gravatar David,

Thanks for reading. ;)
# Posted By Jeffry Houser | 3/20/08 11:46 AM
Phil Heinz's Gravatar The docs say that the InvokeEvent.arguments is an array, but your example, which I would consider trying to pass 2 arguments (test and test2), reports only one entry in the array. In my experimentation, I found that separating the command line arguments with a space instead of a comma creates the desired element separation in the InvokeEvent.arguments array.

You can then get the parts out - I'm going to put them in a hash table (_argHolder:Object) - like this:
   private function onInvoke(ev:InvokeEvent):void {
      for each (var arg:String in ev.arguments) {
         var nm:String = arg.substr(0,arg.indexOf('='));
         var val:String = arg.substr(arg.indexOf("=")+1);
         _argHolder[nm] = val;
      }
   }

Or you could use a regex search. Either way, probably best to have your arguments in separate array elements. It just seems right since they made it an array. Anyway, hope this help....

Best, Phil
# Posted By Phil Heinz | 2/19/09 11:07 PM
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.