1
我试图从Javascript中使用NativeProcess API启动python脚本。 在Adobe AIR API Reference for HTML Developers上,我发现了一个很好的例子,但它不起作用。我查了许多其他的例子,但仍然找不到答案。使用Javascript的Adobe Air2 NativeProcess API
下面是HTML文件中的示例代码:
<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>
这里的蟒蛇文件中的代码(它不工作):
#! /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)
与命令运行AIR应用程序adl main.xml
仅在我的终端(我使用OSX)中显示“支持NativeProcess”。
感谢您的帮助。
在这里,我也给蟒蛇文件的变更得到它的工作:
#! /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])
由于pyfunc ...
感谢您的帮助。更改Python代码工作正常。有关我最终做的细节,请看上面编辑的问题。 – eltorrero 2010-09-04 10:19:23