Spawning Processes in ActionScript

Here’s the translation of the Go code to ActionScript, formatted in Markdown suitable for Hugo:

Sometimes our ActionScript programs need to spawn other, non-ActionScript processes.

package {
    import flash.desktop.NativeProcess;
    import flash.desktop.NativeProcessStartupInfo;
    import flash.events.NativeProcessExitEvent;
    import flash.events.ProgressEvent;
    import flash.filesystem.File;

    public class SpawningProcesses {
        public function SpawningProcesses() {
            // We'll start with a simple command that takes no
            // arguments or input and just prints something to
            // stdout. The NativeProcessStartupInfo class is used to
            // represent this external process.
            var dateInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
            dateInfo.executable = new File("C:\\Windows\\System32\\cmd.exe");
            dateInfo.arguments = new Vector.<String>("/c", "date /t");

            var dateProcess:NativeProcess = new NativeProcess();
            dateProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onDateOutput);
            dateProcess.start(dateInfo);

            // The NativeProcess class doesn't have a direct equivalent
            // to Go's Output() method. Instead, we listen for the output
            // event and handle it in a separate function.

            function onDateOutput(event:ProgressEvent):void {
                var dateOut:String = dateProcess.standardOutput.readUTFBytes(dateProcess.standardOutput.bytesAvailable);
                trace("> date");
                trace(dateOut);
            }

            // Error handling in ActionScript is typically done with
            // try-catch blocks and custom error events.

            try {
                var invalidInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                invalidInfo.executable = new File("C:\\InvalidPath\\invalidcommand.exe");
                var invalidProcess:NativeProcess = new NativeProcess();
                invalidProcess.start(invalidInfo);
            } catch (error:Error) {
                trace("failed executing:", error.message);
            }

            // For piping data to a process, we can use the standardInput
            // property of NativeProcess.

            var grepInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
            grepInfo.executable = new File("C:\\Windows\\System32\\findstr.exe");
            grepInfo.arguments = new Vector.<String>("hello");

            var grepProcess:NativeProcess = new NativeProcess();
            grepProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onGrepOutput);
            grepProcess.start(grepInfo);

            grepProcess.standardInput.writeUTFBytes("hello grep\ngoodbye grep");
            grepProcess.standardInput.close();

            function onGrepOutput(event:ProgressEvent):void {
                var grepOut:String = grepProcess.standardOutput.readUTFBytes(grepProcess.standardOutput.bytesAvailable);
                trace("> grep hello");
                trace(grepOut);
            }

            // To run a full command with arguments, we can use cmd.exe with /c option

            var lsInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
            lsInfo.executable = new File("C:\\Windows\\System32\\cmd.exe");
            lsInfo.arguments = new Vector.<String>("/c", "dir /a /o");

            var lsProcess:NativeProcess = new NativeProcess();
            lsProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onLsOutput);
            lsProcess.start(lsInfo);

            function onLsOutput(event:ProgressEvent):void {
                var lsOut:String = lsProcess.standardOutput.readUTFBytes(lsProcess.standardOutput.bytesAvailable);
                trace("> dir /a /o");
                trace(lsOut);
            }
        }
    }
}

This ActionScript code demonstrates how to spawn external processes, similar to the original example. However, there are some key differences due to the language and runtime environment:

  1. ActionScript uses the NativeProcess class to spawn external processes, which is only available in Adobe AIR applications.

  2. Instead of synchronous methods like Output(), ActionScript uses event listeners to handle process output asynchronously.

  3. Error handling is typically done with try-catch blocks and custom error events, rather than returning error objects.

  4. The equivalent of grep on Windows (which is more commonly used for ActionScript development) is findstr, so we use that in the example.

  5. To run a full command with arguments, we use cmd.exe with the /c option, similar to using bash -c in the original example.

Note that this code assumes a Windows environment, as ActionScript is more commonly used in Windows. For cross-platform compatibility, you’d need to check the operating system and use appropriate commands and file paths.

The spawned programs should return output similar to running them directly from the command line, but the exact output will depend on the system and environment where the code is run.