Export (0) Print
Expand All

Displaying Timed Message Boxes

Microsoft® Windows® 2000 Scripting Guide

In a perfect world, each time you ran a script the script would encounter optimal conditions and be able to quickly and easily complete its appointed task. In the real world, of course, things do not always work quite so smoothly. Sometimes you will run a script and encounter a decision point; for example, you might try to connect to a remote computer, only to find that the connection cannot be made. When that occurs, a decision must be made: Should the script try again, should it ignore the problem, or should it simply give up?

Depending on your needs, you might make the decision ahead of time and include code that instructs the script what to do: If you cannot make the connection, try again. At other times, you might prefer to be notified that a problem occurred and then make the decision yourself. If so, you need a way to be notified that a decision needs to be made, as well as a way to make that decision.

Your script can use the WshShell Popup method to display a message box with a variety of buttons and return a value that indicates which of the buttons the user clicked. For example, you can display a message box with Yes and No buttons, and your script can take the appropriate action based on the button clicked by the user: Try to make the connection if the user clicks Yes; terminate the script if the user clicks No.

In addition to providing users with multiple-choice options, you can also supply the Popup method with a parameter that forces it to time out after a given number of seconds as well as change the icon and title of the message box. By contrast, Wscript.Echo message boxes do not have an icon and always have the title Windows Script Host.

A sample message box created using the Popup method is shown in Figure 3.13.

Figure 3.13 Message Box Created by the Popup Method

sas_wsh_116s

Comparing the Echo and Popup Methods

If you run your script under Wscript, you can use Wscript.Echo instead of the WshShellPopup method to display messages in a message box. However, with Wscript.Echo, users can only click the OK button or do nothing. Wscript.Echo does not enable you to present users with multiple choices.

In addition, your script is paused while the Wscript.Echo message is displayed. Only after the user clicks the OK button does the script proceed. In cases where no user acknowledges the message, Wscript.Echo pauses your script indefinitely or until it times out (if a time-out has been set). If you want your script to display messages in GUI message boxes as it progresses, but continue regardless of whether a user clicks the OK button, you cannot use Wscript.Echo.

As shown in Figure 3.14, the message box displayed by Wscript.Echo has a single OK button. The user can either do nothing and keep the script waiting or click the OK button, allowing the script to continue.

Figure 3.14 Message Box Produced by Using Wscript.Echo Under WScript

sas_wsh_118s

Note

  • Unlike Wscript.Echo, the Popup method always displays a message box, regardless of whether a script is running under CScript or WScript.

Creating a Message Box That Times Out

The script in Listing 3.32 uses the WScript Popup method to create three messages in message boxes, each of which has a single OK button. Each message box will be displayed on the screen for a maximum of 5 seconds. If no one has clicked the OK button after 5 seconds, the message box will automatically dismiss itself from the screen.

Listing 3.32 Displaying Timed Progress Message Boxes

  
1
2
3
4
5
6


Const TIMEOUT = 5
Set objShell = WScript.CreateObject("WScript.Shell")

objShell.Popup "Disk Report Complete", TIMEOUT
objShell.Popup "Memory Report Complete", TIMEOUT
objShell.Popup "CPU Report Complete", TIMEOUT


Timed message boxes are useful in at least two instances. For one, they enable you to provide a sort of graphical progress indicator without interrupting the flow of the script. For example, the script shown in Listing 3.32 can be incorporated within a script that actually generates a disk report, a memory report, and a CPU report.

As each report is completed, you might want to display a message box notifying users of the current status. If this message box were displayed using the Echo method, the message box would remain on the screen, and the script would remain paused until someone clicked OK. With the Popup method, the message remains on the screen either until someone clicks OK or until the time-out period expires. In the preceding script, the message appears and the script pauses for no more than 5 seconds before continuing.

Along similar lines, you might want to give users the opportunity to make a decision but, if no decision is immediately forthcoming, allow the script to follow a default course of action. For example, you might have a script that carries out a number of activities and then copies a set of files across the network. Because this copying procedure might take considerable time and bandwidth, you can display a pop-up message box asking the user whether he or she wants to proceed with the copying. The message can could be displayed for a minute or so; if there is no response, the script can automatically begin copying files.

Choosing Icons and Buttons

The WshShell Popup method enables you to create message boxes with various sets of buttons and icons. For example, you can create a message box with Yes and No buttons or a message box with the button set Abort, Retry, Ignore. In addition, you can determine which button a user clicked and then take appropriate action based on the user choice. This helps differentiate the Popup method from the Echo method; message boxes displayed using Echo have only an OK button.

You specify both the button set and the icon by providing the Popup method with a fourth parameter. This parameter accepts a combination of predefined constants that specify the button set and the icon the message box should use.

Table 3.17 lists the icons available to use with the Popup method along with their corresponding constants.

Table 3.17 Constants for Icons

Icon

Constant Name

Constant Value

STOP

vbCritical

16

QUESTION MARK

vbQuestion

32

EXCLAMATION MARK

vbExclamation

48

INFORMATION

vbInformation

64


Table 3.18 lists the button sets available to use with the Popup method along with their corresponding constants.

Table 3.18 Constants for Button Sets

Button Set

Constant Name

Constant Value

OK

vbOKOnly

0

OK and CANCEL

vbOKCancel

1

ABORT, RETRY and IGNORE

vbAbortRetryIgnore

2

YES, NO and CANCEL

vbYesNoCancel

3

YES and NO

vbYesNo

4

RETRY and CANCEL

vbRetryCancel

5


Although you can use either the constant names or the constant values within a script, using the constant names makes it much easier to understand the code. For example, it is relatively easy to see that the following line of code creates a pop-up message box with Yes and No buttons. (This message box also has a time-out value of 10 seconds and the title Popup Example.)



objShell.Popup "Stop Icon / Abort, Retry and Ignore Buttons", _
     10, "Popup Example", vbYesNo


To display both an icon and a button set, use two constants (joined by a plus sign) in the code:



objShell.Popup "Stop Icon / Abort, Retry and Ignore Buttons", _
     10, "Popup Example", vbCritical + vbYesNo


The script in Listing 3.33 displays a series of message boxes, each of which has a different icon and button set. If you click any button on a message box, the next message box will be displayed; otherwise, the script displays each message box for five seconds.

Listing 3.33 Displaying Combinations of Icons and Button Sets

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14


Const TIMEOUT = 5
Const POPUP_TITLE = "Icons and Buttons"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Popup "Stop Icon / Abort, Retry and Ignore Buttons", _
 TIMEOUT,POPUP_TITLE,vbCritical+vbAbortRetryIgnore
 
objShell.Popup "Question Mark Icon / Yes, No and Cancel Buttons", _
 TIMEOUT,POPUP_TITLE,vbQuestion+vbYesNoCancel
 
objShell.Popup "Exclamation Mark Icon / Yes and No Buttons", _
 TIMEOUT,POPUP_TITLE,vbExclamation+vbYesNo
 
objShell.Popup "Information Icon / Retry and Cancel Buttons", _
 TIMEOUT,POPUP_TITLE,vbInformation+vbRetryCancel


In lines 414, the WshShell Popup method is called four times in succession to display pop-up message boxes with various icons and button sets. The third parameter passed to Popup is always the POPUP_TITLE constant and results in each pop-up message box having Icons and Buttons as its title. The fourth parameter is passed various constants representing both the icon and the button set to be used. Note that the constants are combined by using the plus (+) operator.

Choosing the Default Button

The WshShell Popup method lets you specify the default button when you create a message box. The default button is the button that has focus and will be chosen if the user presses ENTER.

It is not unusual for users to reflexively press ENTER any time a message box is displayed. Because of that, some care should be taken when choosing the default button. For example, you might choose the button that, 9 times out of 10, users will select anyway. Or you might choose the button that is the "safest" should a user press ENTER without realizing what they are doing. For example, your message box might prompt the user, "Are you sure you want to delete all the files on this hard drive?" In a case such as this, you might want to configure No as the default button. That way, no damage will be done if a user accidentally presses ENTER.

You can specify which button to make the default by adding another constant to the fourth parameter of the Popup method. Table 3.19 lists the constants you can use to set the default button in a message box. If you try to set the second or third button as the default when the message box does not have a second or third button, the default button constant you specify will be ignored and the left button will be set as the default.

Table 3.19 Constants for Default Button Locations

Default Button

Constant Name

Constant Value

LEFT

vbDefaultButton1

0

MIDDLE

vbDefaultButton2

256

RIGHT

vbDefaultButton3

512


The script in Listing 3.34 displays two message boxes in succession. Both message boxes use the Abort, Retry, Ignore button set. The first message box does not specify the default button, so the leftmost button, Abort, is automatically selected as the default. The second message box uses the vbDefaultButton2 constant to make Retry the default button.

Listing 3.34 Setting Retry as the Default Button

  
1
2
3
4
5
6
7
8


Const TIMEOUT = 5
Set objShell = WScript.CreateObject("WScript.Shell")

objShell.Popup "Abort, Retry, Ignore. No Default Specified." _
 ,TIMEOUT,,vbAbortRetryIgnore
 
objShell.Popup "Abort, Retry, Ignore. Retry Set as Default." _
 ,TIMEOUT,,vbAbortRetryIgnore+vbDefaultButton2

Retrieving User Input

One of the advantages of using the Popup method rather than the Echo method is that you can give users the chance to make a choice: Yes, I want to try again; no, I would rather just quit. This means that the script needs to determine which button a user has clicked. The Popup method returns an integer that you can compare with a set of constants to determine which button was clicked. If a message box times out, the Popup method returns 1.

Table 3.20 lists the values you can use to identify which button a user has clicked. If the return value of the Popup method is equal to one of these constants, the user has clicked the associated button. Within a script, you can check either for the value of the constant or for the constant itself. For example, these two lines of code both check to see whether the OK button was clicked:



If intClicked = 1
If intClicked = vbOK

Table 3.20 Button Constants

Value

Constant

Button Clicked

1

VbOK

OK

2

VbCancel

Cancel

3

VbAbort

Abort

4

VbRetry

Retry

5

VbIgnore

Ignore

6

VbYes

Yes

7

VbNo

No


The script in Listing 3.35 displays a message box that uses the Yes, No button set to determine whether the user would like more detailed information.

The script uses the FileSystemObject to present the user with information about the host the script is running under. The script determines and displays the version of the script host file and uses the Popup method to allow the user to decide whether or not they would like to see more details about the file.

Listing 3.35 Retrieving User Input from a Message Box

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


Const TIMEOUT = 7
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFS = WScript.CreateObject("Scripting.FileSystemObject")

strPath = Wscript.FullName
strFileVersion = objFS.GetFileVersion(strPath)

iRetVal = objShell.Popup(Wscript.FullName & vbCrLf & _
 "File Version: " & _
 strFileVersion & vbCrLf & _
 "Would you like more details?" _
 ,TIMEOUT,"More Info?",vbYesNo + vbQuestion)
 
Select Case iRetVal
 Case vbYes
 Set objFile = objFS.GetFile(strPath)
 objShell.Popup WScript.FullName & vbCrLf & vbCrLf & _
 "File Version: " & strFileVersion & vbCrLf & _
 "File Size: " & Round((objFile.Size/1024),2) & _
 " KB" & vbCrLf & _
 "Date Created: " & objFile.DateCreated & vbCrLf & _
 "Date Last Modified: " & objFile.DateLastModified & _
 vbCrLf,TIMEOUT
 Wscript.Quit
 Case vbNo
 Wscript.Quit
 Case -1 
 WScript.StdOut.WriteLine "Popup timed out."
 Wscript.Quit
End Select


Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft