Code: Select all
#Persistent
#SingleInstance, Force
#NoEnv
#NoTrayIcon
ListLines, Off
SetBatchLines,-1
ScriptName = System Timer
TimerRunning := 0
;------( Gui )----------------------------------------------------------------
#SingleInstance,force
#SingleInstance,force
Gui,Add,Text,x18 y141 w410 h13 Center,Your chosen action will be executed in
Gui,Font,norm s40,Lucida Console
Gui,Add,Text,x15 y164 w410 h61 vTimer_Update Center,0:30:00
Gui,Font
Gui,Add,Button,x20 y231 w90 h23 gTimer_Start,Start
Gui,Add,Button,x120 y231 w90 h23 gTimer_Pause,Pause
Gui,Add,Button,x220 y231 w90 h23 gTimer_Stop,Stop
Gui,Add,Button,x320 y231 w90 h23 gTimer_Exit,Exit
Gui,Font,norm s20,Arial
Gui,Add,Text,x80 y3 w300 h50,Auto Idle and Shutdown
Gui,Font,norm bold c0xFF0000 s12,Arial
Gui,Add,Text,x139 y38 w209 h22,Press escape to exit
Gui,Font
Gui,Add,Groupbox,x10 y57 w420 h76,Set Timer:
Gui,Add,Edit,x21 y76 w399 h20 vTimer_Minutes gTimer_Preview,30
Gui,Add,DropDownList,x20 y103 w400 vTimer_Action,Sleep|Hibernate|Reboot|Shutdown|
Gui,Show,x718 y275 w440 h274 ,
return
;------( Convert input into HH:MM:SS for prevew
Timer_Preview:
Gui, Submit, Nohide
If !! Timer
{
SetTimer, Timer_Start, Off
MsgBox, ,% ScriptName, Shutdown Timer Stopped. Restart timer with new time
Timer=
}
Timer_Update:="", Timer_UpdateLast:="", TimeSet:="", Flash:="", PauseTime:=""
SetFormat, Float, 0.0
TimeSet:=A_TickCount, TimeSet:=FormatSeconds(((Timer_Minutes*60*1000) - (A_TickCount - TimeSet))/1000)
If TimeSet = :00:00
GuiControl, Text, Timer_Update, 0:00:00
Else
GuiControl, Text, Timer_Update, % TimeSet
Return
;------( Start
Timer_Start:
If Pause
{
GoSub, Timer_Pause
Return
}
If ! TimerRunning ; Whether the Timer is ON
{
Gui, Submit, Nohide
If (!Timer_Minutes || !Timer_Action)
{
SetTimer, Timer_Start, Off
MsgBox, ,% ScriptName, No time or action input.
}
Else
{
TimeSet:=A_TickCount
Timer_Minutes:=(Timer_Minutes * 60) * 1000
SetTimer, Timer_Start, 50
TimerRunning := 1
SetTimer, Wiggle_Start, -0
}
}
Else
{
SetFormat, Float, 0.0
Timer_Update:=FormatSeconds((Timer_Minutes - (A_TickCount - TimeSet))/1000)
If (Timer_Update="" || Timer_Update <> Timer_UpdateLast)
{
GuiControl, Text, Timer_Update, % Timer_Update
Timer_UpdateLast:=Timer_Update
}
If % (A_TickCount - TimeSet) > Timer_Minutes
{
SetTimer, Timer_Start, Off
If Timer_Action=Sleep
DllCall("PowrProf\SetSuspendState", "int", 0, "int", 1, "int", 1)
If Timer_Action=Hibernate
DllCall("PowrProf\SetSuspendState", "int", 1, "int", 1, "int", 1)
If Timer_Action=Reboot
Shutdown, 6
If Timer_Action=Shutdown
Shutdown, 5
}
; Flash Window
If (Timer_Minutes - (A_TickCount - TimeSet))/1000 < 60
{
If ! Flash
WinSet, Alwaysontop, On, % ScriptName
If (Flash="" || (A_TickCount-Flash) > 400)
{
Gui, Flash
Flash:=A_TickCount
}
}
}
Return
;------( Pause
Timer_Pause:
If % Pause:= (Pause = "") ? "1" : ""
{
SetTimer, Timer_Start, Off
PauseTime:=A_TickCount
TimerRunning := 0
}
Else
{
TimeSet:=TimeSet+(A_TickCount-PauseTime), PauseTime:=""
SetTimer, Timer_Start, 50
TimerRunning := 1
}
Return
;------( Stop
Timer_Stop:
TimerRunning := 0
Reload
Return
;------( Exit
Timer_Exit:
ExitApp
Return
Wiggle_Start:
WinGetPos,,, desk_width, desk_height, Program Manager
Loop
{
if (!TimerRunning)
{
break
}
Random, MySleepTime, 1000, 4500
Random, posx, 1, desk_width
Random, posy, 1, desk_height
Random, MySpeed, 20, 100
MouseMove, posx, posy, MySpeed
}
return
Esc::
Gosub, Timer_Stop
return
;------( Functions )----------------------------------------------------------
FormatSeconds(NumberOfSeconds) {
Time = 19990101
Time += %NumberOfSeconds%, seconds
FormatTime, mmss, %time%, mm:ss
Return NumberOfSeconds//3600 ":" mmss
}
I would, however, recommend not using the Reload command to stop the timer. If the user moves the window somewhere, when you hit Esc, the window moves back to the default position.
I suppose you could store the window location in an INI file or something, but I would recommend trying to code it that you can stop the timer without reloading the whole script.