AIR Tip 5 - Passing Arguments to an Application on Install

January 10th, 2008

AIR Tip 5: This tutorial will walk you through the process of passing arguments to an AIR application when it is installed using the AIR installer badge. It includes both an article as well as a demonstration video. This tutorial is current for Adobe AIR Beta 3.

Someone left a comment today asking if it was possible to send arguments to an application when it is installed via the AIR Installer Badge. The answer: yes and no. First, it cannot be done through the default badge that comes with the SDK. However, the badge can easily be modified to perform this. Second, for an AIR application to receive the BrowserInvokeEvent [Reference], the allowBrowserInvocation property in the application descriptor file must be set to true.

Modifying the AIR Install Badge

Assuming this is true, you should go ahead and create a modified AIR Install Badge. The source code for the badge comes with the Adobe AIR Beta 3 SDK. It can be found in the src/badge folder. You will need to edit the AIRBadge.as file in three places.

  1. You will need to add a property to this class called _arguments. The properties are at the bottom of this class, so add your property declaration after the others. This property will be an array.
    ACTIONSCRIPT:
    1. ...
    2. private var _messageColor: String;
    3. private var _buttonColor: String;
    4. private var _imageURL: String;
    5. private var _appURL: String;
    6. private var _appName: String;
    7. private var _airVersion: String;
    8. private var _arguments: Array;
    9. ...

    Code Example 1 - Creating the _arguments Property

     

  2. Next, you will need to assign the arguments passed into the SWF file to the _arguments property. This is done in the constructor. You will need to take this String [Reference] that will be passed in and split it into an Array [Reference]. You can accomplish this with the split method of the String class. In this case, you can use the comma as a delimiter for the Array.
    ACTIONSCRIPT:
    1. ...
    2. _imageURL = validateURL(parameters["imageurl"]);
    3. _airVersion = String(parameters["airversion"]);
    4. _appURL = validateURL(parameters["appurl"]);
    5. _arguments = String(parameters["arguments"]).split(",");
    6. ...

    Code Example 2 - Populating the _arguments Property with an Array of Arguments

     

  3. Finally, you will need to pass the _arguments property as the third argument in the _air.installApplication method. This method is called twice in the switch statement that is in the onButtonClicked method. You will need to update both locations.
    ACTIONSCRIPT:
    1. ...
    2. _air.installApplication( _appURL, _airVersion, _arguments );
    3. ...

    Code Example 3 - Passing the _arguments Array to the AIR Installer

With these changes, you can now pass an arguments parameter into the badge installer, and it will pass those arguments to your AIR application on installation. As an example of this, I have created an AIR application that will display all of the arguments passed to it as a list. It also displays the other properties of the BrowserInvokeEvent.

For the installation, I modified my InstallBadge.js file to also include a property for the arguments (and also to pass those arguments in with the flashvars). For this example I have created a ColdFusion file which passed the IP Address and Server Name into the application as arguments.

The sample AIR application lists all properties of the BrowserInvokeEvent as well as a list of all of the arguments passed into it. You can see an example below (with the IP address and Server Name blurred).

Inserting the Modified Badge into a Site

I recently wrote an article for the Adobe AIR Develop Center entitled Deploying Adobe AIR applications seamlessly with the badge install feature. This article covers the entire process of inserting the badge onto a page. I have only modified these files slightly for this example (to add the arguments feature).

First, I have modified the InstallBadge.js file, and I have included the new version with the exercise file for this tutorial. Second, I added a new global variable (airApplicationArguments) to the head of the HTML page. This is a comma delimited list of arguments to pass to

JAVASCRIPT:
  1. // AIR Application Arguments
  2. var airApplicationArguments = "First Argument,Second Argument";

Code Example 3 - Additional Global JavaScript Variable

Setting Up Your AIR Application

There are two settings that need to be considered when setting up your AIR application to receive BrowserInvokeEvents. First, you will need to modify your application descriptor file to allow your application to receive these events. Be sure that the allowBrowserInvocation option in your application descriptor file is set to true.

XML:
  1. <!-- Whether the application can be launched when the user clicks a link in a web browser.
  2. Optional. Default false. -->
  3. <allowBrowserInvocation>true</allowBrowserInvocation>

Code Example 4 - Allowing Browser Invocation for an AIR Application

Second, you will tell your application to listen for these events. This should be added as follows:

ACTIONSCRIPT:
  1. import flash.events.BrowserInvokeEvent;
  2. private function init():void {
  3.     NativeApplication.nativeApplication.addEventListener(BrowserInvokeEvent.BROWSER_INVOKE,onBrowserInvoke);
  4. }
  5. private function onBrowserInvoke(e:BrowserInvokeEvent):void {
  6.     // Code to React to BrowserInvokeEvent
  7. }

Code Example 4 - Adding Event Listener for BrowserInvokeEvents

Sample Application

The sample AIR application for this tutorial (as seen in the video) is available. It displays all of the information sent to it in a BrowserInvokeEvent. This application also has "View Source" enabled, so you can right-click it and view the actual code.

Video (Click to View)

Exercise Files
Install AIR Application - Click Here
Exercise Files (636 kb)

Reference
Livedocs - BrowserInvokeEvent
Developer Center - Deploying Adobe AIR applications seamlessly with the badge install feature
Developer Guide - Launching an Installed AIR Application from the Browser

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

10 comments on “AIR Tip 5 - Passing Arguments to an Application on Install”

  1. 01

    [...] AIR Tip 5: Passing Arguments to an Application on Install (AIR 1.0) [...]

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

    [...] Browser API that has been covered in AIR Tip 5 and AIR Tip 6 allows you not only to launch an AIR application from the browser, but also from [...]

    Jump to the top of this page
  3. 03

    [...] 来源:AIR Tip 5: Passing Arguments to an Application on Install [...]

    Jump to the top of this page
  4. 04

    I hit a bottleneck when using the flashvars arguments parameter to pass ip address from javascript.

    As per the docs , to send arguments to the swf file , one can use the arguments parameter of the flashvars variable. If there any special characters as part of the variable value % and hexadecimal value should be used.

    I have to pass for example the the following two values are parameters

    172.1.12.25:8080/test1
    172.1.13.26:8080/test2

    I have created the argument values as
    var airApplicationArguments = “172 %2E 12 %2E 25%3A 8080%2F abc,
    172 %2E 13 %2E 26%3A 8080%2F efd&
    “;

    I get a invalid argument error.

    Iam not sure what could be issue .Can u point me to the right direction. How did create the value for arguments variable ?

    Where there any encoding involved ?

    randy at April 30th, 2008 around 5:25 pm
    Jump to the top of this page
  5. 05

    @randy - Since the Browser API is used to send the arguments - the Browser API determines what values are acceptable arguments. With AIR 1.0 the browser API changed a bit - and it only allows alpha-numeric values (no symbols). I haven’t had a chance to update this tutorial yet.

    David Tucker at April 30th, 2008 around 5:29 pm
    Jump to the top of this page
  6. 06

    Yes you are right. Iam apprehensive about the Browser API , since it breaks the application from working , when it does changes like the alpha-numerics only. The best example is your tutorial , which is failing because of the changes .

    randy at May 2nd, 2008 around 3:31 am
    Jump to the top of this page
  7. 07

    Hello david,

    Does this feature work if i click “save” instead of “install” ???

    Anyways, thanks for all your tips. :)

    Rémi at May 13th, 2008 around 7:47 am
    Jump to the top of this page
  8. 08

    @Remi - I don’t believe that it would, but I haven’t tested it.

    David Tucker at May 13th, 2008 around 8:55 pm
    Jump to the top of this page
  9. 09

    So, this feature will be useless… :(

    I’ve to try. ^-^

    Rémi at May 14th, 2008 around 4:36 am
    Jump to the top of this page
  10. 10

    Do you know if there is a similar approach that can be utilized with sidecar installations on CD/DVD?

    Lisa at November 19th, 2008 around 6:59 am
    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