30
Let’s break this statement down into pieces and see how it works. First of all, the statement
executes a built-in VBScript function called
InputBox(). This function displays a pop-up dialog
box with a text entry field that allows the script to collect text input from the player.
The VBScript InputBox() function is just one of a number of options for col-
lecting input. The InputBox() function facilitates direct interaction with users.
When direct user interaction is not required, you can also develop VBScripts
that can read input from text files or the Windows Registry. I’ll show you how to
read data from text files in Chapter 8 “Storing and Retrieving Data” and how to
interact with the Windows Registry in Chapter 10 “Using the Windows Registry
to Configure Script Settings.” You can also create VBScripts that process data
passed to them at run-time. I’ll show you how this works in Chapter 4
“Constants, Variables, and Arrays.”
To communicate with the player, the InputBox() function allows you to display a message.
In the case of this example, the message is simply “Knock Knock,” but could just as easily be
“Hello, what is your name?” or any other question that helps the player understand the type
of information the script is trying to collect.
Finally, the text typed by the player in the pop-up dialog box’s text field is temporarily
assigned to a variable called
Reply1. Variables provide scripts with the capability to store and
later reference data used by the script.
Functions and variables are fundamental components of VBScript. Unfortunately, it is diffi-
cult to write even the simplest scripts without using them. For now, don’t worry too much
about them and keep your focus on the overall steps used to create and run the Knock Knock
game. I’ll go over the use of variables in great detail in Chapter 4, “Constants, Variables, and
Arrays,” and the use of functions in Chapter 7, “Using Procedures to Organize Scripts.”
Validating User Input
The player’s role in this game is to first type in the phrase “Who’s there?” Any variation in
spelling or case will result in an error. After the player has typed in this message and clicked
on the OK button, the script needs to perform a test that validates whether the player is play-
.
.
End If
If Reply1 <> “Who’s there?” Then MsgBox “Incorrect answer. Try again.”
It’s now time to add three lines of code that will reside within the second and third lines of
code just shown. The first of these three lines of code is as follows:
Reply2 = InputBox(“Panther!”)
This statement is very similar to the first statement in the script, except that instead of dis-
playing the message “Knock Knock,” it displays the message “Panther” and then waits for
the player to type in a response (that is, “Panther who?”). The text typed in by the player is
then stored in a variable named
Reply2.
31
Chapter 1 • Getting Started with the WSH and VBScript
32
Validating the User’s Last Response
The following two lines of code need to be inserted just after the previous statement:
If Reply2 = “Panther who?” Then _
MsgBox “Panther no panths I’m going swimming.”
If Reply2 <> “Panther who?” Then MsgBox “Incorrect answer. Try again.”
The first line checks to see if the value stored in Reply2 is equal to the phrase “Who is it?” and
if it is, then the rest of the statement displays the joke’s punch line. If the player typed in
something other than “Who is it?”, then the second of these two statements executes dis-
playing a message that informs the player that he or she did not provide the correct response.
The Final Result
Now let’s take a look at the fully assembled script.
Reply1 = InputBox(“Knock Knock!”)
If Reply1 = “Who’s there?” Then
Reply2 = InputBox(“Panther!”)
If Reply2 = “Panther who?” Then _
MsgBox “Greetings “ & UserName
This page intentionally left blank
Overview of the
Windows Script Host
2
CHAPTER
B
ecause VBScripts cannot execute without an execution host of some type,
the WSH is at the heart of any VBScript that you run from the Windows
desktop or command line. The WSH not only provides an environment in
which VBScripts can execute, but it also provides scripts with direct access to
Windows resources such as the Windows desktop, Start menu, Registry, event
logs, and network resources. To effectively create and execute VBScripts in this
environment, it’s essential to have a good understanding of the WSH core object
model. This includes knowing about the methods and properties associated with
WSH objects, as well as how to configure the WSH to best suit your needs. In this
chapter, you will learn
• About the objects that make up the WSH core object model
• How to use WSH object methods within your VBScripts
• How to use WSH object properties within your VBScripts
• How to configure the execution of the WScript and CScript execution
hosts
CHAPTER
36
Project Preview: The Rock, Paper, and Scissors Game
In this chapter, you will learn how to create a computer version of the Rock, Paper, and Scissors
game that you played as a child. The game begins by displaying its rules and then it asks the
player to choose between one of the three possible choices. After the player makes a selection,
the game randomly makes its own selection and displays the results. Figure 2.1 through 2.3
demonstrate the flow of the game.
other object models. For example, many Windows
applications, including Microsoft Office applications,
expose their own object models, allowing VBScript to
programmatically manipulate them. In addition,
other VBScript execution hosts, such as Internet Explorer, provide VBScript with access to
other object models. The WSH core object model is complex and may at first seem rather
daunting. As a result, you may not leave this chapter feeling 100 percent confident that you
fully understand it. But don’t worry—you’ll continue to develop your understanding of this
complex topic as you go through the rest of this book.
The Core Object Model
The WSH core object model provides programmatic access to Windows resources. There are
14 objects in the WSH core object model, as depicted in Figure 2.4. Each of these objects pro-
vides access to a particular category of Windows resources.
At the top, or
root
, of the WSH core object model is the WScript object. All other objects are
instantiated from this object. The
WScript object is automatically established during the
startup of the execution host and can therefore be referenced without first being instanti-
ated within your scripts. For example, let’s create a short one-line script called
Greeting.vbs.
37
Chapter 2 • Overview of the Windows Script Host
Definition
An object model is a representation
of a number of related objects that
provide a script or program with
the capability to view and interact
with each of the objects (files,
disks, printers, and so on) repre-
•
Echo() Displays a text message in the Windows Console or as a pop-up dialog
depending on which execution host runs the script.
•
Quit() Terminates a script’s execution.
•
Sleep() Pauses the execution of a script for a specified number of seconds.
The
WScript object is referred to as a public or exposed object. The WSH core object model
has three other public objects:
WshController, WshShell, and WshNetwork. Each of these three
objects must be instantiated within your scripts using the
WScript object’s CreateObject()
method. All the other objects in the WSH core object model can only be instantiated
by using properties or methods associated with the
WScript, WshController, WshShell, and
WshNetwork objects.
Table 2.1 lists the rest of the objects in the WSH core object model, as well as the object prop-
erties or methods required to instantiate them.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Definition
Instantiation describes the process
of creating a reference to an object.
To work with an object, you must
first create, or instantiate, a refer-
ence to it within your scripts.
Figure 2.5
A pop-up dialog
created using the
WScript object’s
at execution time.
Properties:
Count, Item, and Length, Named, and Unnamed.
Methods:
Count() and ShowUsage().
TABLE 2.2 WSH CORE O BJECTS
(continues)