javascript 使用 WScript.shell activeX 执行命令行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15351508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a WScript.shell activeX to execute a command line
提问by JoBaxter
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.
我正在使用 WScript.shell activeX 调用 .exe 文件。该文件是 wkhtmltopdf.exe,它用于将 HTML 页面转换为 .pdf。当我只是在代码中调用 C:\wkhtmltopdf.exe 时,一切都运行良好。它运行然后正确关闭。但我的问题是您需要从 cmd 运行它,程序名称然后是您正在阅读的 HTML 文件名,然后是您希望将其创建为的 .pdf 名称。
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.
这将调用 wkhtmltopdf.exe,读取 c:\PDFTestPage.html,然后创建 c:\TEST.pdf。当我在 cmd 中输入它时工作正常。
Does anyone know an activeX that will not just run and .exe but actually execute a command line?
有谁知道一个不仅可以运行和 .exe 而是实际执行命令行的 activeX 吗?
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:
还有旁注。出于某种原因,我无法从绝对路径启动 .exe。我必须移动到该目录,然后输入 wkhtmltopdf.exe。填充路径为:
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.
我真的只使用 UNIX,所以我不确定路径中的空格。我可以用空格做一个 chdir,但在执行它时我不能使用填充路径。任何信息都有帮助。先感谢您。
回答by Pebbl
According to the following:
根据以下内容:
http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx
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:
您应该能够直接将命令作为strCommand
参数的一部分传递,最好去掉包裹整个命令和参数的额外引号:
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).
您还应该记住您是否愿意bWaitOnReturn
,以及intWindowStyle
您需要哪些(某些可执行文件可能受益于特定样式)。
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:
也只是作为一个警告 - 我已经有一段时间没有使用了WScript.shell
- 但你可能需要在你的路径中避开你的反斜杠。因此,路径可能需要如下所示:
objShell.run('"C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe"');
回答by Jon Rubins
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.
我也想使用ActiveXObject("WScript.shell
. 我需要运行一个.bat
脚本来启动谷歌浏览器到一个特定的 URL。
The JS I had was as follows:
我的JS如下:
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:
这将正确启动我的.bat
脚本,这非常简单:
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
我遇到的问题是MY_URL
包含一些查询参数,当我使用上述 JS 时,查询参数会被剥离到一定程度。所以当我试图打开
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
结果证明修复很简单 - 我不得不MY_URL
用引号括起来。所以我修改了这条线
objShell.run('"C:\Scripts\MyChromeBat.bat" MY_URL');
to be
成为
objShell.run('"C:\Scripts\MyChromeBat.bat" "MY_URL"');