I've written a few scripts in which I use VBScript's InputBox and MsgBox functions to get input from users and Windows Script Host's (WSH's) Echo method to display output. The scripts work well, but I'd really like to have a better way to obtain input and display output. How can I provide better-looking UIs in my scripts?
There are several ways to provide better-looking UIs. One of the most common ways is to use HTML Applications (HTAs). Microsoft introduced HTAs in Microsoft Internet Explorer (IE) 5.0. At their core, HTAs are Web pages. They contain HTLM, Cascading Style Sheets (CSS), and embedded scripting code. However, unlike standard HTML Web pages, HTAs aren't run from a server. Instead, they're run from the desktop. Another difference is that standard HTML Web pages use the .htm extension, whereas HTAs use the .hta extension.
In addition to being a useful front end for administrative scripts, HTAs are commonly used for setup programs. In its simplest form, an HTA can contain pure HTML code, but that would result in a static page. Because HTAs can support embedded scripting code, you can use them dynamically to address just about any scripting task.
Much like standard Web pages, HTAs contain HTML tags. HTAs begin and end with the <HTML> and </HTML> tags, respectively. However, HTAs have some unique attributes that set them apart from HTML pages, including the <HTA:APPLICATION> tag and the <SCRIPT> and </SCRIPT>tags. The <HTA:APPLICATION> tag designates the page as an HTA. The <SCRIPT> and </SCRIPT> tags mark the beginning and end of a section of scripting code, much like in Active Server Pages (ASP). HTAs support both embedded VBScript and JScript code.
Listing 2 shows a sample HTA called ReadFile.hta. This HTA prompts users to input a filename, then click the Read File button. After users click that button, the HTA executes VBScript code that reads the filename, opens the file, and displays the file's contents on the page.
Let's take a closer look at how ReadFile.hta works. ReadFile.hta begins with the standard <HTML> tag to denote the beginning of the document. The <HEAD> tag contains the output for the document's title bar.
The code at callout A in Listing 2 is the core of the application. The attributes following the <HTA:APPLICATION> tag define how the HTA window will appear. For example, the attributes specify the window's border style and width.
Next, you'll find the <SCRIPT> tag, which denotes the beginning of the VBScript code. Before I discuss this file-processing code, though, let's look at the HTML code that displays the UI in which users enter the name of the file they want to access.
Callout B shows the HTML code that creates the UI. First, the code sets the background to the color gray, then it creates a simple table that contains two UI objects: a text box in which users input a filename and a button that, when clicked, executes the VBScript code. Setting the <INPUT> tag's Type attribute to "text" creates the text box. The Size attribute sets the text box's width to 50 characters. The Name attribute assigns the text box the name fName, which corresponds to the fName object in the VBScript code. The <BUTTON> tag creates the button. The Style attribute sets the button's appearance. The onClick attribute specifies that the ReadFile subroutine will be executed when the user clicks the button.
The VBScript code between the <SCRIPT> and </SCRIPT> tags contains the ReadFile subroutine. First, this subroutine creates a FileSystemObject object named oFS, then uses that object's FileExists method to test for the existence of the fName.value file. (VBScript views the UI's text box as an object—i.e., the fName object—and this object's Value property contains the filename.) If the FileExists method doesn't find the specified file, the subroutine displays a message that states the file wasn't found. Otherwise, the subroutine uses the oFS object's OpenTextFile method to create a TextStream object named oFile. The subroutine uses the oFile object's ReadAll method to read the contents of the file into the sContents variable and assigns this variable to the HTA's document.body.InnerHTML property, which displays the contents of the file in the HTA window.
Although ReadFile.hta displays the file's contents in a window that looks very much like the standard IE window, HTAs aren't limited to that look. You have a great deal of control over what the UI looks like. Moreover, you can execute just about any VBScript code from within the HTA. You can find more information about scripting and HTA applications at http://msdn.microsoft.com/library/default.asp?url=/workshop/author/hta/overview/htaoverview.asp.