|
阅读:2705回复:9
请问firefox执行应用程序后如何获取应用的输出?
执行应用后,怎么获取应用的输出呢(不是exitValue)?
exec: function (bin, args, func) { try { var file=Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); var process = Components.classes["@mozilla.org/process/util;1"] .createInstance(Components.interfaces.nsIProcess); file.initWithPath(bin); process.init(file); if (typeof args === "undefined") args = []; alert(bin + "" + args.join("\n")); if (typeof func === "function") process.runAsync(args, args.length, { observe: function(subject, topic, data) { // data 是null alert("xxx" + data); process = subject.QueryInterface(Components.interfaces.nsIProcess); func(topic == "process-finished", process.exitValue, data); } }); else process.run(false, args, args.length); } catch (e) { alert("exec error: " + e); } } |
|
|
1楼#
发布于:2014-07-21 15:02
|
|
|
2楼#
发布于:2014-07-21 00:02
|
|
|
3楼#
发布于:2014-07-20 22:45
这就是重定向啊
// TODO: implement 'shellredir' 从这里开始 |
|
|
|
4楼#
发布于:2014-07-20 21:18
joiky:实现没找到好办法,难道还真只有重定向?头疼啊。。回到原帖应该有其它方法的。我用 pentadactyl 可以获取到 cmd 的输出信息。 这是 pentadactyl 下的实现方法,供你参考下 // TODO: when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is
// fixed use that instead of a tmpfile
/**
* Runs *command* in a subshell and returns the output. The shell used is
* that specified by the 'shell' option.
*
* @param {string|[string]} command The command to run. This can be a shell
* command string or an array of strings (a command and arguments)
* which will be escaped and concatenated.
* @param {string} input Any input to be provided to the command on stdin.
* @param {function(object)} callback A callback to be called when
* the command completes. @optional
* @returns {object|null}
*/
system: function system(command, input, callback) {
util.dactyl.echomsg(_("io.callingShell", command), 4);
let { shellEscape } = util.closure;
return this.withTempFiles(function (stdin, stdout, cmd) {
if (input instanceof File)
stdin = input;
else if (input)
stdin.write(input);
function result(status, output) ({
__noSuchMethod__: function (meth, args) this.output[meth].apply(this.output, args),
valueOf: function () this.output,
output: output.replace(/^(.*)\n$/, "$1"),
returnValue: status,
toString: function () this.output
});
function async(status) {
let output = stdout.read();
for (let f of [stdin, stdout, cmd])
if (f.exists())
f.remove(false);
callback(result(status, output));
}
let shell = io.pathSearch(storage["options"].get("shell").value);
let shcf = storage["options"].get("shellcmdflag").value;
util.assert(shell, _("error.invalid", "'shell'"));
if (isArray(command))
command = command.map(shellEscape).join(" ");
// TODO: implement 'shellredir'
if (config.OS.isWindows && !/sh/.test(shell.leafName)) {
command = "cd /D " + this.cwd.path + " && " + command + " > " + stdout.path + " 2>&1" + " < " + stdin.path;
var res = this.run(shell, shcf.split(/\s+/).concat(command), callback ? async : true);
}
else {
cmd.write("cd " + shellEscape(this.cwd.path) + "\n" +
["exec", ">" + shellEscape(stdout.path), "2>&1", "<" + shellEscape(stdin.path),
shellEscape(shell.path), shcf, shellEscape(command)].join(" "));
res = this.run("/bin/sh", ["-e", cmd.path], callback ? async : true);
}
return callback ? true : result(res, stdout.read());
}, this, true);
},
/**
* Creates a temporary file context for executing external commands.
* *func* is called with a temp file, created with {@link #createTempFile},
* for each explicit argument. Ensures that all files are removed when
* *func* returns.
*
* @param {function} func The function to execute.
* @param {Object} self The 'this' object used when executing func.
* @returns {boolean} false if temp files couldn't be created,
* otherwise, the return value of *func*.
*/
withTempFiles: function withTempFiles(func, self, checked, ext, label) {
let args = array(util.range(0, func.length))
.map(bind("createTempFile", this, ext, label)).array;
try {
if (!args.every(util.identity))
return false;
var res = func.apply(self || this, args);
}
finally {
if (!checked || res !== true)
args.forEach(f => { f.remove(false); });
}
return res;
},
/**
* Runs an external program.
*
* @param {File|string} program The program to run.
* @param {[string]} args An array of arguments to pass to *program*.
*/
run: function run(program, args, blocking, self) {
args = args || [];
let file = this.pathSearch(program);
if (!file || !file.exists()) {
util.dactyl.echoerr(_("io.noCommand", program));
if (callable(blocking))
util.trapErrors(blocking);
return -1;
}
let process = services.Process(file.file);
process.run(false, args.map(String), args.length);
let deferred = Promise.defer();
if (callable(blocking))
// Deprecated.
deferred.promise.then(blocking);
else if (blocking) {
// Deprecated?
while (process.isRunning)
util.threadYield(false, true);
return process.exitValue;
}
let timer = services.Timer(
function () {
if (!process.isRunning) {
timer.cancel();
deferred.resolve(process.exitValue);
}
},
100, services.Timer.TYPE_REPEATING_SLACK);
return deferred.promise;
}, |
|
|
5楼#
发布于:2014-07-20 21:10
|
|
|
|
6楼#
发布于:2014-07-20 17:23
|
|
|
7楼#
发布于:2014-07-20 17:22
aaaa007cn:获取 stdout、stderr 的输出?实现没找到好办法,难道还真只有重定向?头疼啊。。 |
|
|
8楼#
发布于:2014-07-19 09:50
采用 console.log 直接输出也没找到
console.log(subject, topic, data) |
|
|
9楼#
发布于:2014-07-19 09:12
|
|
|