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.
-
<mx:WindowedApplication
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
layout="vertical"
-
invoke="onInvoke(event)">
-
-
...
-
-
</mx:WindowedApplication>
-
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
-
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)




10 comments on “AIR Tip 7 - Using Command Line Arguments”
01
Sweet, just what I have been looking for. Well pleased…
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
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?
04
Sweet resource. Will recommend…
05
[...] AIR Tip 7: Using Command Line Arguments (AIR 1.0) [...]
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 [...]
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?
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.
09
[...] 来源:AIR Tip 7: Using Command Line Arguments [...]
10
[...] 原文:AIR Tip 7: Using Command Line Arguments [...]
Leave a Reply