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,337
  • 3
  • 40
  • 70
Manish
  • 507
  • 3
  • 9
  • 21

1 Answers1

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
  • 141
  • 3
  • 18
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • 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? – Ciaran Archer Jun 30 '10 at 19:39
  • 1
    @Red Filter :-I already tried that before posting ...doesnt help though – Manish Jun 30 '10 at 19:40
  • 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) – Manish Jun 30 '10 at 19:51
  • 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. – D'Arcy Rittich Jun 30 '10 at 20:03
  • 1
    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 .... – Manish Jun 30 '10 at 20:39
  • I mean is there a way of changing the security settings to allow this execution since I vouch for the .exe that I have created .... – Manish Jun 30 '10 at 20:49
  • Is this how you want the .hta to be run from Javascript? var oShell = new ActiveXObject("Shell.Application"); var commandtoRun = "C:\\Documents and Settings\\User\\Desktop\\ABCD.hta"; oShell.ShellExecute(commandtoRun,"","","open","1"); ABCD.hta contains exact same code [I copied it in a txtfile and saved it as .hta] that you wrote above .I tried running it like this in IE and it doesnt work. – Manish Jun 30 '10 at 21:28
  • Is it possible to open the .hta file without downloading it. By default IE 9 saves and provides option to download. – Kannan Karmegam Jun 05 '15 at 04:42
  • hello, this only works in IE. help me with chrome? – meekash55 Jul 20 '23 at 18:09