Hi I\'m attempting to create a Message box with a \"DELAY\" button If the user does not press the Delay button the script will launch a batch file (or command)
So it
Challenge: accepted. Save this as a .bat file and run it.
@if (@CodeSection == @Batch) @then
@echo off
setlocal
set "task=cmd.exe"
set "timeout=60"
rem // Is %task% running?
tasklist /fi "imagename eq %task%" | find /i "%task%" >NUL && (
rem // Re-launch script with JScript interpreter
wscript /e:JScript /nologo "%~f0" %timeout% || (
rem // If timeout or user hits No, kill %task%
taskkill /im "%task%" /f
)
)
rem // End main runtime
goto :EOF
rem // Begin JScript portion
@end
var osh = WSH.CreateObject('WScript.Shell'),
noise = WSH.CreateObject('WMPlayer.OCX.7'),
nag = 'Greetings! Your administrator has requested you to log out of this '
+ 'application after work. It appears you are still using the program. If '
+ 'you are, in fact, not at your computer, please ignore this message.\n\n'
+ 'Otherwise, press Yes to continue working, or No to go ahead and close the '
+ 'application. This message will self-destruct in ' + WSH.Arguments(0)
+ ' seconds.';
with (noise) {
URL = osh.Environment('Process')('SYSTEMROOT') + '\\Media\\Windows Exclamation.wav';
Controls.play();
}
popup = osh.Popup(nag, WSH.Arguments(0), 'Are you still here?', 0x4 + 0x20 + 0x1000);
WSH.Quit(popup - 6);
The script employs JosefZ's suggested WshShell.Popup()
method, and is pretty straight forward. It uses a hybrid convention for including JScript within a batch script without having to use secondary / temporary files. It also employs conditional execution to evaluate the exit code of find
and wscript
.
The WMPlayer.OCX.7
idea to play a sound came from this answer.
Edit: I saw your edit above and your struggle to include ^G
in your paste. Check this out. You can capture a beep to a variable like this:
@echo off
setlocal
for /f %%I in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo 0x07"') do set "beep=%%I"
set /P "=%beep%"
... will beep 3 times without echoing a new line.