How Can I Automatically Dismiss a Message Box After a Specified Length of Time?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’d like to display a Yes/No message box for a few seconds and then, if no one clicks a button, have the message box disappear and the script perform the default operation. How do I do that?

— JR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JR. Sounds like you need the WSH Shell object and the Popup method. Popup works a lot like the VBScript Msgbox function: like Msgbox it displays various types of graphical message boxes (for example, one with Yes and No buttons; one with OK and Cancel buttons; etc.). There’s one big difference, however. When you display a message box using the Msgbox function, that message box stays on screen until someone clicks a button and dismisses it; if no one clicks a button, the message box will stay there forever. (And, by extension, the script will remain frozen in place.) With the Popup method, however, you can specify an optional timeout value; when the specified amount of time has elapsed, the message box automatically dismisses itself, and the script is free to resume operation.

Let’s take a look at a simple script that displays a message box (one with Yes and No buttons) and then reports whether the user clicked one of these buttons or whether the message box dismissed itself after the 10-second time limit was reached:

Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32

Set objShell = CreateObject(“Wscript.Shell”)

intReturn = objShell.Popup(“Do you want to delete this file?”, _ 10, “Delete File”, wshYesNoDialog + wshQuestionMark)

If intReturn = wshYes Then Wscript.Echo “You clicked the Yes button.” ElseIf intReturn = wshNo Then Wscript.Echo “You clicked the No button.” Else Wscript.Echo “The popup timed out.” End If

The script begins by defining a few constants. The first two – wshYes and wshNo – are used to determine which button the user clicked. Clicking the Yes button returns 6; clicking the No button returns 7. If the script times out and the popup dismisses itself, then -1 is returned. Later on in the script we’ll check for these values in order to determine which action to take.

The other two constants – wshYesNoDialog and wshQuestionMark – are used to construct the message box; wshYesNoDialog specifies a message box with Yes and No buttons, and wshQuestionMark indicates that the message box should also include a question mark icon. In fact, our finished message box will look an awful lot like this:

Popup Message Box

After defining the constants we create an instance of the WSH Shell object and then call the Popup method (with the return value to be assigned to the variable intReturn). For this script we’ve provided the Popup method with four parameters:

Parameter

Description

Do you want to delete this file?

Message box message.

10

Timeout value, in seconds. If no one clicks a button within 10 seconds, the message box will automatically dismiss itself.

Delete File

Message box title.

wshYesNoDialog + wshQuestionMark

Message box type: a message box with Yes and No buttons and a question mark icon.

When we call the Popup method the message box appears on screen. When the message box is dismissed – either because someone clicked a button or because the message box timed out – the return value is assigned to the variable intReturn. The script then checks the value of intReturn and reports whether the Yes button was clicked, whether the No button was clicked, or whether the message box timed out.

Hopefully this will do the trick for you. For a slightly more useful script, here’s one that asks whether or not you want to delete a file (C:\Scripts\Test.vbs). If you click No, the script terminates and the file is left untouched. However, if you click Yes or if the message box times out, the file is deleted.

Here’s the script:

Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32

Set objShell = CreateObject(“Wscript.Shell”) Set objFSO = CreateObject(“Scripting.FileSystemObject”)

intReturn = objShell.Popup(“Do you want to delete this file?”, _ 10, “Delete File”, wshYesNoDialog + wshQuestionMark)

If intReturn = wshNo Then Wscript.Quit End If

objFSO.DeleteFile(“c:\scripts\test.vbs”)


0 comments

Discussion is closed.

Feedback usabilla icon