Popup Dialog Box
The Windows Script Host as of this writing has limited ways to interact with the user. One of these is to use a popup dialog box. It is similar to the Win32 Message Box.
var rslt = WshShell.Popup(strText, [natSecondsToWait], [strTitle], [natType]);
There are two ways to call a popup
First with only the message as a parameter.
Second specifying all parameters.
I will begin with using all the parameters so that you can see what parameters can be used.
The variables are used to add clearity to this example, you can of course enter the parameters differently into the function call.
function showit(){
var Message = "This is my first popup";
var timeout = 0;
var title = "This is the title";
var buttons = 65;
var rslt;
var WS = WScript.CreateObject("WScript.Shell");
rslt = WS.Popup(Message, timeout, title, buttons);
if ( rslt == 2 ) {
WScript.Quit;
}
}
This function produces the following message box.
By changing the value of the variable buttons you can display different button combinations and icons. Please refer to the programmers reference for more information.
Buttons
0 OK
1 OK and Cancel
2 Abort, Retry, and Ignore
3 Yes, No, Cancel
4 Yes and No
5 Retry and Cancel
Icon Types
16 Stop Mark icon
32 Question Mark icon
48 Exclamation Mark icon
64 Information Mark icon
Return Values of Buttons
1 OK button
2 Cancel button
3 Abort button
4 Retry button
5 Ignore button
6 Yes button
7 No button
If you are unfamilar with using the Win32 Popup Dialog box conventions, here is a nutshells worth.
- Decide what buttons you wish to show on the popup and find the value listed above.
- Decide what icon you want to display and find its value from the icons types listed above.
- Add the two numbers together - this is the number you enter.
In the example that I used, I set the value to 65
1 = OK and Cancel
64 = Information Mark icon
65 = Total
Determine what buttons was press.
In the example the popup function call returns a value which is stored the in variable rslt.
rslt = WS.Popup(Message, timeout, title, buttons);
The variable can then be tested for which button has been pressed.
Please refer to the table above.
Close Popup without user action
You can also set the Popup box to terminate after a specified time period. This value is set with the variable timeout in the example.
Setting the value to zero essentially disables this feature. A value greater than zero specifies how many seconds it will wait before it terminates. When it terminates under a time out condition, the popup returns a value of -1.
This is useful if you are writing admin or utility scripts that will take a default course of action if nothing is clicked.