Tomasz Dzięcielewski
Tomasz Dzięcielewski

Reputation: 3907

How to run windows command without window in JavaScript using nsIProcess

I'd like to run some command for example:

notepad C:\file.txt

using Firefox addon and nsIProcess. I've already written code like bellow:

var exepath = "C:\Windows\System32\cmd.exe";
var args = ["/c", "notepad", "C:\file.txt"];
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(exepath);
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
process.init(file);
process.run(true, args, args.length);

But I don't want opening window. Is it possible to run some command without opening this window?

I know, that I can use cmd.exe /C start notepad to close this window after command executed, but it flashes (appears and disappears). Additionally I'd like to read exit value:

var exitval = process.exitValue;

I forgot to mention that I can't install any additional software!

Upvotes: 3

Views: 2275

Answers (2)

user3147249
user3147249

Reputation: 11

This is a old question but this page is well ranked on google results

To avoid windows appears and disappears you shold use this command

system/child_process

Upvotes: 1

Wladimir Palant
Wladimir Palant

Reputation: 57651

You can run Notepad directly - you don't need cmd.exe for that. But you have to find the full path of the executable. Its path is %windir%/notepad.exe, you can use nsIEnvironment to resolve that path (something you actually have to do with cmd.exe as well instead of assuming that Windows is installed in c:\windows):

var env = Cc["@mozilla.org/process/environment;1"]
            .getService(Ci.nsIEnvironment);
var exepath = env.get("WINDIR") + "\\notepad.exe";

And just in case you want to run something other than Notepad: you will have to do the work that the command line is normally doing, namely getting the PATH environment variable and going through the directories in the list, looking where the file exits.

Upvotes: 3

Related Questions