views:

300

answers:

1

I have an existing AIR app whose main content is App.swf. I would like to have another AIR app that hosts and runs App.swf. When I say run it, I mean displays it's WindowedApplication.

Here's the code for the 2 AIR projects (imports are omitted for brevity):

// App AIR Project -> App.mxml -> App.swf (it's just a window)
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
<mx:Script>
<![CDATA[
public function doSomething():void {
    trace("doSomething called");
}
]]>
</mx:Script>
</mx:WindowedApplication>

// AirAppHostApplication AIR Project -> AirAppHostApplication.mxml -> AirAppHostApplication.swf
<?xml version="1.0" encoding="utf-8"?>
<custom:AirAppHostApplication xmlns:custom="components.*" />

// components/AirAppHostApplication.as
public class AirAppHostApplication extends WindowedApplication
{   
    private var ldr:Loader;

    public function AirAppHostApplication()
    {
        addEventListener (FlexEvent.CREATION_COMPLETE, handleComplete);
    }

    private function handleComplete( event : FlexEvent ) : void
    {
        loadSwf("App.swf");
    }

    private function loadSwf(swf:String):void {
        ldr = new Loader();
        var req:URLRequest = new URLRequest(swf);
        var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
        ldr.load(req, ldrContext);
    }

    private function completeHandler(event:Event):void {
        var appSystemManagerCls:* = ApplicationDomain.currentDomain.getDefinition("_app_mx_managers_SystemManager") as Class;
        var appSystemManagerInstance:* = new appSystemManagerCls(Application.application);
        var appInstance:WindowedApplication = appSystemManagerInstance.create();
        appInstance.activate();
        appInstance.doSomething();
    }   
}

I get the following error while App.swf is being loaded:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.managers::SystemManager/initHandler()[C:\autobuild\galaga\frameworks\projects\framework\src\mx\managers\SystemManager.as:3001]

I believe the issue has to do with AirAppHostApplication's SystemManager conflicting with App's SystemManager because they are both living in the same app domain. Can an AIR app be written where a WindowedApplication class isn't statically defined, but loaded at runtime by loading a swf and instantiating the WindowedApplication subclass that's contained in the swf.

The reason I want to do this is for an automation scenario where I have to assume I don't have the source code for the app I'm automating, but I do have access to the names of the public classes and their public methods exposed for automation. I have complete control over the environment and don't have to deal with any constraints around that, so I can put 2 AIR apps in the same directory, etc.

Is this possible?

+2  A: 

Yes, this is possible. Take a look at http://blog.everythingflex.com/2009/06/08/open-an-air-application-from-a-2nd-air-application/

It is called Open an AIR application from a 2nd AIR application.

It says:

one thing that is required by both is that the application you are attempting to launch has the allowBrowserInvocation property within the AIR configuration file set to true and the application installed on your system.

and

You must also know the application’s id and publisher’s id. For example within my LauncherSample, the application id defined in the AIR configuration file is:

plus tells how to do this.

Here is Adobe's description for the error.

Error 1009 Cannot access a property or method of a null object reference.

An object that evaluates to null can have no properties. This error can occur in some unexpected (though valid) situations. For example, consider the following code, which creates a Sprite object. Because this Sprite object is never added to the display list (through the addChild() method of a DisplayObjectContainer object), its stage property is set to null. Thus, the example generates this error because Sprite object's stage property cannot have any properties:

import flash.display.Sprite;
var sprite1:Sprite = new Sprite();
var q:String = sprite1.stage.quality;
Todd Moses
Thanks for your reply, but I don't want to simply launch another AIR app from an AIR app. I'd like to host an existing AIR application in another AIR app. This means that there's a single process where the code from both the hosting app and the hosted app reside. I updated the code to make this more clear.
methym