//
you're reading...
AIR, Flex, Source Code, Technology

AIR 2.0 Native Process Batch File

I really wanted this feature almost a year back when I was working on Adobe Mobile Packager with AIR 1.0. The very reason why we didn’t build the UI with AIR was its inability to interact with Native processes. And finally now with the 2.0 release of AIR, the feature has been made available to millions of developers.

The AS3 NativeProcess Class provides us with the much needed functionality to launch external processes and interact with them using standard input or output. But I hit the road block when I had to launch a batch file. I tried to launch a batch file the same way I launched an EXE and boom it failed. The reason AIR doesn’t allow us to launch batch files directly and the work around is to launch CMD.exe with the batch file as an argument to it. After googling and binging for examples, I decided to write my own.

So here is how we can launch a batch file using AIRs Native Process API

public function launchBatchFile():void{
    var file:File = File.applicationDirectory;
    file = file.resolvePath("C:\\Windows\\System32\\cmd.exe");
    var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
    NativeProcessStartupInfo.executable = file;
    nativeProcessStartupInfo.workingDirectory = File.applicationDirectory.resolvePath(<PATH to BATCH FILE>);
    var args:Vector<String> = new Vector<String>();
    args.push(<BATCH FILE NAME>);
    args.push(<ARGUMENT TO BATCH>);  // Repeat this to pass all the arguments 
    nativeProcessStartupInfo.arguments = args;
    var process:NativeProcess = new NativeProcess();
    process.start(nativeProcessStartupInfo);
    process.addEventListener(NativeProcessExitEvent.EXIT, exitHandler);
}

protected function exitHandler(event:NativeProcessExitEvent):void{
    Alert.show("Execution Complete");
}
Advertisements

Discussion

9 thoughts on “AIR 2.0 Native Process Batch File

  1. There was an issue with the
    NativeProcessExitEvent not being called and I realized that by following this method cmd.exe does not get killed on automatically. So hence in your batch file we need explictly add the Exit command to ensure that the CMD.exe is terminated and the NativeProcessExitEvent is fired.

    Was stuck with this problem for more than half a day

    Posted by sudharshan | April 15, 2010, 1:33 pm
  2. Another important that I forgot to mention is the application.xml should contain the following tag <supportedProfiles>extendedDesktop</supportedProfiles> for it to work

    Posted by sudharshan | May 31, 2010, 11:11 am
  3. thanks!

    the line

    “nativeProcessStartupInfo.workingDirectory = File.applicationDirectory.resolvePath();”

    or in my case

    nativeProcessStartupInfo.workingDirectory = File.applicationDirectory.resolvePath(“./
    “);

    was the most important one to me in this one..

    Posted by jarno | July 21, 2010, 12:39 pm
  4. When using your example and Running my AIR app I get this error…

    ArgumentError: Error #3214: NativeProcessStartupInfo.executable does not specify a valid executable file.

    It references what would be line five in your example as the culprit. Is this a problem with my sdk for AIR 2 perhaps?

    Posted by Jon | August 2, 2010, 3:18 pm
  5. Hi, i think you should add /c for working on windows 7
    args.push(“/c” + );

    Posted by Andry | September 1, 2010, 11:14 am
  6. Hello sudharshan,
    Good post.
    some questions:
    1. Can I run the script directly instead of a cmd file.?
    2. I tried your way, somehow it did not work. I have done as mentioned in above 2 pots also.

    Best
    Deepanjan Das

    Posted by deepanjandas | September 10, 2010, 8:55 am
  7. Thank you for adding this tutorial, real help. I was wondering if you had to package this as a native installer, instead of an air app? I’m making my app on mac but the air application would be used on a pc. When i run my air application in windows it won’t trigger the batch file, and i’ve rounded it down to being that the nativeProcess ins’t supported. I added the bit but flex forced me to add mobile devices aswell. Thanks David

    Posted by David | September 23, 2010, 12:25 pm
  8. I changed the following in your code to run a batch file,
    File.applicationDirectory.resolvePath(“c:\\sampleFolder”);
    var args:Vector = new Vector();
    args.push(“abc.bat”);

    But when I run the Air app, it hangs. I go to Taskmanager and it shows me a lot of cmd.exes have started. Then I have to kill this Air App to close them all.

    Could you tell what m I missing here??

    Posted by Arham | July 1, 2011, 1:46 pm
  9. Try the following:

    The string generated by the File object by either nativePath or resolvePath uses “\” for the path. Replace “\” with “/” and see if it works.

    Posted by irongamer | July 10, 2011, 6:34 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: