如何在Air app中实现NativeProcess

时间:2013-05-14 18:03:52

标签: actionscript-3 command-line air

我需要使用NativeProcess做一些非常简单的事情,我需要通过cmd行启动一个.exe文件并传递一个参数。我找到了NativeProcess示例,但它们都是针对更复杂的事情,并没有显示完整的实现。我有很多关于flash as3的经验,但不是在这个特定的领域...如果有人能告诉我这是如何从开始到结束完成的,我将非常感激。

2 个答案:

答案 0 :(得分:2)

以下是来自Adobe网站的代码,用于完全按照您的要求进行操作:

package
{
    public class Main extends Sprite
    {
        public var process:NativeProcess;

        public function Main()
        {
            if(NativeProcess.isSupported)
                setupAndLaunch();
            else
                trace("NativeProcess not supported.");
        }

        public function setupAndLaunch():void
        {     
            var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
            var file:File = File.applicationDirectory.resolvePath("yourapp.exe");
            nativeProcessStartupInfo.executable = file;

            var processArgs:Vector.<String> = new Vector.<String>();
            processArgs[0] = "the parameter you are passing";
            nativeProcessStartupInfo.arguments = processArgs;

            process = new NativeProcess();
            process.start(nativeProcessStartupInfo);
            process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
            process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, onErrorData);
            process.addEventListener(NativeProcessExitEvent.EXIT, onExit);
            process.addEventListener(IOErrorEvent.STANDARD_OUTPUT_IO_ERROR, onIOError);
            process.addEventListener(IOErrorEvent.STANDARD_ERROR_IO_ERROR, onIOError);
        }

        public function onOutputData(event:ProgressEvent):void
        {
            trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable)); 
        }

        public function onErrorData(event:ProgressEvent):void
        {
            trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable)); 
        }

        public function onExit(event:NativeProcessExitEvent):void
        {
            trace("Process exited with ", event.exitCode);
        }

        public function onIOError(event:IOErrorEvent):void
        {
             trace(event.toString());
        }
    }
}

重要信息The NativeProcess class and its capabilities are only available to AIR applications installed with a native installer (extended desktop profile applications). When debugging, you can pass the -profile extendedDesktop argument to ADL to enable the NativeProcess functionality. At runtime, you can check the NativeProcess.isSupported property to to determine whether native process communication is supported.

我在Flash Develop中通过将应用程序配置文件设置为Extended Desktop来测试上述内容并且它可以正常运行。

更多信息here

答案 1 :(得分:0)

除了Baris Usaki的回答,我还要补充两个非常重要的观点:

  1. 您必须将应用程序发布为.dmg,而不是.air。这样做会把 / Applications目录中的应用程序。

  2. 您必须仅将配置文件设置为“扩展桌面”。还添加     如果需要,则为“桌面”。否则,您将得到错误:     “不支持NativeProcess”

我能够运行/ usr / lib / osascript之类的可执行文件来运行.scpt文件。但是,我无法运行位于桌面文件夹中的任何其他.app文件。我找不到原因。