Hi Derz, this script will shut down at the hour:min am/pm specified of the current day. Simply edit the shutDownHour, shutDownMin, and AM. The timer is only set once instead of checking at a certain interval. (Although some might note I didn't bother setting it to be a negative value since your computer isn't going to be checking if it is shut down.) If you are going to be changing this time on a regular basis or make it more user friendly then a gui and an initfile should be added on.
The drawback: You must run the script every day that you want an automatic shutdown. In other words what SifJar said is true
You're probably better off using Task Scheduler if you can get it to work
The only advantage I can see of using a script is if you are going to change the time frequently and turn it on or off frequently. In that case, you would probably want to use task scheduler
anyway to start the script on startup, and the additional gui and initfile would need to be created.
Code: Select all
#Persistent
; Edit these to reflect the shut down time required
shutDownHour:=10
shutDownMin:=0
AM:=false ; if am is false then it is pm, and if it is true it is am
;;;
if((timeToShutDown:= calcTimeTo(shutDownHour, shutDownMin, AM)*-1) = 0)
MsgBox, The time is already passed!
else {
; MsgBox, % timeToShutDown
setTimer, ShutCompDown, %timeToShutDown%
}
return
ShutCompDown:
MsgBox, 1,, Shutting Down Now, 5
IfMsgBox, Cancel
return
Run, %comspec% /c shutdown /p /f
return
; calcTimeTo returns the number of milliseconds till the hour, min, am|pm of the current day
; if the time is already passed returns 0
calcTimeTo(hr, min, isAm=true)
{
if(!isAm)
hr += 12
minAmount:=min-a_min
hrAmount:=hr-a_hour
if (minAmount < 0) {
minAmount+=60
hrAmount-=1
}
return ((amount:=(hrAmount*60+minAmount)*60000) > 0 ? amount : 0)
}
EDIT: oops was missing a couple parenthesis there
Not trying to make you lose your work... Should be fine now, and I added a messagebox that will anounce shutdown that times out after 5 secs, that way a person can cancel.