Reputation: 8617
I am calling a batch file from Javascript in this fashion:
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 is:
echo on
echo %1
pause
exit
Each time I call a batch file, however, the command prompt is not displayed, as it would be if I simply ran the batch file from the desktop. How can I remedy this and display a command prompt for the batch file?
Edit To be clear, the cmd.exe process is launched - I can see it in the task bar. But no window gets displayed. This snippet 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);
}
Upvotes: 4
Views: 6170
Reputation: 9
You are doing right but repair 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);
}
If you do 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);
}
An put @echo off at init???
Thanks
Upvotes: 0
Reputation:
I had to launch a batch file and pass in an argument. This 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 line window
Upvotes: 0
Reputation: 1
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>
Upvotes: -1
Reputation:
Pfft, very ugly code.. A much nicer trick is to use Win.com to spawn a 16bit subsystem of the command prompt. Win.com will send the console to the right virtual terminal, showing you the output.
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);
Nicer, and works :)
Upvotes: -1
Reputation: 8617
The only solution I've heard so far (that should work, although I haven't done it yet, comes from Mook in the Mozilla xulrunner IRC channel:
create a temporary batch file, writing in the batch file to call and arguments to pass 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.
Upvotes: 2
Reputation: 7535
This code snippet seems to work fine. Of course, you have to change D:\Windows\system32\ to path to cmd.exe in your operation 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);
Upvotes: 0
Reputation: 3007
Did you try using the launch method of nsiLocalFile?
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 if you double-clicked the file."
Upvotes: 1