Batch file invoked from Javascript / XPCOM does not display command prompt window

I call the batch file from Javascript this way:

function runBatch(){ var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); exe.initWithPath("C:\\test.bat"); var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess); run.init(exe); var parameters = ["hi"]; run.run(false, parameters,parameters.length); } 

my test batch file:

 echo on echo %1 pause exit 

However, every time I call the batch file, the batch line is not displayed, as if I was just running the batch file from the desktop. How can I fix this and display the command line for the batch file?

Edit To be clear, the cmd.exe process is running - I see it in the taskbar. But the window does not appear. This fragment behaves similarly:

 function runCmd(){ var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); exe.initWithPath("C:\\WINDOWS\\system32\\cmd.exe"); var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess); run.init(exe); run.run(false, null,0); } 
+4
source share
7 answers

The only solution I've heard so far (this should work, although I haven't done it yet, comes from Mook in Mozilla xulrunner's IRC channel:

create a temporary batch file by writing to the batch file for invocation and arguments for passing it. then execute the temporary batch file.

e.g. psuedocode:

 f = fopen("temp.bat"); fprintf(f, "other.bat 1 2 3 4 5"); fclose(f); exec("temp.bat"); 

not very elegant, but it should work.

+2
source

Have you tried using the nsiLocalFile startup method?

 function runBatch(){ var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); exe.initWithPath("C:\\test.bat"); exe.launch(); } 

This should have " the same effect as double-clicking on a file .

+1
source

This piece of code is working fine. Of course, you should change D: \ Windows \ system32 \ to the path to cmd.exe in your operating system.

 const FileFactory = new Components.Constructor("@mozilla.org/file/local;1","nsILocalFile","initWithPath"); var str_LocalProgram = "D:\\Windows\\system32\\cmd.exe"; var obj_Program = new FileFactory(str_LocalProgram); var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); process.init(obj_Program); var args = ["/C", "regedit.exe"]; process.run(true, args, args.length); 
0
source

You are doing it right, but fix this:

 function runBatch(){ var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); exe.initWithPath("***C:\ \test.bat***"); var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess); run.init(exe); var parameters = ["hi"]; run.run(false, parameters,parameters.length); } 

Should you do it?

 function runBatch(){ var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); exe.initWithPath("***C:\test.bat***"); var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess); run.init(exe); var parameters = ["hi"]; run.run(false, parameters,parameters.length); } 

Set @echo off on init

thanks

0
source

I had to run a batch file and pass an argument. Here is how I did it:

  let file = uri.QueryInterface(Components.interfaces.nsIFileURL).file; let run = Components.classes['@mozilla.org/process/util;1'] .createInstance(Components.interfaces.nsIProcess); let path = file.path; if(file.exists()) { // quick security check if(file.isExecutable()) { // show error message return; } let localfile = file.QueryInterface(Components.interfaces.nsILocalFile); if(localfile != null) { if (app == "app1") { localfile.initWithPath("C:\\app1.bat"); } else { localfile.initWithPath("C:\\app2.bat"); } run.init(localfile); var parameters = [path]; run.run(false, parameters, parameters.length); } else { // show error message } } else { // show error message } 

and in my window batch file I did:

 @ECHO OFF START "application.exe" %1 

using START, allowed me to launch the application and close the command prompt window

0
source

Pfft, very ugly code .. It is much nicer to use Win.com to create a 16-bit command line subsystem. Win.com will send the console to the right virtual terminal, showing you the result.

 var lPath = getWorkingDir.path + "\\..\\..\\WINDOWS\\system32\\win.com"; lFile.initWithPath(lPath); var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); process.init(lFile); var args = ["cmd.exe"]; process.run(false, args, args.length); 

More and it works :)

-1
source

For Linux:

 <script> function callLight2() { netscape.security.PrivilegeManager.enablePrivilege( 'UniversalXPConnect' ); var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile); // exe.initWithPath(C:\\Windows\\system32\\cmd.exe""); exe.initWithPath("/usr/bin/gnome-terminal"); var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess); run.init(exe); var parameters = ["-e", "/usr/bin/ip_connect_up.sh 2 2 3 4 5 6"]; // var parameters = ["/C", "regedit.exe"]; // var parameters = ["hi"]; run.run(true, parameters,parameters.length); } </script> <a href="#" onClick ="callLight2()">start</a> 
-1
source

All Articles