Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

javascript - Using a WScript.shell activeX to execute a command line

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 when I am just calling C:wkhtmltopdf.exe in the code. It runs and then closes correctly. But my issue is you need to run it from cmd with the program name then the HTML file name you are reading followed by the .pdf name you want it to be created as.

For example:

c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf

This will call wkhtmltopdf.exe, read c:PDFTestPage.html, then create c:TEST.pdf. Works fine when I type it into cmd.

Does anyone know an activeX that will not just run and .exe but actually execute a command line?

Here is my code that I am currently using.

function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:wkhtmltopdf.exe"');
}

Would really like it to be the following.

function callShellApplication(){
var objShell = new ActiveXObject("WScript.shell");
objShell.run('"c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf"');
}

Also side note. For some reason I cant launch the .exe from an absolute path. I have to move to the directory and then just type in wkhtmltopdf.exe. The fill path is:

C:Program Files (x86)wkhtmltopdfwkhtmltopdf.exe

I really only work with UNIX so I'm not sure about spaces in the path. I can do a chdir with the spaces but I cant use the fill path when executing it. Any information would be helpful. Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the following:

http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx

You should be able to pass the commands directly as part of the strCommand param, you'd probably be better off getting rid of the extra quotes wrapping the entire command and arguments:

function callShellApplication(){
  var objShell = new ActiveXObject("WScript.shell");
  objShell.run('c:wkhtmltopdf.exe c:PDFTestPage.html c:TEST.pdf');
}

Also you should be able to handle spaces in paths by wrapping each item in quotes, like so:

function callShellApplication(){
  var objShell = new ActiveXObject("WScript.shell");
  objShell.run('"C:Program Files (x86)wkhtmltopdfwkhtmltopdf.exe" "c:PDFTestPage.html" "c:TEST.pdf"');
}

You should also keep in mind whether you want to bWaitOnReturn or not, and which intWindowStyle you need (some executables may benefit from a particular style).

Also just as a cautionary note — it's been a while since I've used WScript.shell — but you may need to escape your backslashes in your paths. So a path may need to look like the following:

objShell.run('"C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe"');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...