17

I am trying to run a .exe file from Javascript. This is what I have:

var oShell = new
ActiveXObject("Shell.Application"); 
var commandtoRun = "C:\Documents and
Settings\User\Desktop\ABCD.exe";
oShell.ShellExecute(commandtoRun,"","","open","1");

If I have only the first 2 lines code it seems to work fine (it asked me do I want activeX when I opened it first time in IE) but if I add the last line (ShellExecute) there seems to be an error. I want to pass arguments to the exe.

Does anyone know how to do it ?

Hackoo
18.9k3 gold badges49 silver badges85 bronze badges
asked Jun 30, 2010 at 19:01
2

1 Answer 1

18

You need to escape the backslashes, e.g.,

var commandtoRun = "C:\\Documents and Settings\\User\Desktop\\ABCD.exe";

Update:

This works fine on my machine:

var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Windows\\notepad.exe"; 
oShell.ShellExecute(commandtoRun,"","","open","1");

Update 2

You can save this as a file with the extension .hta and it should work in your browser:

<HTA:APPLICATION ID="oMyApp" 
APPLICATIONNAME="Application Executer" 
BORDER="no"
CAPTION="no"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="yes"
SCROLL="no"
WINDOWSTATE="normal">
<script type="text/javascript" language="javascript">
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Windows\\notepad.exe"; 
oShell.ShellExecute(commandtoRun,"","","open","1");
</script>
clerktech
1413 silver badges20 bronze badges
answered Jun 30, 2010 at 19:05
Sign up to request clarification or add additional context in comments.

9 Comments

Also: @Jason's link suggests using %20 for escaping the spaces and using the format: file:///C:/Program%20Files/...etc - perhaps it's some variation on that?
@Red Filter :-I already tried that before posting ...doesnt help though
Still doesnt work for me :( ....Although it shouldnt matter- I call this .exe from a GWT project using native Javascript module ...is there a way to know the error? (If I remove the last line web page opens up fine ...if I keep the line though it runs fine until that line and then shows a blank page in the browser)
Let's be clear, I am running this from the command-line, executing a file name test.js using cscript.exe. Do not expect to be able to launch applications from a web page with this Javascript embedded. That is against the security restrictions. And I gues technically I am using JScript, not Javascript.
Is there a work around for this ? Everything is done locally (as a prototype on my machine ...I am not publically hosting it)....I just want the .exe to run when a particular event happens in the UI ....
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.