How to Generate Popup Messages in Batch Files
Most messages in batch files consist of plain text in the command prompt.
We use ECHO to display a message text, and PAUSE or SET /P to wait for confirmation.
Sometimes, however, we would rather have a popup message that sits there waiting for confirmation by the user.
NET SEND
By far the easiest solution using native commands is NET SEND.
In NT 4 and later:
NET SEND %ComputerName% This is a popup message.
Notes: (1) The NET SEND command requires the Messenger service; if this service is disabled, no messages will be displayed.
(2) By default the %ComputerName% environment variable is not available in Windows 9x or the "real" MS-DOS versions.
Use NetSetxx.bat to add this variable in Windows 98.
Using carets for escape characters we can even make multi line messages in NT 4 and later:
NET SEND %ComputerName% This is line 1 of the popup message.ˆ
This is line 2.ˆ
And this is line 3.
Note the empty lines following the carets, these are required to make this trick work.
This code will result in a message box similar to this one:
This method can also be used with the ECHO command:
ECHO This is line 1 of the popup message.ˆ
This is line 2.ˆ
And this is line 3.
This code will result in the following text on screen:
This is line 1 of the popup message.
This is line 2.
And this is line 3.
I wouldn't recommend it, though, as it doesn't make the batch file any easier to read.
Better use 3 ECHO command in a row to display the 3 lines.
By the way, with NET SEND, you can get a similar result using ASCII character 20 or ¶ (trick found in the PCReview Forum Archives):
NET SEND %ComputerName% This is line 1 of the popup message.¶This is line 2.¶And this is line 3.
This may even work in Windows 95/98/ME, but I didn't test that.
This trick does not work with the ECHO command:
ECHO This is line 1 of the popup message.¶This is line 2.¶And this is line 3.
This code will result in the following text on screen:
This is line 1 of the popup message.¶This is line 2.¶And this is line 3.
Quite useless, unfortunately.
Notes: (1) In Windows 9x escape characters are not available.
(2) When using * instead of %ComputerName% to send a message to every computer in the network, the message length is limited to 128 characters.
Longer messages will be truncated.
MSG.EXE (Terminal Servers only)
In Terminal Server sessions, use the MSG command to generate popup messages.
Alternative Scripting Languages (On-The-Fly)
If you don't mind using a mix of scripting languages, consider creating a temporary KiXtart script on the fly, using the MessageBox( ) function to generate your popup message:
> usermessage.kix ECHO $MsgStr = "Multiple lines?@CRLF" + "No problem"
>> usermessage.kix ECHO $Title = "My Personal Message Box"
>> usermessage.kix ECHO $X = MessageBoxˆ( $MsgStr, $Title, 0 ˆ)
KIX32.EXE usermessage.kix
DEL usermessage.kix
This code will result in a message box similar to this one:
If you prefer a native scripting language (Windows 2000 and later), create a VBScript script on the fly, using the WScript.Echo method (requires WSCRIPT instead of CSCRIPT):
> usermessage.vbs ECHO WScript.Echoˆ( "Multiple lines?" ˆ& vbCrLf ˆ& "OK..." ˆ)
WSCRIPT.EXE usermessage.vbs
DEL usermessage.vbs
This code will result in a message box similar to this one:
With a little more code you can customize the message title, add an icon and set a timeout period:
> usermessage.vbs ECHO Set wshShell = CreateObject( "WScript.Shell" )
>>usermessage.vbs ECHO wshShell.Popup "Multiple lines are possible, a" ˆ& vbCrLf ˆ& _
>>usermessage.vbs ECHO "custom title, a timeout, and a" ˆ& vbCrLf ˆ& _
>>usermessage.vbs ECHO "selection of buttons and icons.", 10, _
>>usermessage.vbs ECHO "Read this quickly", 64
WSCRIPT.EXE usermessage.vbs
DEL usermessage.vbs
This code will result in a message box similar to this one, which will close automatically after 10 seconds:
Message boxes asking for user input are possible too:
> usermessage.vbs ECHO WScript.Echo InputBox( "Where were you born?", "Place of Birth", "London" )
FOR /F "tokens=*" %%A IN ('CSCRIPT.EXE //NoLogo usermessage.vbs') DO SET PlaceOfBirth=%%A
ECHO You were born in %PlaceOfBirth%
DEL usermessage.vbs
This code will result in a message box similar to this one:
Unfortunately, combining these input and output dialogs into a single dialog box isn't possible, to do that read the next section.
Internet Explorer
More advanced message boxes, input, output and any combination, including (masked) password prompts, can be created using VBScript and Internet Explorer.
However, I would not recommend creating them on the fly.
An example using IELogin2.vbs:
:: Note: delims is a TAB
FOR /F "tokens=1,2 delims= " %%A IN ('CSCRIPT //NoLogo IELOGIN2.VBS') DO (
SET Name=%%˜A
SET Password=%%˜B
)
ECHO The password of %Name% is %Password%
This is what the dialog box will look like:
See the VBScript Scripting Techniques section for other advanced user message
Using VBScript to display message boxes
In today's article, I'll show you how you can make really cool looking VBScript popup boxes to amaze and shock your friends and colleagues. Basically, with VBScript and a little time, you can create a Message box on any Microsoft Windows computer with a custom text of your choice.
This example was taken off the autoitscript website. You can create such message boxes with any text you want.
Here's how:
Step 1 - Open up a simple text editor, such as notepad.
Step 2 - Enter in the following line
msgbox"Have a nice day!",16,"Your title here"
You will replace the text in quotes "Have a nice day!" with a message of your choice (don't remove the quotes!)
The number after the message text is an indicator of what icon you may want to use.
Here are some examples :
• Icon number 16 is a ERROR sign, a circle with a cross
• Icon number 32 is a QUESTION mark sign "?"
• Icon number 48 is a WARNING sign, a triangle with a "!" on it
• Leaving it as a 0 will give no icon.
The last field is for the message title, which you can change to anything you want as well.
Step 3 - Save it as a VBS file. When saving, make sure you select "all files" and enter the extension ".VBS" (for example, your file may be named "hello.vbs")
Step 4- Double click to run. Simple as that.
The possibilities of what you can do with this are endless. Obviously you could pull some pranks on other people's computer by leaving messages such as "system failure" or anything you like. You can also use such messages to leave notes or alerts.
A little more fun: Adding this line
WScript.Sleep 5000
before the msgbox line will force the script to wait 5000 miliseconds before running and displaying the messagebox. In this way you can set a timer (or a fuse) to your script so it only shows after the specified time has passed.
I hope this tutorial has been useful to you. Have fun on your computer with VBScript!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment