How to start an application from HTA?
ask:
Hello, script expert! For HTA, is there anything that can replace the Wscript.Shell command? I need to run some application and specify the file to open.
-- DL
answer:
Hello DL. Yes, we do know that such commands can be used in HTA and can be used instead of the Wscript.Shell command, and we'll introduce you to it in a while. Before introducing it, however, we should note that you can actually use the Wscript.Shell object in HTA. This is a common confusion: Because you cannot use certain commands in the HTA (such as Wscript.Echo and Wscript.Sleep), people think you cannot use any WSH commands in the HTA.
Before continuing, there is another question: Why can't you use Wscript.Echo and Wscript.Sleep in HTA? That is, these methods are properties of Wscript objects, and you are unable to create instances of Wscript objects. Wscript objects are created automatically and are only created when you run Windows Script Host (i.e. Wscript.exe or Cscript.exe). It is precisely because of this that the following scripts are absolutely valid:
The code is as follows:
Wscript.Echo "Hey."
Note that we did not create a Wscript object, which was automatically created when we call Windows Script Host.
But this is limited to Wscript objects. There are other WSH objects that you can create, including Shell objects. For example, here is a simple little HTA that creates a Wscript.Shell object and then runs Notepad.exe (and opens the file C:/Scripts/Test.txt in the process):
The code is as follows:
<html>
<head>
<script language="VBScript">
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe c:/scripts/test.txt"
End Sub
</script>
</head>
<body>
<button onclick="RunProgram">Run Program</button> <p>
</body>
</html>
As you can see, this is almost the easiest HTA you can get: it consists of just one button and a subroutine called RunProgram is run when clicked. Please take a look at the RunProgram code:
The code is as follows:
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe c:/scripts/test.txt"
End Sub
That's it: we create an instance of the Wscript.Shell object and call the Run method. This passes a single parameter for Run: the executable file name (notepad.exe) followed by the path of the file you want to open. That's all we need to do.
By the way, as long as the script is run in the HTA, there will be no problems. If you try to run it in an HTML file (that is, a file with the file extension .htm), a message box appears warning you that an ActiveX control is trying to run on the page. You must click Yes to allow the subroutine to create a Shell object and then run it. This is because WSH objects are considered "insecurity for scripting".
Note: Yes, this sounds a bit weird, and I actually think that scripting objects are not safe for scripting. But this is because Internet Explorer uses different script hosting and different security models than WSH. Fortunately, HTA uses a different security model than Internet Explorer, which means you don't encounter this problem when creating Shell objects within the HTA.