In case you are wondering: “Can I pass parameters to an Adobe Air application at startup?” the answer is … YES. I didn’t know and I didn’t expect to ask myself a question like that. But it happened. For an interanl project there was a requirement to startup an application via shell and pass some parameter as input. A query to google with these keywords “adobe air passing shell parameters” does not return relevant information. So I decided to write a blog post.
The WindowedApplication class has an interesting event called InvokeEvent. As you can imagine it is fired when the application is invoked. This is the right place to detect whether there are parameters to be considered before drawing the main window. Let’s see some code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    invoke="handleOnInvoke(event)">
    <mx:Script>
    <![CDATA[
	private function handleOnInvoke(event:InvokeEvent):void {
	    trace("arguments: "+ event.arguments.toString());
	    feedbackText.text = "arguments: " + event.arguments.toString();
	}
    ]]>
    </mx:Script>
 
    <mx:Text id="feedbackText"/>
 
</mx:WindowedApplication>>

At line 3 we bind the event to a function, handleOnInvoke. The InvokeEvent handled by the function has some interesting properties attached. One is the arguments array. If you simply debug the application, the array is empty. To test it you have two ways:

  • specify parameters in the Flex builder
  • create an application and run it from the shell

Specify parameters in the Flex builder

Runner and debugger can handle parameters that are passed to the application. Follow this procedure.

  1. Right click on the project’s folder and select ‘Properties’
  2. On the left select ‘Run/Debug settings’
  3. Select the current launch configuration (there should be one) and click edit
  4. In the field ‘Command line arguments’ specify your parameters (e.g. testParameter) and hit Apply (see figure below)

Specify startup parameters in Flex Builder.

If you now debug the application you’ll see the trace message in the console tab, as in the figure, and the text field will display parameters as well.

Message in the Flex Builder Console

Create and run an application

Another way to see the parameters is to export a .air file and install it. In this case you have to run it from the shell (Terminal for MacOsX users, command line for Windowsians). Once you installed the air application move to the installation directory and try to run it with some parameters, as in the figure below.

Example run in the shell.

The InvokeEvent contains interesting data about the application. For example you can easily find out which is the local directory where the application is running.

    event.currentDirectory.nativePath;

The source code for this post is available here under the New BSD license.

Similar Posts

  • Share/Bookmark

Comments

  1. Very nice, and informative. I was wondering, in your example, how can the AIR app return something back to the command line? if there is an “invoke” command, can it respond back to the command line with say…….an array/string/number?? is this possible?

    February 20, 2010
    - Tomo
  2. You might want to have a look at Adobe Air 2.0 (still in beta) which introduced native processes api.

    February 20, 2010
  3. Thank you very much for this very easy and well written article! It’s exactly what I needed!

    March 4, 2010
  4. Leave a Reply