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

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.

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"');

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"');

Related

casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js did not working

Hi for me the command did not working:
$ casperjs --web-security=no --cookies-file=/tmp/mycookies.txt myscript.js
(just copied from the documentation of casperjs: http://docs.casperjs.org/en/latest/cli.html#casperjs-native-options)
I have created the cookie file from another script. Now I want to implement the cookies into the second script. But the above command did not work.
What I want, is to implement the cookies before the first page invoke.
And yes the cookies are still alive, bcause if I use this in the second script:
var fs = require('fs');
phantom.cookies = JSON.parse(fs.read("pathToTheCookies"));
it works. But I want to do it with the command line, because I want to give the path with arguments.
Thanks.
OS: Windows 10
Best regards.
rikku47
I got it. My solution for Windows (my OS Windows 10) is a command line like this:
casperjs --cookies-file="""pathIncludeWhiteSpaces""" """scriptPathIncludeWhitspaces""" """argument1""" """argument2"""
In the arguments you can put paths too. Just wrape them with six quotes
"""AnyArgumentAndOrPath"""
and it should work. the official solution from the documentation did not work for me, if I use cookies.
With other word if I use this command:
casperjs --cookies-file=\"pathIncludeWhiteSpaces\" \"scriptPathIncludeWhitspaces\" \"argument1\" \"argument2\"
it work for the first time to write the cokkie file. But if I let it read, it do nothing (it looks like).
Here is the part of the documentation:
Hint
You may need to wrap an option containing a space with escaped double
quotes in Windows. –foo=\”space bar\”
http://docs.casperjs.org/en/latest/cli.html#casperjs-native-options
Best regards rikku47

using command line in javascript

I know there are lots of questions about this already, but i cant seem to find one that works for me.
I am trying to launch a local file from a local html using cmd to pass command to launch file but it does not seem to work.
This is what i used so far:
<script type="text/javascript" language="javascript">
function RunFile() {
window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
}
</script>
someone pls help with this.
Lets just asume i can do this on IE window.open('C:/Windows/System32/cmd.exe); and it will open cmd.
My question is how do i pass some extra argument to make the cmd open my file from another location e.g. window.open('C:/Windows/System32/cmd.exe /c START %temp%/file.cpl');
You cannot run a program using a browser. You might be confused with Windows Scripting JScript, check: https://en.wikipedia.org/wiki/JScript
You may run apps using that (in windows shell). Check this: https://stackoverflow.com/a/15351708/1082061
You could do this using server-side binary execution on Nodejs using child_process.
Pro: Easy to use, just need a simple AJAX call to trigger execution from client to Node server.
Cons: Need to use a server instead of a single HTML page.

How to minify JavaScript files that contains a function name separated with a '.'?

I am minfying 100's of Javascript files on a windows OS and ran into a issue with almost half of them. It turns out that the minfiying compressor cannot properly minify js files that has a function spearated by a dot. For the YUI compressor it deletes the contents of the file when such a situation happens, and for Ajaxminifier, it just ignores the dot and take the function name before it.
Example of the function in question:
function window.onload() {}
Error Message: :missing ( before function parameters
Using the YUI compressor as an MSBuild task and command line both yeild the same results.
Command line example:
java -jar yuicompressor.jar --type js --charset utf-8 -o D:\foo.js D:\foo-min.js
Using the Ajax Minifier example:
Command line:
AjaxMin.exe -o D:\foo.js D:\foo-min.js
However, one solution is to rewrite the function and the minification process works great
Example:
window.onload = function() {}
But this is not an option for us, right now.
Does anyone know about this problem and able to provide a solution?
Thanks. Yes, I did inherit these files and I am yet to determine why it was written this way. I dont believe there is any processor that converts it to proper JS..I do know that the web app only runs on IE, this may be the reason why this is working. On firefox etc, it may not be the case.I am looking to get this changed
It's breaking because you're trying to minify invalid javascript. Functions cannot have dots in them.
I think you're trying to namespace. If you are, you would be better off doing something like:
var myVar = {
foo: function () { /* do something */ }
}
myVar.foo();

Windows XP and Up: JavaScript instead of VBScript?

I asked how to check modify timestamps with BAT files and launch a command based on an if statement and Wimmel asked if I could use VBScript instead of Batch Files. I think this is a grand idea. This leads to another question
Can I access the VBScript functionality with JavaScript, while still being compatible Windows XP to Current? (specifically checking file modify timestamp and running a command depending on how recently modified)
Not sure that it is a good idea, but yes, you can use JavaScript (actually, JScript) instead of VBScript. Just use ActiveXObject class instead of CreateObject function that is used in VBScript to create objects.
Here is a code that reads the file modify timestamp using Windows Scripting and JScript:
var o = new ActiveXObject("Scripting.FileSystemObject");
var file = o.GetFile("c:\\temp\\test.js");
WScript.Echo(file.DateLastModified);
For more information, see JScript documentation and Windows Script Host documentation
Even though there are probably easier ways to achieve what you want to do, I had a go at trying the more theoretical part of your question, and apparently all the things we need are there.
Here is what I tried:
test.js:
WshShell = WScript.CreateObject("WScript.Shell");
var result = WshShell.Run("test.vbs", 0, true);
WSH.Echo(result);
test.vbs:
WSH.Echo "test.vbs"
WSH.Quit 5

Set working directory in javascript

Does anyone know how to set the working directory in JavaScript before?
Code I use for launching an application is this:
// Create an object script
oL = new ActiveXObject("WScript.Shell");
oFile = '"C:/Application.exe"';
oL.run(oFile);
According to MSDN, you should be able to use:
var oL = new ActiveXObject("WScript.Shell");
oL.CurrentDirectory = "C:\\Foo\\Bar";
oFile = '"C:\\Application.exe"';
oL.run(oFile);
...assuming you're running this script in Windows Script Host, in which case you probably ought to make that clear in your question - about 99% of JavaScript programmers only ever use the language in a web browser, where this kind of stuff is only possible under extremely unusual circumstances.
Javascript typically runs in a sandbox that means it doesn't have access to the filesystem anyway, thus setting the cwd is meaningless.
What context are you trying to do this in (website javascript, local script running with Rhino etc.) and what are you trying to achieve?
Javascript dosent have access to your harddrive so why should you be able to set the working directory?

Categories