views:

78

answers:

1

Hi, I am trying to launch a python script with the NativeProcess API from Javascript. On the Adobe AIR API Reference for HTML Developers I found a good example for that task, but it does not work. I looked up tons of other examples but still can not find the answer.

Here is the example code for the html file:

<html>
   <head>
   <title>Test</title>
   <script type="text/javascript" src="AIRAliases.js"></script>
   <script type="text/javascript"> 

        var process;

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

        function setupAndLaunch()
        {     
            var nativeProcessStartupInfo = new air.NativeProcessStartupInfo();
            var file = air.File.applicationDirectory.resolvePath("test.py");
            nativeProcessStartupInfo.executable = file;

            var processArgs = new air.Vector["<String>"]();
            processArgs.push("foo");
            nativeProcessStartupInfo.arguments = processArgs;

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

        function onOutputData()
        {
            air.trace("Got: ", process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable)); 
        }

        function onErrorData(event)
        {
            air.trace("ERROR -", process.standardError.readUTFBytes(process.standardError.bytesAvailable)); 
        }

        function onExit(event)
        {
            air.trace("Process exited with ", event.exitCode);
        }

        function onIOError(event)
        {
             air.trace(event.toString());
        }

   </script>
   </head>

   <body onload="launchProcess()">
   </body>
</html>

And here the code for the python file (it does not work):

#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

import sys

for word in sys.argv: #echo the command line arguments
 print word

print "HI FROM PYTHON"
print "Enter user name" 
line = sys.stdin.readline()

sys.stdout.write("hello," + line)

Running the Air App with the command adl main.xml shows in my terminal (I use OSX) only "NativeProcess supported."

Thanks for help.

And here the changes I did to the python file to get it working:

#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

import sys
import os

def convert(args):
 path = os.path.expanduser('~') + "/Desktop/"

 myFile = open(path+args, 'w')
 myFile.write('Hello World\n')
 myFile.close()

 sys.stdout.write("Python Done")

if __name__ == "__main__":
 convert(sys.argv[1])

Thanks to pyfunc...

A: 

If you run your program, then following is the output:

test.py
HI FROM PYTHON
Enter user name

Thats is this piece of python code is looking for standard input and writing to standard output. I don't think that would be possible if you do not run that from shell.

How about removing:

line = sys.stdin.readline()
sys.stdout.write("hello," + line)

and do some innocuous python expressions like

a = 1+4 

and see, if that works. I have a hunch that this might be an issue.

pyfunc
Thanks for your help. Changing the python code worked fine. For details what I finally did, look at the edited question above.
ElTorrero