Avg. Rating 4.5
Tags:



Problem

Similar to the operating system opening a file using its default application, AIR 2.0 provides the ability for applications to launch native processes and interact with them. The following classes add support for these capabilities. Need a useful example that shows that functionality

Solution

The application below opens up a text file: foobar.txt using the MacOS TextEdit text editor.

Detailed explanation

Similar to the operating system opening a file using its default application, AIR 2.0 provides the ability for applications to launch native processes and interact with them. The following classes add support for these capabilities.

  • flash.desktop.NativeProcess  - provides command line integration and general launching capabilities on the host OS. Once a process is launched the AIR application can monitor the standard input, output and error of the process.
  • flash.desktop.NativeProcessStartupInfo  - provides basic information used to start a process on the host OS.

flash.events.NativeProcessExitEvent - event dispatched once a process exits. It is possible that this event will never be dispatched if the child process outlives the AIR application.

Launching and interacting with native processes will only be available for applications that are installed using a native OS installer.
Create a new application and name it NativeProcessExample.
You need to make sure you have enabled the extended desktop in your descriptor. Open NativeProcessExample-app.xml and add the following tag:

<supportedProfiles>extendedDesktop</supportedProfiles>


The application below opens up a text file: foobar.txt using the MacOS TextEdit text editor.

<?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/mx">
    <fx:Script>
        <![CDATA[
            import flash.events.NativeProcessExitEvent;

            public function executeNativeProcess():void
            {
                var executable:File = new
File("/Applications/TextEdit.app/Contents/MacOS/TextEdit");
                var workingDirectory:File = new File("/");
              
                var nativeProcess:NativeProcess = new
NativeProcess();
              
                if (NativeProcess.isSupported)
                {
                    trace("Native Process Supported");
                }
              
                var
nativeProcessStartupInfo:NativeProcessStartupInfo = new
NativeProcessStartupInfo();
                nativeProcessStartupInfo.executable = executable;
                nativeProcessStartupInfo.workingDirectory =
workingDirectory;
              
                var args:Vector.<String> = new
Vector.<String>(); 
                args.push("/Users/Elad/Desktop/foobar.txt"); //
open file that was given with the executable application
                nativeProcessStartupInfo.arguments = args;
              
                nativeProcess.addEventListener(
NativeProcessExitEvent.EXIT, onExitError );
              
                try {
                    nativeProcess.start(nativeProcessStartupInfo);
                } catch (error:IllegalOperationError) {
                    trace("Illegal Operation: "+error.toString());
                } catch (error:ArgumentError) {
                    trace("Argument Error: "+error.toString());
                } catch (error:Error) {
                    trace ("Error: "+error.toString());
                }
              
                if (nativeProcess.running)
                {
                    trace ("Native Process Support");
                }     
            }
          
            public function
onExitError(event:NativeProcessExitEvent):void
            {
                trace( "Native Process Exit code: "+event.exitCode
);
            }          
          
        ]]>
    </fx:Script>
  
    <s:Button id="button"
              label="Open File foobar.txt with text editor"
              click="executeNativeProcess();"
              width="250" />
  
</s:WindowedApplication>

To learn more about this feature and more order
AdvancED
Flex 4


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes