最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Batch file called from JavascriptXPCOM doesn't show command prompt window - Stack Overflow

programmeradmin4浏览0评论

I am calling a batch file from Javascript in this fashion:

function runBatch(){
    var exe = Components.classes['@mozilla/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
    exe.initWithPath("C:\\test.bat");
    var run = Components.classes['@mozilla/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 mand 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 mand 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/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
exe.initWithPath("C:\\WINDOWS\\system32\\cmd.exe");
var run = Components.classes['@mozilla/process/util;1'].createInstance(Components.interfaces.nsIProcess);
run.init(exe);
run.run(false, null,0);
}

I am calling a batch file from Javascript in this fashion:

function runBatch(){
    var exe = Components.classes['@mozilla/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
    exe.initWithPath("C:\\test.bat");
    var run = Components.classes['@mozilla/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 mand 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 mand 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/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
exe.initWithPath("C:\\WINDOWS\\system32\\cmd.exe");
var run = Components.classes['@mozilla/process/util;1'].createInstance(Components.interfaces.nsIProcess);
run.init(exe);
run.run(false, null,0);
}
Share Improve this question edited Nov 22, 2008 at 20:25 pc1oad1etter asked Nov 22, 2008 at 7:16 pc1oad1etterpc1oad1etter 8,61710 gold badges50 silver badges64 bronze badges 1
  • FWIW, your first snippet works fine for me on Vista and Firefox 3.6. – Nickolay Commented May 4, 2010 at 21:16
Add a ment  | 

7 Answers 7

Reset to default 2

The only solution I've heard so far (that should work, although I haven't done it yet, es 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.

Did you try using the launch method of nsiLocalFile?

function runBatch(){
    var exe = Components.classes['@mozilla/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."

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/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/process/util;1"].createInstance(Components.interfaces.nsIProcess);
process.init(obj_Program);
var args = ["/C", "regedit.exe"];
process.run(true, args, args.length);

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/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 mand line window

You are doing right but repair this:

function runBatch(){
    var exe = Components.classes['@mozilla/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
    exe.initWithPath("***C:\ \test.bat***");
    var run = Components.classes['@mozilla/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/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
    exe.initWithPath("***C:\test.bat***");
    var run = Components.classes['@mozilla/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

Pfft, very ugly code.. A much nicer trick is to use Win. to spawn a 16bit subsystem of the mand prompt. Win. will send the console to the right virtual terminal, showing you the output.

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

Nicer, and works :)

For Linux:

<script>

    function callLight2()
    {

        netscape.security.PrivilegeManager.enablePrivilege(
        'UniversalXPConnect'
    );
        var exe = Components.classes['@mozilla/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/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>
发布评论

评论列表(0)

  1. 暂无评论