Monday, February 1, 2010

Using the Open document API in AIR 2.0

I am back as promised ppl.Within 48 hours of my first tutorial in a series of many to come.
Today we will be talking about how to launch a file with the default appliacation using AIR 2.0 SDK.
Now I am pretty sure that you guys are well aware of how rich and powerful this functionality is.For starters ,you can create a full fledged interoperable file browser.You can let your imagination go wild.;)
So lets get to the code:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
creationComplete="windowedapplication1_creationCompleteHandler(event)"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.events.Event;

import mx.events.FlexEvent;

public var file:File=new File();


protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
this.file.browseForOpen("Selec any application you want to run");
this.file.addEventListener(Event.SELECT,runFile);
}

public function runFile(event:Event):void
{
this.file.openWithDefaultApplication();
}


]]>
</fx:Script>
</s:WindowedApplication>


I am pretty sure that the code is self explanatory to you even if you know the meager basics of using Flex.For the people who aren't so used to yet the following is the description:
The function windowedapplication1_creationCompleteHandler(event:FlexEvent) is called whenever the application is started.In this function the application will open a browser window for a user to select a file.

The function runFile(event:Event) is called whenever the user selects the file from the broswer window.We instructed the application to listen for this event using the code his.file.addEventListener(Event.SELECT,runFile);
The moment the file is selected the runFile function is called in which we have called the function file.openWithDefaultApplication();
I am sure I do not need to explain what the function will do.

So try running this code on your new flash builder.

Keep Flexing!

Stay tuned for next tutorial on FilePromise objects in AIR 2.0 which allows the usage of remote files as if its on the local file system.

No comments:

Post a Comment