Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)\wkhtmltopdf\wkhtmltopdf.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.

like image 209
JoBaxter Avatar asked Mar 12 '13 01:03

JoBaxter


2 Answers

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&param2=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"');
like image 32
Jon Rubins Avatar answered Nov 14 '22 23:11

Jon Rubins


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)\wkhtmltopdf\wkhtmltopdf.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"');
like image 118
Pebbl Avatar answered Nov 14 '22 22:11

Pebbl