1

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

4

2 回答 2

3

这是 Adob​​e 网站上的代码,用于完全按照您的要求进行操作:

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并且它可以工作。

更多信息在这里

于 2013-05-15T02:51:39.373 回答
0

除了 Baris Usaki 的回答之外,我想补充的两个非常重要的点是:

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

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

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

于 2018-06-22T14:51:02.910 回答