如何退出 .JS 进程,使其 returns 成为 AHK 的 StdOut 值?
How to exit a .JS process so it returns a StdOut value to AHK?
我有一个由 AutoHotKey 脚本启动的 NodeJS 脚本。我需要这个 NodeJS 脚本在退出时 return 一个特定的值,所以 it can be retrieved and used by the AHK script。我可以直接在我的 AHK 脚本中获得进程 returned 的值,但它不是想要的值。
如何使我的过程 return 成为特定值?
到目前为止,我尝试使用
process.exit(myValue);
和
process.exitCode = myValue;
和
process.stdout.write(myValue)
但 none 有效。
这是我的 AHK 脚本(工作正常):
RunWait, C:\path_to_node\node.exe C:\path_to_script\index.js,,, output
MsgBox, %output%
您的输出只是进程 ID (PID)。您需要 运行 WSH 包装器中的 .js,并且您的 .js 需要 return StdOut,如下所示:
这是 AHK 的“秘方”RunWaitStdOut:
MsgBox % RunWaitStdOut("C:\path_to_script\index.js")
RunWaitStdOut(command)
{
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /c node " command)
return exec.StdOut.ReadAll()
}
与此同时,您的 .js 的末尾应该有类似于以下生成 StdOut 的内容:
process.stdout.write("The Result this javascript returns to AHK is " + myValue);
记住,StdOut 是一个字符串,所以如果你的 myValue
是一个数字或类似的,你可能需要 toString()
方法(正如 OP 注意到的,根据 OP 的评论):
process.stdout.write( myValue.toString() );
Hth,
来自docs:
RunWait sets ErrorLevel
to the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the word ERROR
is stored.
因此您可以通过 ErrorLevel
:
获取退出代码
RunWait, C:\path_to_node\node.exe C:\path_to_script\index.js
output := ErrorLevel
MsgBox, %output%
请注意,退出代码是一个 32 位带符号整数,如果 node
遇到错误,则设置为 1。如果您需要正确的输出,请使用标准输出,如 .
我有一个由 AutoHotKey 脚本启动的 NodeJS 脚本。我需要这个 NodeJS 脚本在退出时 return 一个特定的值,所以 it can be retrieved and used by the AHK script。我可以直接在我的 AHK 脚本中获得进程 returned 的值,但它不是想要的值。
如何使我的过程 return 成为特定值?
到目前为止,我尝试使用
process.exit(myValue);
和
process.exitCode = myValue;
和
process.stdout.write(myValue)
但 none 有效。
这是我的 AHK 脚本(工作正常):
RunWait, C:\path_to_node\node.exe C:\path_to_script\index.js,,, output
MsgBox, %output%
您的输出只是进程 ID (PID)。您需要 运行 WSH 包装器中的 .js,并且您的 .js 需要 return StdOut,如下所示:
这是 AHK 的“秘方”RunWaitStdOut:
MsgBox % RunWaitStdOut("C:\path_to_script\index.js")
RunWaitStdOut(command)
{
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /c node " command)
return exec.StdOut.ReadAll()
}
与此同时,您的 .js 的末尾应该有类似于以下生成 StdOut 的内容:
process.stdout.write("The Result this javascript returns to AHK is " + myValue);
记住,StdOut 是一个字符串,所以如果你的 myValue
是一个数字或类似的,你可能需要 toString()
方法(正如 OP 注意到的,根据 OP 的评论):
process.stdout.write( myValue.toString() );
Hth,
来自docs:
RunWait sets
ErrorLevel
to the program's exit code (a signed 32-bit integer). If UseErrorLevel is in effect and the launch failed, the wordERROR
is stored.
因此您可以通过 ErrorLevel
:
RunWait, C:\path_to_node\node.exe C:\path_to_script\index.js
output := ErrorLevel
MsgBox, %output%
请注意,退出代码是一个 32 位带符号整数,如果 node
遇到错误,则设置为 1。如果您需要正确的输出,请使用标准输出,如