Node.js 列出使用NodeJS运行的应用程序
使用NodeJS,我想获得Windows上打开的应用程序列表 大致如下:Node.js 列出使用NodeJS运行的应用程序,node.js,Node.js,使用NodeJS,我想获得Windows上打开的应用程序列表 大致如下: exec("tasklist", function (error, stdout, stderr) { for(var i=0;i<stdout.length;i++) { if( stdout[i]['name'].indexOf('ll_') > -1 ) { appList.push({'id':stdout[i]['id'],'n
exec("tasklist", function (error, stdout, stderr) {
for(var i=0;i<stdout.length;i++)
{
if( stdout[i]['name'].indexOf('ll_') > -1 )
{
appList.push({'id':stdout[i]['id'],'name':stdout[i]['name']});
}
}
});
exec(“任务列表”,函数(错误、stdout、stderr){
对于(变量i=0;i-1)
{
push({'id':stdout[i]['id'],'name':stdout[i]['name']});
}
}
});
其中,appList
是运行应用程序的对象,如果应用程序的ID和名称以ll
开头
我怎样才能做到这一点呢?因为stdout只是一个大字符串,它需要解析 一种方法是:
var exec = require('child_process').exec;
exec('tasklist', function(error, stdout, stderr) {
var lines = stdout.trim().split("\n"); //split by line
var processes = lines.slice(2); //remove the table headers
var parsed = processes.map(function(process) {
return process.match(/(.+?)[\s]+?(\d+)/); //match the process name and ID
});
var filtered = parsed.filter(function(process) {
return /^ll_/.test(process[1]); //filter out process names starting with ll_
});
console.log(filtered);
});
(我不运行Windows,因此以下内容未经测试)
首先,安装:
然后,使用以下脚本:
var tasklist = require('tasklist');
tasklist(function(err, tasks) {
if (err) throw err; // TODO: proper error handling
var appList = tasks.filter(function(task) {
return task.imageName.indexOf('ll_') === 0;
}).map(function(task) {
return {
id : task.pid, // XXX: is that the same as your `id`?
name : task.imageName,
};
});
});
谢谢,你的回答很有帮助。但是,为了将来参考,似乎任务列表模块已经更改,需要与承诺一起使用,而不是回调。因此调用它将是
tasklist()代码>
var tasklist = require('tasklist');
tasklist(function(err, tasks) {
if (err) throw err; // TODO: proper error handling
var appList = tasks.filter(function(task) {
return task.imageName.indexOf('ll_') === 0;
}).map(function(task) {
return {
id : task.pid, // XXX: is that the same as your `id`?
name : task.imageName,
};
});
});