How Can I Start an Application From an HTA?

How Can I Start an Application From an HTA?

  • Comments 14
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there an alternative to the Wscript.Shell command for HTAs? I need to run an application and specify the file to open.

-- DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. Yes, we do know of an alternative to the Wscript.Shell command that will work in HTAs, and we’ll show you that in a minute. Before we do that, however, we should note that you actually can use the Wscript.Shell object within an HTA. This is a point that often creates confusion: because you can’t use certain commands - such as Wscript.Echo and Wscript.Sleep - within an HTA people assume you can’t use any WSH commands in an HTA.

First things first: why can’t you use Wscript.Echo and Wscript.Sleep in an HTA? Well, those methods are properties of the Wscript object, and you can’t create an instance of the Wscript object. Instead, the Wscript object is automatically created - and only created - when you run Windows Script Host (that is, Wscript.exe or Cscript.exe). That’s why this is a perfectly valid script:

Wscript.Echo "Hey."

Notice that we didn’t create the Wscript object; instead, it was created for us when we invoked Windows Script Host.

But that’s just for the Wscript object. There are other WSH objects that you can create, including the Shell object. For example, here’s a simple little HTA that creates the Wscript.Shell object and then runs Notepad.exe (opening the file C:\Scripts\Test.txt along the way):

<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 about as simple an HTA as you can get: it consists entirely of a button that, when clicked, runs a subroutine named RunProgram. And take a look at the code for RunProgram:

Sub RunProgram 
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run "notepad.exe c:\scripts\test.txt"
End Sub

There it is: we create an instance of the Wscript.Shell object and then call the Run method. And in doing so we pass Run a single parameter: the executable file name (notepad.exe) followed by the path to the file we want to open. That’s all we have to do.

Incidentally, as long as you run this in an HTA everything will work just fine. If you try to run it in an HTML file (that is, a file with a .htm file extension) you’ll be presented with a message box warning you that an ActiveX control is trying to run on the page. At that point you’ll have to click Yes to allow the subroutine to create the Shell object and then run. This is due to the fact that WSH objects are considered “unsafe for scripting.”

Note. Yes, it sounds a bit weird that scripting objects aren’t considered safe for scripting. But that’s because Internet Explorer uses a different script host and a different security model than WSH. Fortunately, HTAs use a different security model than Internet Explorer, which means you won’t encounter this problem when creating the Shell object within an HTA.

Now, what about that alternative? Well, if for some reason you don’t want to use the Wscript.Shell object then you can use the Windows Shell object instead. This HTA will also start Notepad and open the file C:\Scripts\Test.txt:

<html> 
<head> 
<script language="VBScript"> 

Sub RunProgram 
    Const NORMAL_WINDOW = 1
    Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "notepad.exe", "c:\scripts\test.txt", , , NORMAL_WINDOW
End Sub

</script> 
</head> 

<body> 
<button onclick="RunProgram">Run Program</button> <p>
</body> 
</html>

To be honest, we don’t see any real advantage to using the Windows Shell object as opposed to the Wscript.Shell object: both objects do pretty much the same thing. However, if you’d like to play around a bit with the Windows Shell, check out the documentation on the ShellExecute method. The important thing is this: if you want to start an application from your HTA, either approach will work.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • How can you do this + have that notepad window automatically resize itself and move to a certain place on the screen?

  • When I run this, why do I get an error "Object doesn't support this property or method"?

  • shell = new ActiveXObject("WScript.shell");

    shell.run("googleearth.exe");

    This JavaScript version snippet works for winzip.exe, iexplore.exe, firefox.exe but not for googleearth, notepad++, sketchup.exe ... after which I gave up trying to find a link. I hope you can unmask the mystery for me.

    Thanks

  • Hey,

    I am not a script guy or a developer, but I have a need and so here is my question.

    I am trying to run a .vbs file that is being created by the "Scriptomatic" Microsoft program, but I want to make it so when the script runs, to be displayed onto the screen as an HTML file.

    When I run it through the scriptomatic it all works great, but I can't have the program out there for my Help Desk, so I want the .vbs file to be self-contained, and give it to the Help desk to run it when needed.

    When I save the .vbs file and run it as-is I get a whole bunch of windows of every function within the script.

    I need either a big window with all of the info together, or an HTML file built and displayed for us.

    Any help would be very much appreciated.

    Thanks

    Example:

    On Error Resume Next

    Const wbemFlagReturnImmediately = &h10

    Const wbemFlagForwardOnly = &h20

    arrComputers = Array("localhost")

    For Each strComputer In arrComputers

      WScript.Echo

      WScript.Echo "=========================================="

      WScript.Echo "Computer: " & strComputer

      WScript.Echo "=========================================="

      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

      Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", _

                                             wbemFlagReturnImmediately + wbemFlagForwardOnly)

      For Each objItem In colItems

         strBiosCharacteristics = Join(objItem.BiosCharacteristics, ",")

            WScript.Echo "BiosCharacteristics: " & strBiosCharacteristics

         strBIOSVersion = Join(objItem.BIOSVersion, ",")

            WScript.Echo "BIOSVersion: " & strBIOSVersion

         WScript.Echo "BuildNumber: " & objItem.BuildNumber

         WScript.Echo "Caption: " & objItem.Caption

         WScript.Echo "CodeSet: " & objItem.CodeSet

         WScript.Echo "CurrentLanguage: " & objItem.CurrentLanguage

         WScript.Echo "Description: " & objItem.Description

         WScript.Echo "IdentificationCode: " & objItem.IdentificationCode

         WScript.Echo "InstallableLanguages: " & objItem.InstallableLanguages

         WScript.Echo "InstallDate: " & WMIDateStringToDate(objItem.InstallDate)

         WScript.Echo "LanguageEdition: " & objItem.LanguageEdition

         strListOfLanguages = Join(objItem.ListOfLanguages, ",")

            WScript.Echo "ListOfLanguages: " & strListOfLanguages

         WScript.Echo "Manufacturer: " & objItem.Manufacturer

         WScript.Echo "Name: " & objItem.Name

         WScript.Echo "OtherTargetOS: " & objItem.OtherTargetOS

         WScript.Echo "PrimaryBIOS: " & objItem.PrimaryBIOS

         WScript.Echo "ReleaseDate: " & WMIDateStringToDate(objItem.ReleaseDate)

         WScript.Echo "SerialNumber: " & objItem.SerialNumber

         WScript.Echo "SMBIOSBIOSVersion: " & objItem.SMBIOSBIOSVersion

         WScript.Echo "SMBIOSMajorVersion: " & objItem.SMBIOSMajorVersion

         WScript.Echo "SMBIOSMinorVersion: " & objItem.SMBIOSMinorVersion

         WScript.Echo "SMBIOSPresent: " & objItem.SMBIOSPresent

         WScript.Echo "SoftwareElementID: " & objItem.SoftwareElementID

         WScript.Echo "SoftwareElementState: " & objItem.SoftwareElementState

         WScript.Echo "Status: " & objItem.Status

         WScript.Echo "TargetOperatingSystem: " & objItem.TargetOperatingSystem

         WScript.Echo "Version: " & objItem.Version

         WScript.Echo

      Next

    Next

    Function WMIDateStringToDate(dtmDate)

    WScript.Echo dtm:

    WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _

    Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _

    & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))

    End Function

  • Chris, check the following link for your answer.

    technet.microsoft.com/.../ee692829.aspx

  • i dont know how i can aply a notebook

    in my background desktop can you show me

    i am looking everywhere but i cant find

    how to do it does it have to be windows 7?

  • Im new to scripting and could use some help.  I have a need for a vbscript that has a drop down list and a button to run the application with arguments.  I got how to start an application but cant get the arguments to work.  I know how to create a select list but how do i tie them together so that when users select an option it runs the app with those specific arguments.  any help is much appreciated.

  • How can I get the browser to open the default media player for an audio file?

  • Just create a hyperlink to teh file.  It will open in player when clicked.

    <a href='c:\music\song.mpg'>Play Song</a>

  • I have an interesting question?

    Maybe simple for someone of your great scripting talents.

    I have the following script that checks a machine to see what the value is for free memory total memory and page file size.

    The vb works but I want it to be in an HTA so So as to when I log on I can add all of my scripts.

    THE VB Script:

    ========================

    'On Error Resume Next

    '''Pulls the local computer name'''

    Set WshShell = WScript.CreateObject("WScript.Shell")

    strComputer = WshShell.RegRead ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName")

    '''Sets the file you want to write to'''

    strOutputFile = "MemoryInfo.txt"

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

    For Each objItem in colItems

           freePhysMem =  objItem.FreePhysicalMemory / 1048576

                   totalVMSize = objItem.TotalVirtualMemorySize / 1048576

                   totalVisMem = objItem.TotalVisibleMemorySize / 1048576

                   freeVMSize = objItem.FreeVirtualMemory / 1048576

    Next

    Set objFileSystem = CreateObject("Scripting.fileSystemObject")

    Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)

       ObjOutputFile.WriteLine("Run Date = " & Date)

       ObjOutputFile.WriteLine("Run Time = " & Time)

       ObjOutputFile.WriteLine("")

       ObjOutputFile.WriteLine("Computer:"& strComputer)

       ObjOutputFile.WriteLine("")

    'objOutputFile.WriteLine ("Server Name           = " & strComputer)

            ObjOutputFile.WriteLine("-----------------------------------")

    objOutputFile.WriteLine ("Total Physical Memory = " & totalVisMem & " MB")

    objOutputFile.WriteLine ("Free Physical Memory  = " & freePhysMem & " MB")

    objOutputFile.WriteLine ("Total Virtual Memory  = " & totalVMSize & " MB")

    objOutputFile.WriteLine ("Free Virtual Memory   = " & freeVMSize & " MB")

            ObjOutputFile.WriteLine("-----------------------------------")

                   ObjOutputFile.WriteLine("")

                   ObjOutputFile.WriteLine("")

    Set objFileSystem = Nothing

    ====================================================================

    My question is how can I wrap this in a .hta file to display the values and copy to clipboard.

  • @Ty the Tek

    You are posting a help question in a blog.  Your question has nothing to do with this blog post.  Questions of this type should be posted in the associated forum for this blog.

    Post here and you will get an answer.

    social.technet.microsoft.com/.../ITCG

  • Is there any way for the script to automatically launch with associated app of the file? I have an application which generates csv file and I want to display a link to the output file which opens in excel, for instance.

  • How would I go about adding a second button?

  • I cannot begin to tell you how awesome your blog is!!! You guys rock!!! This little article helped me out immensely!