虽然实际上并没有按照问题的要求结婚,但评论中的@o_nix 对https://github.com/idobatter/node-win32ole提出了建议。
我建议这个模块可以满足从 Google 来到这里的人们的许多问题(就像我所做的那样)。它也可以从这里的 npm 获得:https ://www.npmjs.com/package/win32ole
该模块还有不少示例,例如:
https ://github.com/idobatter/node-win32ole/blob/dev0.1.3/examples/activex_filesystemobject_sample.js
var win32ole = require('win32ole');
. . .
var withReadFile = function(filename, callback){
var fso = new ActiveXObject('Scripting.FileSystemObject');
var fullpath = fso.GetAbsolutePathName(filename);
var file = fso.OpenTextFile(fullpath, 1, false); // open to read
try{
callback(file);
}finally{
file.Close();
}
};
var withEachLine = function(filename, callback){
withReadFile(filename, function(file){
// while(file.AtEndOfStream != true) // It works. (without unary operator !)
// while(!file.AtEndOfStream) // It does not work.
while(!file.AtEndOfStream._) // *** It works. oops!
callback(file.ReadLine());
});
};
withEachLine(testfile, function(line){
console.log(line);
});
所以,对我来说,这就像结合旧的 WSH 脚本一样好。当然会涉及到调整,但之后就是再见了 WSH。
更具体地说,对于手头的问题,这是一个演示 IE 脚本的片段:
https ://github.com/idobatter/node-win32ole/blob/master/examples/ie_sample.js
var win32ole = require('win32ole');
. . .
var ie = new ActiveXObject('InternetExplorer.Application');
ie.Visible = true;
for(var i = 0; i < uris.length; ++i){
console.log(uris[i]);
ie.Navigate(uris[i]);
win32ole.sleep(15000, true, true);
}
ie.Quit();