lundi 30 novembre 2015

How can I launch a python script using Adobe AIR's NativeProcess?

I'm having more than a little trouble running a python script from an AIR application using the NativeProcess interface. In theory, this should be quite simple. Adobe even uses this as their example in the ActionScript 3.0 documentation for NativeProcess, as follows:

var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = File.applicationDirectory.resolvePath("");
nativeProcessStartupInfo.executable = file;

They even include the contents of what might include:

# ------------------------------------------------------------------------------
# Sample Python script
# ------------------------------------------------------------------------------

import sys

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

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

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

The problem is that, as far as I can see, this simply doesn't work. I get the following error when I attempt it:

Error #3219: The NativeProcess could not be started. '%1 is not a valid Win32 application.

Presumably the latest version of AIR (19.0) doesn't allow the execution of anything without an "exe" file extension. The following code does seem to do what I want:

var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = new File("C:/Python/Python35/python.exe");
nativeProcessStartupInfo.executable = file;
nativeProcessStartupInfo.workingDirectory = File.applicationDirectory.resolvePath(".");

var processArgs:Vector.<String> = new Vector.<String>();
processArgs[0] = "";
nativeProcessStartupInfo.arguments = processArgs;

The problem here is twofold. First, you need to know the absolute path to the executable, which I can't assume. Second, the code is no longer platform independent. The file extension would be something else on Linux or Mac.

I thought I might solve the first problem by requiring a %PYTHON_PATH% environment variable and then making the executable dependent on that. However, I can't figure out a way to use an environment variable within the ActionScript File object. It "helpfully" escapes all the "%" characters before ever sending something to the command line.

At this point this fairly simple problem has turned into a showstopper. Could someone help me understand a way to either:

  1. Execute something with the "py" extension with NativeProcess
  2. Successfully resolve a path that depends on an environment variable in the File object?

Aucun commentaire:

Enregistrer un commentaire