A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Raghava Doregowda
Posts: 130
Joined: 06 Nov 2022, 01:48

A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by Raghava Doregowda » 29 Jan 2023, 04:15

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."

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 29 Jan 2023, 04:55

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

Raghava Doregowda
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

Post by Raghava Doregowda » 31 Jan 2023, 11:30

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

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 31 Jan 2023, 12:21

Your syntax for IfMsgBox is wrong. See the documentation for the correct syntax and examples.

Raghava Doregowda
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

Post by Raghava Doregowda » 31 Jan 2023, 15:10

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

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by RussF » 31 Jan 2023, 15:17

Please see example #1 in the IfMsgBox documentation.

Russ

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 31 Jan 2023, 17:19

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
Or:

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

Raghava Doregowda
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

Post by Raghava Doregowda » 30 Mar 2023, 04:13

boiler wrote:
31 Jan 2023, 17:19
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
Or:

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
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?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 30 Mar 2023, 05:37

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
}

Raghava Doregowda
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

Post by Raghava Doregowda » 13 Aug 2023, 07:06

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
}


I tried incorporating this but I can't seem to change the format in the message box. How do I write the regex match statement to accordingly display the format like min:sec.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 13 Aug 2023, 07:21

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.

Raghava Doregowda
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

Post by Raghava Doregowda » 13 Aug 2023, 11:17

boiler wrote:
13 Aug 2023, 07:21
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.
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?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: A messagebox that displays the countdown on it and shuts down if the messagebox finishes countdown

Post by boiler » 13 Aug 2023, 11:33

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...
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: ...so that I can accordingly reduce the time by one second and whenever timeout=0 the minute reduces by one?
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
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

Post by Raghava Doregowda » 15 Aug 2023, 12:08

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
     }
   }
  }
 }
}
I adopted GUI into the picture instead of a messagebox to avoid the alert sounds. One has to long press the media next button to activate the GUI to input minutes and seconds. If you continuously hold for around 4 seconds, the Shutdown timer starts working directly bypassing and closing the Input GUI. Anyone can use this script to customize the timer, along with a change of color from yellow to red when there's less than 10 seconds remaining. Cheers :D

Post Reply

Return to “Ask for Help (v1)”