Trigger script at certain time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Trigger script at certain time

05 Feb 2016, 16:39

Hi AHK experts,
I've been wondering if there is a way to start your script at a certain time. E.g. my script says this:

Code: Select all

Run, %comspec% /c shutdown /p /f
I want to shut down my computer every night. I tried triggering it with Task Scheduler, but haven't got that working yet. :? :problem: Can't AHK do it somehow? Should it be suggested for future updates??? :?: :idea:
Or is there information about it somewhere? :oops: I did look, I promise.
Thanks, I use AHK a lot, and want to learn more. :) :thumbup:
try it and see
...
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Trigger script at certain time

05 Feb 2016, 18:21

Hello Derz00.

Welcome to the AutoHotkey community forums.

You can launch a SetTimer routine that checks the current time (using the built-in variable A_Now) and proceeds to executed the code if the time is found to above to a certain hour.

Best wishes.
Last edited by Gio on 05 Feb 2016, 18:47, edited 1 time in total.
SifJar
Posts: 398
Joined: 11 Jan 2016, 17:52

Re: Trigger script at certain time

05 Feb 2016, 18:41

You're probably better off using Task Scheduler if you can get it to work - it's an already running service that is part of Windows, so there is not performance draw from it, while there would be a (rather minimal, most likely) performance draw of having an AHK script check the current time every X milliseconds via SetTimer.

For Task Scheduler, you'll most likely need to compile the AHK script to an EXE and then it should work.

Although if the script is only executing that one command, there is no real need to use AHK at all; you should be able to add the command shutdown /p /f directly in Task Scheduler. Which will be both simpler and more efficient that invoking AHK, only to do what Task Scheduler would have done in the first place.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Trigger script at certain time

05 Feb 2016, 22:31

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 :D 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.
Last edited by wizardzedd on 06 Feb 2016, 17:17, edited 2 times in total.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Trigger script at certain time

06 Feb 2016, 16:46

Sorry.
I'm lost.
It's 4:30 PM, and I ran that script just as it is.

The computer shut down. :o :shock:


;) No big deal. No unsaved work lost. But what is it supposed to do?

As for the task scheduler, I just must not know how to run it, because I made an AHK script to shutdown, which worked when I ran it manually, (see original post) and told Task Scheduler to run it at 11:00 at night, and it doesn't do it. :oops: Must not know what I'm doing.

Wouldn't it work to have the script run (in Startup folder) and check every 1 or half hour and shut down. If it's just within a part of the interval time, just shut down. It'd be irregular shut down time, but I leave this computer around 6 PM anyway.
Excuse my ignorance. I'm new to AHK, but I like it and want to learn more. :thumbup:
try it and see
...
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Trigger script at certain time

06 Feb 2016, 17:23

derz00 wrote: The computer shut down. :o :shock:
Sorry, it is fixed now was missing parenthesis.
derz00 wrote: Wouldn't it work to have the script run (in Startup folder)
Yes that would work instead of task scheduler.
The script will set a One time only timer to shutdown at the specified time; no need for periodic checks.
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: Trigger script at certain time

11 Feb 2016, 11:23

Well, I thought I knew quite a bit about AHK coding. However, your simple script proves me wrong. I haven't got into those complicated functions a lot.
So, it's going to check the time, find how long till 10:00 PM, and wait till then? I'll have to analyze, tweak, experiment with your script to learn some things.

The way it looks now, I'm going to be using AHK for a long time!!! No other scripting languages are the same... :)
try it and see
...

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: arrondark, mikeyww, Noitalommi_2, Zedcars and 207 guests