A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown Topic is solved
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
I want a message box that shows a message inside the box (not the window title) a countdown of 12 seconds, and every second that passes it updates the time remaining?
The title is : Shutdown Alert
The messagebox contents:
(there is a caution exclamation yellow color symbol) followed by "System will auto-shutdown in (12,11,10....) seconds. Press cancel/close button to abort, Press OK to shutdown immediately."
The title is : Shutdown Alert
The messagebox contents:
(there is a caution exclamation yellow color symbol) followed by "System will auto-shutdown in (12,11,10....) seconds. Press cancel/close button to abort, Press OK to shutdown immediately."
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Code: Select all
SetTimer, Countdown, 1000
Timeout := 12
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770") {
SetTimer, Countdown, Off
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1)
SetTimer, Countdown, Off
return
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
I don't understand why I'm getting this simple thing wrong but the computer is shutting down even If I press cancel. Why is that?
Code: Select all
SetTimer, Countdown, 1000
Timeout := 12
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
IfMsgbox="Ok"
Shutdown, 1
IfMsgbox="Cancel" ; This isn't working. It's shutting down the system
return
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770")
{
SetTimer, Countdown, Off
If(!errorlevel)
Shutdown, 1 ; Is this "if block" needed?
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1)
SetTimer, Countdown, Off
return
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Your syntax for IfMsgBox is wrong. See the documentation for the correct syntax and examples.
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
I don't know why after the countdown the msgbox doesn't appear
Code: Select all
SetTimer, Countdown, 1000
Timeout := 12
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
IfMsgbox, Ok
Msgbox Success ; means it will shut down
IfMsgbox, Cancel
return
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770")
{
SetTimer, Countdown, Off
If(sec=1) ; I even tried If(!errorlevel) and still doesn't work
Msgbox Success ; means it will shut down
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1)
SetTimer, Countdown, Off
return
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Raghava Doregowda wrote: ↑ I don't know why after the countdown the msgbox doesn't appear
Code: Select all
SetTimer, Countdown, 1000
Timeout := 12
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
IfMsgbox, Ok
Msgbox Success ; means it will shut down
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770") {
SetTimer, Countdown, Off
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1) {
SetTimer, Countdown, Off
Msgbox Success ; means it will shut down
}
return
Code: Select all
SetTimer, Countdown, 1000
Timeout := 12
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
IfMsgBox, TIMEOUT
Msgbox Success ; means it will shut down
else IfMsgbox, Ok
Msgbox Success ; means it will shut down
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770") {
SetTimer, Countdown, Off
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1)
SetTimer, Countdown, Off
return
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
One more thing, if I have to have a timer for say Half an hour, I don't want the message box to say 1800 seconds which is difficult to understand how many minutes it is. Is there a way to convert the seconds countdown to hours:minutes:seconds countdown?boiler wrote: ↑31 Jan 2023, 17:19Raghava Doregowda wrote: ↑ I don't know why after the countdown the msgbox doesn't appearOr:Code: Select all
SetTimer, Countdown, 1000 Timeout := 12 Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout IfMsgbox, Ok Msgbox Success ; means it will shut down return Countdown: if !WinExist("Shutdown Alert ahk_class #32770") { SetTimer, Countdown, Off return } ControlGetText, Msg, Static2 RegExMatch(Msg, "\d+", Sec) ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1) if (Sec = 1) { SetTimer, Countdown, Off Msgbox Success ; means it will shut down } return
Code: Select all
SetTimer, Countdown, 1000 Timeout := 12 Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout " seconds. Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout IfMsgBox, TIMEOUT Msgbox Success ; means it will shut down else IfMsgbox, Ok Msgbox Success ; means it will shut down return Countdown: if !WinExist("Shutdown Alert ahk_class #32770") { SetTimer, Countdown, Off return } ControlGetText, Msg, Static2 RegExMatch(Msg, "\d+", Sec) ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1) if (Sec = 1) SetTimer, Countdown, Off return
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
From Example #3 in the FormatTime documentation:
Code: Select all
MsgBox % FormatSeconds(7384)
FormatSeconds(s) {
t := 19990101
t += s, seconds
FormatTime, mmss, %t%, mm:ss
return s // 3600 ":" mmss
}
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
$Media_Next::
{
Send {Media_Next}
keywait, Media_Next, T1
If(Errorlevel)
{
IfWinNotExist, Shutdown Alert
{
IF FileExist("timer.ini")
{
IniRead, timeout_min, timer.ini, Urls, timeout_min
IniRead, timeout_sec, timer.ini, Urls, timeout_sec
}
Gui, Add, Text, x88 y112 w96 h28 , MINUTES
Gui, Add, Text, x88 y199 w96 h28 , SECONDS
Gui, Add, Edit, x242 y112 w57 h19 vtimeout_min, %timeout_min%
Gui, Add, Edit, x242 y199 w57 h19 vtimeout_sec, %timeout_sec%
Gui, Add, Text, x146 y7 w144 h38 , ENTER THE TIMEOUT FOR SHUTDOWN
Gui, Add, Button, x260 y300 w124 h38 gGuiClose, CANCEL
Gui, Add, Button, x109 y300 w124 h38 gGuiSubmit, OK
Gui, Show, w479 h377, Shutdown Alert
keywait, Media_Next, T1.5
If(Errorlevel)
{
Gui, destroy
goto set_timer
}
return
GuiClose:
Gui, destroy
return
Guisubmit:
Gui, submit, nohide
IniWrite,% timeout_min, timer.ini, Urls, timeout_min
IniWrite,% timeout_sec, timer.ini, Urls, timeout_sec
Gui, destroy
set_timer:
timeout:=(timeout_min*60)+timeout_sec
Gui,destroy
SetTimer, Countdown, 1000
Msgbox, 49, Shutdown Alert, % "System will auto-shutdown in " Timeout ". Press Cancel button to abort. Press OK to shutdown immediately.", % Timeout
IfMsgBox, TIMEOUT
Msgbox, Timeout
;Shutdown 8 ; means it will shut down
else IfMsgbox, Ok
Msgbox, Timeout
;Shutdown 12 ; means it will shut down
return
Countdown:
if !WinExist("Shutdown Alert ahk_class #32770")
{
SetTimer, Countdown, Off
return
}
ControlGetText, Msg, Static2
RegExMatch(Msg, "\d+", Sec)
ControlSetText, Static2, % RegExReplace(Msg, "\d+", Sec - 1)
if (Sec = 1)
SetTimer, Countdown, Off
return
}
}
}
FormatSeconds(s) {
t := 19990101
t += s, seconds
FormatTime, mmss, %t%, mm:ss
return mmss
}
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
You are not understanding what RegExMatch does. It can’t convert seconds to minutes:seconds. You have to call the function I showed and use the returned result. You can’t just plop the function at the bottom of your script and never call it and it expect it to do anything.
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Yes. but if regexmatch has to analyze the text in the msgbox for each second, How do I make it read the time left so that I can accordingly reduce the time by one second and whenever timeout=0 the minute reduces by one?boiler wrote: ↑13 Aug 2023, 07:21You are not understanding what RegExMatch does. It can’t convert seconds to minutes:seconds. You have to call the function I showed and use the returned result. You can’t just plop the function at the bottom of your script and never call it and it expect it to do anything.
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown
Which are you asking about? How to read the number of seconds left (which it already does, but in a very roundabout manner) or displaying the time in min:sec?Raghava Doregowda wrote: ↑ Yes. but if regexmatch has to analyze the text in the msgbox for each second, How do I make it read the time left...
You don't have it reduce the minutes by one. You call the function I provided so it returns min:sec format, and that's what you display. And don't read from the display to keep track of the total seconds. Keep it in a variable.Raghava Doregowda wrote: ↑ ...so that I can accordingly reduce the time by one second and whenever timeout=0 the minute reduces by one?
-
- Posts: 130
- Joined: 06 Nov 2022, 01:48
Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown Topic is solved
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetTitleMatchMode, 2
$Media_Next::
{
Send {Media_Next}
keywait, Media_Next, T1
If(Errorlevel)
{
IfWinNotExist, Shutdown Alert
{
IfWinNotExist, %A_ScriptName% ahk_class AutoHotkeyGUI
{
IF FileExist("timer.ini")
{
IniRead, cnt_min, timer.ini, Urls, cnt_min
IniRead, cnt_sec, timer.ini, Urls, cnt_sec
}
Gui, Add, Text, x88 y112 w96 h28 , MINUTES
Gui, Add, Text, x88 y199 w96 h28 , SECONDS
Gui, Add, Edit, Number x242 y112 w57 h19 vcnt_min, %cnt_min%
Gui, Add, Edit, Number x242 y199 w57 h19 vcnt_sec, %cnt_sec%
Gui, Add, Text, x146 y7 w144 h38 , ENTER THE TIMEOUT FOR SHUTDOWN
Gui, Add, Button, x260 y300 w124 h38 gGuiClose, CANCEL
Gui, Add, Button, x109 y300 w124 h38 gGuiSubmit, START COUNTDOWN
Gui, Show, w479 h377, Shutdown Alert
keywait, Media_Next, T1.5
If(Errorlevel)
{
Gui, destroy
goto set_timer
}
return
Guisubmit:
Gui, submit, nohide
if(cnt_min=0) and (cnt_sec=0)
{
Msgbox,49,CAUTION,You have entered 0:00. Instant shutdown will commence. Proceed?
ifmsgbox, Ok
goto ok
else
return
}
if(cnt_sec>59)
{
Msgbox,16,ERROR,Invalid entry- Seconds. Try again with a value lesser than 60.
return
}
ok:
cnt_min:=cnt_min+0
cnt_sec:=cnt_sec+0
IniWrite,% cnt_min, timer.ini, Urls, cnt_min
IniWrite,% cnt_sec, timer.ini, Urls, cnt_sec
Gui, destroy
set_timer:
Gui, Color, FFFF00
Gui, Font, s25 w1000 q1000, Courier New ;Microsoft Sans Serif
Gui, Add, Text, vCountdown w100, System will shutdown in %countdown%
Gui, Add, Button,, Shutdown Immediately
Gui, Add, Button,gGuiclose, Cancel
Gui, Show, h350 w500
SetTimer, Go, 1000
return
Go:
{
if(cnt_sec<10) and (cnt_min=0)
Gui, Color, FF0000
if(cnt_sec<10)
cnt_mid:=0
else
cnt_mid:=""
GuiControl,,Countdown, System will shutdown in %cnt_min%:%cnt_mid%%cnt_sec%
if(--cnt_sec<0) and (cnt_min!=0)
{
cnt_sec:=59
cnt_min:=cnt_min-1
}
if (cnt_min = 0) and (cnt_sec=-1)
{
SetTimer, Go, off
sleep 1000
shutdown, 12
}
return
}
ButtonShutdownImmediately:
shutdown, 8
Guiclose:
{
SetTimer, Go, off
Gui, destroy
return
}
}
}
}
}