Display alerts and questions from scripts with messagebox

messagebox.200.175Build a batch file of any complexity and there’s a chance you’ll need to interact with the user, either to ask a question or display a message.

Messagebox is an open-source solution which delivers the basics you need, has no dependencies, and runs on anything from Windows NT 4.0 up.

The syntax looks like this:

messagebox message title [type]

You could use the program to display a simple alert.

messagebox "About to start deleting junk files", "Warning"

What’s more interesting is the optional "type", which defines the icon displayed, the buttons used, and which button is the default.

To set it up, you would find the settings you need from this list and add up those values (yes, it’s just a wrapper for the standard API function).

set MB_ABORTRETRYIGNORE=2
set MB_CANCELTRYCONTINUE=6
set MB_OK=0
set MB_OKCANCEL=1
set MB_RETRYCANCEL=5
set MB_YESNO=4
set MB_YESNOCANCEL=3

rem Icon.
set MB_ICONEXCLAMATION=48
set MB_ICONWARNING=48
set MB_ICONINFORMATION=64
set MB_ICONASTERISK=64
set MB_ICONQUESTION=32
set MB_ICONSTOP=16
set MB_ICONERROR=16
set MB_ICONHAND=16

rem Default button.
set MB_DEFBUTTON1=0
set MB_DEFBUTTON2=256
set MB_DEFBUTTON3=512
set MB_DEFBUTTON4=768

rem Other options.
set MB_SETFOREGROUND=65536

rem Result ERRORLEVELs.
set IDOK=1
set IDCANCEL=2
set IDRETRY=4
set IDABORT=3
set IDIGNORE=5
set IDYES=6
set IDNO=7
set IDTRYAGAIN=10
set IDCONTINUE=11

For example, using Retry and Cancel buttons gives us a value of 5; setting Retry as the default is a value of 4; an exclamation mark icon is 48. That’s a total of 57, and we’d use it in a line like this:

messagebox "We’ve failed to do something important", "Horrible Error", 57

Checking ErrorLevel in the batch file would then give us 4 if the user selected Retry, 2 for Cancel.

This delivered good results for us, and is certainly versatile, but if you don’t need all the button options there are simpler solutions around. If you’re running Windows 10 try ToastNotification to display your message as a standard desktop alert.

Messagebox requires Windows NT 4.0 or later.

Comments are closed.

© 1998-2024 BetaNews, Inc. All Rights Reserved. Privacy Policy - Cookie Policy.