Reputation: 11
Is there a way to add a timeout to the message box so if there no input it defaults to no?
I would like it to default to No after 1 hour
@echo off
Call :YesNoBox "Are you sure you want to do that?"
if "%YesNo%"=="7" (
Call :MessageBox "You answered NO" "Heading"
exit /b
)
exit /b
:YesNoBox
REM returns 6 = Yes, 7 = No. Type=4 = Yes/No
set YesNo=
set MsgType=4
set heading=%~2
set message=%~1
echo wscript.echo msgbox(WScript.Arguments(0),%MsgType%,WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set YesNo=%%a
exit /b
:MessageBox
set heading=%~2
set message=%~1
echo msgbox WScript.Arguments(0),0,WScript.Arguments(1) >"%temp%\input.vbs"
cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"
exit /b
Upvotes: 1
Views: 746
Reputation: 57252
Here's a jscript/bat hybrid script with yes/no buttons pop-up.It uses popup object which has a time-out option:
@if (@x)==(@y) @end /***** jscript comment ******
@echo off
cscript //E:JScript //nologo "%~f0" "%~nx0" %*
exit /b 0
@if (@x)==(@y) @end ****** end comment *********/
var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);
var timeout=-1;
var message="";
function printHelp() {
WScript.Echo(title + "[-title Title] [-timeout m] [-message \"pop-up message\"]");
WScript.Echo(title + "if time out not defined will wait only for button pressing");
}
if (WScript.Arguments.Length==1){
runPopup();
WScript.Quit(0);
}
if (args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 0 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(10);
}
for (var arg = 1 ; arg<args.Length;arg=arg+2) {
if (args.Item(arg).toLowerCase() == "-title") {
title = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-timeout") {
timeout = parseInt(args.Item(arg+1));
if (isNaN(timeout)) {
timeout=-1;
}
}
if (args.Item(arg).toLowerCase() == "-message") {
message = args.Item(arg+1);
}
}
function runPopup(){
var btn = wshShell.Popup(message, timeout, title, 0x4 + 0x20);
//WScript.Echo(btn)
switch(btn) {
// yes pressed.
case 6:
WScript.Echo("yes");
WScript.Quit(btn);
break;
// no pressed.
case 7:
WScript.Echo("no");
WScript.Quit(btn);
break;
// Timed out.
case -1:
WScript.Echo("timeout");
WScript.Quit(btn);
break;
}
}
runPopup();
to use it try something like :
for /f %%a in ('yesnopopup.bat -title "T.I.T.L.E." -timeout 5 -tom "timeout" -message "Hello"') do (
if "%%~a" equ "yes" echo YES PRESSED
if "%%~a" equ "no" echo NO PRESSED
if "%%~a" equ "timeout" echo TIMED OUT
)
I prefer using jscript/bat hybrids as it makes the code easier to read , they use a single file which reduces the i/o operations and everything is faster.
Upvotes: 1