Flex Pasta » Command Line Execution in Air 2.0
Command Line Execution in Air 2.0
Air 2.0 comes with command line integration(Cheer!) among other features. This means I can execute commands if I choose from directly within an air app. Here is a simple AIR app having an input field and directly feeds the command line. The results are then output in an mx:List control.
To get this running, Download Flash Builder 4. Create an air app in Flex 4 and paste the following code into windowed app:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:WindowedApplication xmlns:fx=”http://ns.adobe.com/mxml/2009″
xmlns:s=”library://ns.adobe.com/flex/spark” title=”Command Line Exe”
xmlns:mx=”library://ns.adobe.com/flex/halo” height=”480″ width=”1024″ layout=”vertical”
creationComplete=”cc()”>
<fx:Script>
<![CDATA[
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.NativeProcessExitEvent;
import flash.events.ProgressEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ScrollEvent;
private var np:NativeProcess;
private function cc():void
{
np = new NativeProcess();
np.addEventListener(NativeProcessExitEvent.EXIT, done);
np.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutput);
np.addEventListener(ProgressEvent.STANDARD_INPUT_PROGRESS, inputProgressListener);
np.addEventListener(Event.STANDARD_ERROR_CLOSE, error);
var info:NativeProcessStartupInfo = new NativeProcessStartupInfo();
info.executable = new File(”C:\\WINDOWS\\system32\\cmd.exe”);
var args:Vector.<String> = new Vector.<String>();
np.start(info);
}
public function inputProgressListener(event:ProgressEvent):void
{
}
private function doCommand(s:String):void
{
if(s.charAt(0)==”^”)
{
np.standardInput.writeUTFBytes(s);
}
else
{
t.text = “”;
np.standardInput.writeUTFBytes(s + “\n”);
}
//np.exit();
}
[Bindable] private var lines:ArrayCollection = new ArrayCollection();
private function done(event:NativeProcessExitEvent):void
{
}
private function error(event:Event):void
{
trace(’error’);
}
private function onOutput(event:ProgressEvent):void
{
lines.addAll(new ArrayCollection(np.standardOutput.readUTFBytes(np.standardOutput.bytesAvailable).split(’\n’)));
}
]]>
</fx:Script>
<mx:HBox width=”100%”>
<s:TextInput id=”t” enter=”doCommand(t.text)”/>
<s:Button click=”doCommand(t.text)” label=”Run”/>
</mx:HBox>
<mx:List width=”100%” height=”100%” dataProvider=”{lines}”
verticalScrollPosition=”{l.maxVerticalScrollPosition}” id=”l”
rowHeight=”20″ selectable=”false”>
</mx:List>
</mx:WindowedApplication>
I had a slight problem after downloading Flash Builder 4. I guess the latest AIR code did not make it into the beta release I downloaded. The airglobal.swc was different(older) that the one you can download directly from the AIR 2.0 site. You’ll know if you have the wrong download if you get compile errors in the NativeProcess/NativeProcessExitEvent classes. Hopefully this problem is corrected soon with the Flash Builder release you download. If not you will have to do some hacking. You will need to copy the downloaded air libs folder from the AIR 2.0 download into your air/libs folder within your Flex 4 install with flash builder. This is the only way I could get it working. Even the latest nightly download of the Flex SDK didn’t contain the lastest airglobal.swc.
Now, who wants to make a slick maven air app?

3 Comments
1. Fedlarm replies at 10th December 2009, 2:24 pm :
Just a small note to others trying to install AIR 2.0. You can get a lot of info on how to install AIR 2 sdk in the release notes here. Two thing you should note if try to run the above code after installing the sdk:
In the air app.xml file remember to set the beta namespace in the XML to:
And further also remember to insert this tag in the app.xml file:
extendedDesktop
If you don’t set the supperted profiles tag you will get an Error thrown that says:
Hope this can help others getting to run your app without to much research.
And hey, pretty cool demo you put together!
2. Daniel replies at 17th December 2009, 12:39 pm :
Note that if you try to run a .bat file with some parameters, you’ll still get this message.
3. susrut316 replies at 18th December 2009, 2:12 pm :
Pretty Cool Man!! Just for clarification we need to add this to make it work extendedDesktop in the app.xml. Thanks
Leave a comment