Previous Section Table of Contents Next Section

Windows Script Host

The most common way to run scripts is to use WScript.exe, the graphical version of the Windows Script Host (WSH), which I introduced in Chapter 1. WScript is registered to handle common scripting file extensions, so simply double-clicking a .VB or .VBS file will normally execute WScript.exe, then ask it to execute the double-clicked script.

To see WScript in action, try this:

  1. Right-click your desktop and select New; then point to Text File.

  2. Rename the new text file to Sample1.vbs.

  3. Right-click the file and choose Edit. By default, Windows registered Notepad as the handler for the Edit action, so a blank Notepad window will open.

  4. Type WScript.Echo "Displaying Output" and save the file.

  5. Close Notepad.

  6. Create another new text file on the desktop, and name this one Sample2.vbs.

  7. Edit Sample2.vbs and enter the following:

    
    Wscript.Echo "Here we go..."
    
    
    
    Dim V
    
    V = InputBox("What is your name?")
    
    MsgBox "Hello, " & V
    
    

These aren't terribly complex scripts, but they'll serve to illustrate some important concepts. First, double-click Sample1.vbs. If your system is properly configured, you'll see a dialog box like the one in Figure 2.1. Click OK on the dialog box to dismiss it and end the first script. Now, double-click Sample2.vbs. It'll start with a similar dialog box, as shown in Figure 2.2. Then, as shown in Figure 2.3, you'll be prompted to enter your name.

Figure 2.1. Basic graphical dialog box from a script

graphics/02fig01.gif

Figure 2.2. Starting dialog box

graphics/02fig02.gif

Figure 2.3. Prompting you for your name

graphics/02fig03.gif

Finally, Figure 2.4 shows the last dialog box, which addresses you by name.

Figure 2.4. Addressing you by name

graphics/02fig04.gif

TIP

What you've just seen is the sum total of VBScript's user interface capabilities. If you were hoping to use VBScript to create complex dialog boxes and graphical controls, forget about it! You'll need a full programming language, like Visual Basic 6.0 or VB.NET, to create a more complex user interface.


What does all this buy you? First, you've experienced the type of graphical user interface that VBScript can provide: simple input and output. You can get a tad more complex and create dialog boxes with Yes and No buttons, or Abort, Retry, and Ignore buttons, but that's about the extent of it.

This script is simple enough that you should be able to figure out exactly what each line of code is doing. Wscript.Echo obviously displays a dialog box with some text in it, and was used in both scripts. Dim V creates a new variable named "V" (more on those in Chapter 5), and the InputBox function collects some information and places it into the variable. Finally, MsgBox seems to duplicate Wscript.Echo, displaying some specified information in a dialog box.

The big question on your mind is probably, "What's the difference between this Echo and that MsgBox?" There is a difference, although it's subtle.

    Previous Section Table of Contents Next Section