This page is an archive of my old blog. Please visit DavidTucker.net for my current blog.
This site is no longer being maintained and commenting is disabled.

AIR Tip 6 – Launching an Application from the Browser

AIR Tip 6: This tutorial will walk you through the process of launching an AIR application from the browser and passing arguments into it. This tutorial is current for Adobe AIR Beta 3.

AIR Beta 3 introduced the browser API. This API allows for AIR application to be launched from a web browser, and it also allows for arguments to be passed into the application from the browser.

Setting up Your AIR Application

Applications must set the allowBrowserInvocation value to true to be able to receive events from the Browser API (as illustrated in Code Example 1). Once this value is set, your AIR application will receive a BrowserInvokeEvent [Reference] when it is invoked via the Browser API.

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 1 - Application Descriptor File with Browser Invocation

Knowing the API

The Browser API contains a method, launchApplication, that actually invokes your AIR application [more information]. This application can take three arguments (two of which are required).

  1. appID - the application ID of your AIR application
  2. pubID - the publisher ID of your AIR application
  3. arguments - an array of arguments to pass to your AIR application

Getting Your Publisher ID

The publisherID [Reference] will be the same for each application that is created with a given certificate. This ID can be obtained in two ways:

  • In each AIR application the publisherID is shown with the property NativeApplication.nativeApplication.publisherID
  • When an AIR application is installed, the publisherID is saved in the file META-INF/AIR/publisherid.

Creating a Movie to Launch an AIR Application

For a good starting place, you can view the sample install badge that comes with the Adobe AIR SDK. The source for the badge can be found in the src/badge folder. This sample already has the code needed to load the Browser API. This can be seen in Code Example 2.

ACTIONSCRIPT:
  1. ...
  2. _loader = new Loader();
  3. var loaderContext:LoaderContext = new LoaderContext();
  4. loaderContext.applicationDomain = ApplicationDomain.currentDomain;
  5. _loader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
  6.  
  7. try {
  8.     _loader.load(new URLRequest(BROWSERAPI_URL_BASE + "/air.swf"), loaderContext);
  9. } catch (e:Error) {
  10.     root.statusMessage.text = e.message;
  11. }
  12. ...

Code Example 2 - Loading the Browser API (from the Adobe AIR SDK)

The Adobe AIR SDK also contains the code needed to detect if AIR has been installed. We can modify this to display a status message in our custom badge.

 

ACTIONSCRIPT:
  1. private function onInit(e:Event):void {
  2.     _air = e.target.content;
  3.     switch (_air.getStatus()) {
  4.         case "installed" :
  5.             statusMessage.text = "AIR is installed and has been detected."
  6.             stage.addEventListener(MouseEvent.CLICK,onButtonClicked);
  7.             break;
  8.         case "available" :
  9.             // AIR is Available
  10.             statusMessage.text = "AIR is not installed - application cannot be launched."
  11.             break;
  12.         case "unavailable" :
  13.             // AIR Not Available
  14.             statusMessage.text = "AIR is not installed - application cannot be launched."
  15.             break;
  16.     }
  17. }

Code Example 3 - Detecting AIR (Modified from the Adobe AIR SDK)

You could easily extend this example to also install AIR if it isn't installed (as well as your application), but for this tutorial, we will just launch an application that is already installed. Code Example 4 illustrates how to use the installApplication method to actually launch the application.

ACTIONSCRIPT:
  1. private function onButtonClicked(e:Event):void {
  2.        
  3.     statusMessage.text = "Attempting to Launch AIR Application";
  4.     _air.launchApplication(_applicationID,_publisherID,_arguments);
  5.  
  6. }

Code Example 4 - Detecting AIR (Modified from the Adobe AIR SDK)

Inserting the Launch Badge Into a Page

Code Example 5 shows how to insert this badge into a page using SWFObject. All of the values that are passed to the browser API are passed into the badge with the flashvars that are created with the JavaScript SWFObject method so.addVariable.

For this example, I have passed the applicationID and publisherID into the movie with flashvars, but there might be situations where you don't want to expose all of this information. In those cases, you can hard code these values into your movie.

JAVASCRIPT:
  1. var so = new SWFObject("launchMovie.swf", "badge", "250", "75", "9.0.115", "#FFFFFF");
  2. so.addVariable( "applicationID", "net.davidtucker.airtips.BrowserAPITest" );
  3. so.addVariable( "publisherID", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.1" );
  4. so.addVariable( "arguments", "Application Launched from Browser,Argument 2" );
  5. so.write("foo_flashcontent");

Code Example 5 - Inserting the Launch Badge Into a Page with SWFObject

Testing the Example

For the sample to work properly, you will need to have the BrowserInvokeEvent Sample AIR Application from AIR Tip 5. You can install this application here.

Whoa. You need to install Adobe Flash Player.

Exercise Files
Download (15 kb)

Reference
Getting the application and publisher identifiers (Developer Guide)
Checking from a web page if an AIR application is installed (Developer Guide)
Launching an installed AIR application from the browser (Developer Guide)
Loading the air.swf file (Developer Guide)




17 Responses to “AIR Tip 6 – Launching an Application from the Browser”

  1. [...] Deploying Adobe AIR applications seamlessly with the badge install feature Launching an Application from the Browser [...]

  2. [...] SOAP Webservice (AIR Beta 1) AIR Tip 5: Passing Arguments to an Application 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 [...]

  3. [...] 来源:AIR Tip 6: Launching an Application from the Browser [...]

  4. [...] Launching an Aplication from the Browser [...]

  5. Kostas says:

    Hi,

    I have followed all examples and managed to open my AIR app by clicking on a flash applet in the browser.
    I have also managed to call a function of a flash applet, by clicking on another flash applet.
    However I was not able to call a function of my AIR app by clicking on a flash applet. Is this even possible?

    I am using the following code in the AIR app:

    var lc:LocalConnection = new flash.net.LocalConnection();
    lc.connect(“myTestConnection”);

    and this in the flash applet:

    var conn:LocalConnection = new flash.net.LocalConnection();
    conn.send(“myTestConnection”, “myTestFunction”);

    the same (AIR) code seems to work when in a second applet but not for the AIR desktop application…

  6. Hi,
    Thanks for the gr8 tutorial, Can you guide me in executing local application (say windows notepad) from adobe AIR application.
    I am not finding any source code / help for this on internet.

  7. Michl says:

    Hi, I read your book as well as the online articles here but I haven’t found a solution for the following problem:

    I want to invoke an Air app automatically from the Browser after the user successfully logged in to a specific site.

    It looks like executing the .launchApplication() function checks back if it’s triggered from a click event and I don’t know how to cheat that one yet.

    So, any ideas on that one?

    Thanks a lot!
    - Michl

  8. Joe Napoli says:

    David,

    We are trying to figure out where to get the Publisher ID for AIR help files and cannot. Adobe says they don’t support the product. Can you please tell me how to get this ID? I’m not sure what you mean by the NativeApplication.nativeApplication.publisherID.

    Thanks and regards,

    J. Napoli

  9. Bob says:

    Hi David,

    I am new to AIR. I tried to open a installed AIR app from browser by following the tip but no luck. what I did is as following.

    1. Installed Tip11-1.air downloaded from your tip 11.

    2. Unzip airtip6 and webshare the example folder.

    3. Changed the applicationID and publisherID in the index.html to point to Tip11.
    so.addVariable( “applicationID”, “net.davidtucker.airtips.LaunchApplication1″ );
    so.addVariable( “publisherID”, “C3AD24548343F7569498274306102AB328526006.1″ );

    4. Browse to index.html file, I can see the flash detected air installed, but when I double click it, it stucked at “Attempting to Launch AIR Application”.

    Also I tried to install the tip6 sample app, but I got “Invalid argument: IP 203.99.129.1″

    Could you please help out to see if I misunderstood or missed something? thank you very much in advance.

    Bob

  10. David Tucker says:

    @Bob – after I wrote this article there was a change to the API. The Browser API only allows alpha-numeric characters. If you were to format the IP in a different manner (without the periods) – the error should go away.

  11. Bob says:

    Hi David,

    What IP are you talking about? I just point the applicationID and publishID to the a installed AIR app, I didn’t specify any IP address.

    thanks.

  12. Bob says:

    Hi David,

    Is it possible for browser to find out if a certain AIR application is running or not?

    thanks

  13. Jonathan Blackburn says:

    Hi David,

    I have an AIR app installing from the browser. However, when the user clicks the launch button in my swf (which is a home-spun badge essentially), I get a dialogue that says “Open”, “Save” and “Cancel”. I want to get rid of the “Save” button as the direct download approach doesn’t accept custom params… or so it seems.

    Any tips you might have would be welcome.

    thanks.

  14. CK says:

    Hi David,

    Can I use this way to install/launch native installer application (instead of air)? My application is using the NativeProcess class. When I tried to put appurl as EXE, I got “The application could not be installed because the installer file is damaged.” error.

    Thanks.

  15. Web Developer says:

    Hi David

    I am trying to use this on AIR 2.0. I couldnt get this to work correctly. I think there is no requirement of a publisher ID, etc on AIR 2.0 and this is causing issues.

    Thanks!

  16. Ain says:

    All Developer Guide links under References are broken.