我正在尝试使用Javascript中的NativeProcess API启动python脚本。 在Adobe AIR API Reference for HTML Developers我发现了一个很好的例子,但它不起作用。我查了很多其他的例子,但仍然找不到答案。
以下是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>
这里是python文件的代码(它不起作用):
#! /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)
使用命令adl main.xml
运行Air App仅在我的终端(我使用OSX)中显示“NativeProcess支持。”
感谢您的帮助。
这里是我对python文件所做的更改,以使其正常工作:
#! /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 ...
答案 0 :(得分:0)
如果您运行程序,则输出如下:
test.py
HI FROM PYTHON
Enter user name
这就是这段python代码正在寻找标准输入并写入标准输出。 如果你不从shell运行它,我认为这是不可能的。
如何移除:
line = sys.stdin.readline()
sys.stdout.write("hello," + line)
并做一些无害的python表达式,如
a = 1+4
看看,如果可行的话。我预感到这可能是一个问题。