Using a WScript.shell activeX to execute a command line

前端 未结 2 1231
青春惊慌失措
青春惊慌失措 2020-12-05 16:24

I am working on calling a .exe file with a WScript.shell activeX. The file is wkhtmltopdf.exe and it is used to convert a HTML page to a .pdf. Everything is working well wh

2条回答
  •  无人及你
    2020-12-05 17:16

    For anyone else that comes across this issue, I had a similar (but slightly different) problem that I thought I'd share.

    I too wanted to run a command using the ActiveXObject("WScript.shell. I needed to run a .bat script that would launch Google Chrome to a specific URL.

    The JS I had was as follows:

    var objShell = new ActiveXObject("WScript.shell");
    objShell.run('"C:\\Scripts\\MyChromeBat.bat" MY_URL');
    

    This would properly launch my .bat script which was very simple:

    start "" chrome.exe %1
    

    The issue I came across was that MY_URL contained some query parameters and when I used the above JS, the query params would be stripped to an extent. So when I was trying to open

    http://localhost:8080/webapp/mypage.html?param1=test¶m2=test2
    

    it would actually open

    http://localhost:8080/webapp/mypage.html?param1
    

    The fix turned out to be simple - I had to surround MY_URL in quotes. So I modified the line

    objShell.run('"C:\\Scripts\\MyChromeBat.bat" MY_URL');
    

    to be

    objShell.run('"C:\\Scripts\\MyChromeBat.bat" "MY_URL"');
    

提交回复
热议问题