Customizable dialog boxes give you freedom in developing interactive scripts for users. I found this feature to be sorely lacking in the Windows Script Host environment until I recently discovered a technique for using Internet Explorer (IE) objects within VBScript. I showed you how to create the DialogBox function in “Create your own reusable dialog box for Windows Script Host.” In this Daily Drill Down, I’ll show you how to create two templates: the HTML dialog box template and the Windows Script File template that calls the DialogBox function and then manipulates the results. Then I’ll explain how the three files work together to implement my reusable dialog box technique. For those of you who feel the need to see how this technique looks in the real world, I have included a sample script in a download, which you can use for your own purposes.
The HTML dialog box template
The HTML document holds the contents of the dialog box and will handle both the HTML and VBScript code. The HTML code will use the form tags to create the contents of the dialog box, which will include a prompt for information and appropriate controls for gathering user input, such as text boxes, radio buttons, or check boxes. It will also contain two buttons—OK and Cancel—for signaling the completion or dismissal of the dialog box. The VBScript code will provide a means of interacting with the DialogBox function.
Of course, the contents of each dialog box will be different depending on what the application is and what input is needed from the user. However, each dialog box HTML document you’ll create will contain some basic elements. Listing A provides you with an HTML dialog box template that you can use to create your custom dialog boxes.
To describe how the HTML dialog box file works, I’ll break it down into four key sections—the design, the script components, setting up the form, and the OK and Cancel buttons. As I go over this file, I’ll bypass the standard HTML components and focus on those that pertain to this technique.
Listing A: HTMLDbx.htm
- <body bgcolor=”#D3D1CF” scroll=”no”>
- <script language=”VBScript”>
- Dim Continue
- Sub Window_OnLoad()
- Continue = 0
- End Sub
- Sub button1_OnClick
- Continue = 1
- End Sub
- Sub button2_OnClick
- Continue = 3
- End Sub
- Public Function ClickButton
- ClickButton = Continue
- End Function
- <form name=”Dbx”>
- <p>Sample Dialog Box</p>
- <p><input type=”button” name=”button1″ value=” OK “>
- <input type=”button” name=”button2″ value=”Cancel”></p>
To begin, you’ll want your custom HTML dialog boxes to have the same silver color as the traditional dialog boxes. Set the bgcolor tag to #D3D1CF, as shown in line 5. You won’t want your custom HTML dialog to have a vertical scroll bar, so set the scroll option to No.
In line 6, I begin the script components of the HTML file by declaring the scripting language to be VBScript. As you can see, the script components in the HTML file consist of three subroutines and one function.
Now, in line 8, I create the Continue variable that will be used by all of the subroutines and the function to hold the value associated with the result of clicking one of the dialog box’s buttons. That result will eventually be sent back to the Windows Script File via the DialogBox function, as we’ll see a bit later on.
Lines 9 through 11 make up the Window_OnLoad subroutine, which is actually an event procedure that is automatically triggered as soon as the HTML file is loaded into the browser. As shown in line 10, the purpose of this procedure is simply to initialize the Continue variable with a value of 0.
The button1_OnClick subroutine, in lines 12 through 14, is also an event procedure that is triggered as soon as button1 (in this case, the OK button) is clicked. This event procedure then assigns the Continue variable a value of 1. Likewise, lines 15 through 17 make up an event procedure that is triggered as soon as the Cancel button is clicked; the procedure then assigns the Continue variable a value of 3.
Finally, lines 18 through 20 make up the Public function called ClickButton. Since this function is declared as Public, the value in its name is available to all running scripts, including the Windows Script File that called it. This means that it can pass its results back to the Windows Script File.
Now, let’s take a look at the outline of the form that will create the contents of the dialog box. The form is declared and assigned the name Dbx in line 23. Keep in mind that the form must be named Dbx. The Windows Script File Driver template is designed to reference the form using this name. While you could name it anything, sticking to this naming convention helps make the dialog box technique easy to reuse—all you have to remember is that the form is named Dbx.
Between lines 23 and 27, you’ll place the commands for your custom dialog box controls. (You’ll learn how to implement them in a future Daily Drill Down.) For now, the template only contains a simple text description in line 24 and then the commands to set up the OK and Cancel buttons.
OK and Cancel buttons
Lines 25 and 26 use the input type=button tag to set up the OK and Cancel buttons. The buttons are then named button1 and button2, respectively, which correspond to the OnClick events described in the script section.
The Windows Script File Driver template
We’re now ready to look at the Windows Script File that will act as the driver for the dialog box technique. The template for the Windows Script File Driver file is shown in Listing B.
To describe how the Windows Script File Driver file works, I’ll break it down into six key sections—accessing the DialogBox function, initializing the variables, calling the DialogBox function, handling a Cancel request, closing the dialog box, and working with the results. As I go over this file, I’ll bypass the standard XML components and focus in on the scripting components that pertain to this technique.
Listing B: DbxDriver.wsf
- <?XML Version=”1.0″ ?>
- <?job error=”true” debug=”true”?>
- <job id=”VB Code”>
- <script language=”VBScript” src=”FDialogBox.vbs”>
- Dim IEDbx, FileSystem, DbxFile, Cancel
- Set WshShell = WScript.CreateObject(“WScript.Shell”)
- Set FileSystem = CreateObject(“Scripting.FileSystemObject”)
- Set IEDbx = WScript.CreateObject(“InternetExplorer.Application”)
- Cancel = False
- DbxFile = “HTMLDbx.htm”
- Call DialogBox(100, 200, 160, 280, Cancel, DbxFile)
- If Cancel Then
- Set IEDbx = Nothing
- End If
- Set IEDbx = Nothing
- Display = “You clicked OK”
- MsgBox Display
Including the DialogBox function
To access the reusable DialogBox function from within your Windows Script File Driver file, you’ll need to declare the scripting language and include the file containing the DialogBox function. I’ve used the XML <script> tag and its two arguments in line 4 to do so. Here, the language= argument tells the Windows Script Host to load the VBScript scripting language engine. Then the src= argument tells the Windows Script Host to load the FDialogBox function and make its contents available.
Initializing the variables
In lines 7 through 9, I initialize the object variables that I’ll use for the technique. In line 7, I initialize the Windows Script Host object and assign it to the object variable WshShell. Keep in mind that while I don’t specifically use the Windows Script Host object in the Windows Script File Driver template file, I’ve included it to make the template easy to use in the future.
Line 8 initializes the Windows Script Host’s FileSystemObject and assigns it to the object variable appropriately called FileSystem. The FileSystem object variable is used in the DialogBox function to locate the exact path to the HTML dialog box file.
Line 9 initializes the IE object and assigns it to the object variable called IEDbx. Both the Windows Script File Driver file and the DialogBox function specifically reference the IE object by the name IEDbx. You’ll want to make sure that you use this name for the IE object variable.
Now, in line 10, I set the Cancel flag variable to False. I then assign the DbxFile variable the name of the HTML file containing the dialog box controls. In this case, I’m using the name of the HTML dialog box template.
Calling the DialogBox function
In line 12, I use the Call statement to transfer control of the scripting session to the DialogBox function, along with the list of required parameters. These parameters include the dimensions for the dialog box, the Cancel flag, and the DbxFile variable, which contains the name of the file containing the dialog box controls.
Handling a Cancel request
Lines 13 through 17 are designed to handle the situation when a user clicks the Cancel button in the dialog box. In this case, I use the Quit method to close the dialog box. I then reset the IEDbx object variable by using the Set command to assign its contents to the VBScript Nothing keyword. This is basically a clean-up step that releases any memory and system resources associated with the object variable. I then use the Windows Script Host’s Quit method to gracefully end the script.
Closing the dialog box
If the user clicks OK, I still need to close the dialog box and reset the IEDbx object variable before continuing. I do this in lines 18 and 19.
Working with the results
After you close the dialog box and reset the IEDbx object variable, you’ll interpret the values obtained from the dialog box and perform the associated actions. (I’ll show you how to do this in a future Daily Drill Down.) For now, lines 20 and 21 simply display a Message Box indicating that the user clicked OK.
Testing the DialogBox function
You now have a working DialogBox function that you can incorporate into your future scripts. To use it, you’ll simply modify the Windows Script File Driver and HTML dialog box templates.
You can test the DialogBox function with the template files. To do so, run the DbxDriver.wsf file. You’ll see the dialog box shell shown in Figure A. If you click the OK button, you’ll see a follow-up dialog box that informs you that you’ve clicked OK. If you click Cancel, the dialog box will simply disappear.
|You can test the DialogBox function using the Windows Script File Driver and HTML dialog box templates.|
In this Daily Drill Down, you’ve learned how to make your reusable DialogBox function for VBScript work with its associated files—the Windows Script File Driver file and the HTML dialog box file. Using these concepts, you now have the tools necessary to design your own customizable dialog boxes using Windows Script Host.
I’d like to thank Gunter Born for his coverage of how to access IE and its objects in his book Microsoft Windows Script Host 2.0 Developer’s Guide. You can find more information about this book and Mr. Born on his Windows Script Host Bazaar Web site. It was a great aid in helping me design this function.