Run a script every specified weekday at specified time (use ahk instead of task manager) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
partof
Posts: 94
Joined: 16 Jan 2016, 08:38

Run a script every specified weekday at specified time (use ahk instead of task manager)

21 Mar 2019, 10:22

I would like to replace my 50+ tasks manager with ahk.
I am not sure if this is the best way of doing it but thanks to this https://autohotkey.com/board/topic/88278-help-to-close-and-open-a-program-at-a-specific-time/?p=560220 I found the sleeptill functionhttps://autohotkey.com/board/topic/51576-sleep-until-time/. So have replaced all my tasks that are running daily.

I would like now to replace my tasks that are running only on specified days ex: every monday, wednesday and friday at 8am, 11am and 2pm.

I am not sure what's the best way of doing.

I call my tasks like this :

Code: Select all

Loop
{
  SleepTill(1100) ;wait till 11 am
  Run ...
  SleepTill(1610) ;wait till 4:10 pm
  Run  ... 

}
Maybe something like this could work but I don't know how to handle this in the sleepTill function.

Code: Select all

Loop
{
  SleepTill(1,3,5, 1610) ;run at 4:10 pm only monday(1), wednesday (3) and friday(5)
  Run  ... 
}

Here is the sleepTill function

Code: Select all

SleepTill(Time) {
ST_Hour:=SubStr(Time, 1 ,2)
ST_Min:=SubStr(Time, 3 ,2)
ST_Sec:=(SubStr(Time, 5 ,2)<>"" ? SubStr(Time, 5 ,2) : "00")
STime:=(((ST_Hour-A_Hour)*60+(ST_Min-A_Min))*60+(ST_Sec-A_Sec))*1000 
STime:=STime<0 ? STime+86400000 : STime
Sleep %STime%
Return % A_Hour ":" A_Min
}
Maybe this code would be a better start, but I don't need to schedule for the next day as I shut down my computer every evening and restart it every morning.
//autohotkey.com/board/topic/51576-sleep-until-time/?p=322568
partof
Posts: 94
Joined: 16 Jan 2016, 08:38

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

21 Mar 2019, 11:36

So far, Iwas able to do this:

Code: Select all

SleepTill(1610,"1,3,5")

Code: Select all

SleepTill(Time, date:=0) {
ST_weekday:=A_WDay
if (%ST_weekday% in date){
ST_Hour:=SubStr(Time, 1 ,2)
ST_Min:=SubStr(Time, 3 ,2)
ST_Sec:=(SubStr(Time, 5 ,2)<>"" ? SubStr(Time, 5 ,2) : "00")
STime:=(((ST_Hour-A_Hour)*60+(ST_Min-A_Min))*60+(ST_Sec-A_Sec))*1000 
STime:=STime<0 ? STime+86400000 : STime
Sleep %STime%
Return % A_Hour ":" A_Min
}
}
SOTE
Posts: 653
Joined: 15 Jun 2015, 06:21

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

21 Mar 2019, 16:09

Interesting, but the obvious issue that comes up is the computer going into sleep or hibernation mode. Usually this kills any AutoHotkey script from executing at a specified time. Consequently, the script would have to prevent sleep or hibernation mode from happening, or use a DLL to wake the computer back up from sleep or hibernation. The next issue would be screen lock. Where that needs to be temporarily turned off, so that the script will be able to execute properly when the time comes.
nou
Posts: 19
Joined: 24 Feb 2019, 21:21

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

21 Mar 2019, 17:57

Without taking into consideration all other issues and looking purely at the code, I'd suggest you do something similar to what you already have

Code: Select all

if (InStr(date, A_Wday))
that way, you can just do

Code: Select all

sleepTill(1610, 135)

Code: Select all

SleepTill(Time, date:= 0)
{
	if (InStr(date, A_Wday))
	{
		; get hour 
		ST_Hour:=SubStr(Time, 1 ,2)
	
		; get minute 
		ST_Min:=SubStr(Time, 3 ,2)
	
		; get seconds 
		ST_Sec:=(SubStr(Time, 5 ,2)<>"" ? SubStr(Time, 5 ,2) : "00")
	
			; calculate remaining time in milliseconds
			; target Time - current time, into milliseconds 
		STime:=(((ST_Hour-A_Hour)*60+(ST_Min-A_Min))*60+(ST_Sec-A_Sec))*1000 
	
			; if remaining time < 0, then add in 86400000 ms (24 hours)
		STime:= (STime<0) ? (STime+86400000) : (STime)

			; sleep for that amount of remaining time. 
		Sleep %STime%
	}

	Return % A_Hour ":" A_Min
}
However, as oppose to using Loops, I'd recommend using a SetTimer instead. So using a negative value for a set-timer will cause it to run once only. Plus if you want to run anything else on that script, you still can. I also do recommend putting a check for "if date = 0", and allowing it to be scheduled anyday of the week if it's 0/blank.
See here:

Code: Select all

F1::
{
	Schedule("Hi", 1556)
	return
}

; ----------- Labels ----------
Hi: 
MsgBox % "Hi! It is now " A_Hour " : " A_Min "!`n"
return

Schedule(label, Time, date:= 0)
{
	; we'll keep this portion the same
	if (InStr(date, A_Wday)) || (date = 0)
	{
		; get hour 
		ST_Hour:=SubStr(Time, 1 ,2)

		; get minute 
		ST_Min:=SubStr(Time, 3 ,2)

		; get seconds 
		ST_Sec:=(SubStr(Time, 5 ,2)<>"" ? SubStr(Time, 5 ,2) : "00")

			; calculate remaining time in milliseconds
			 ; target Time - current time, into milliseconds 
		STime:=(((ST_Hour-A_Hour)*60+(ST_Min-A_Min))*60+(ST_Sec-A_Sec))*1000 

			; if remaining time < 0, then add in 86400000 ms (24 hours)
		STime:= (STime<0) ? (STime+86400000) : (STime)
			; sleep for that amount of time. 
		
		SetTimer, % label, % -1 * STime
		Return 
	}
	
	; otherwise, if it's not on the weekday we specified, turn it off. 
	SetTimer, % label, Off
	return
}
SOTE
Posts: 653
Joined: 15 Jun 2015, 06:21

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

22 Mar 2019, 06:37

partof, you might also want to take a look at this thread. https://www.autohotkey.com/boards/viewtopic.php?f=76&t=41598

Another possible way to address the problem, is to have your code run at startup. Due to the computer being turned off, sleep, hibernation, or locked; you can have the script run when possible, when a user logs in. This solution can work, if execution of the script is not so critical, and the nearest convenient time is possible. So if a date is missed, upon startup, the script will check that execution is overdue and then run.
partof
Posts: 94
Joined: 16 Jan 2016, 08:38

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

22 Mar 2019, 09:34

Thanks nou, this is indeed a better way to do it.

Sote, I run into this problem today. I put my computer into sleep and all my tasks were disorganized. I do not want ahk to wake up my computer. I only need my task to be display when I log back in my computer after a sleep. Ex: I have a task set for 10 am and 11am. I put my computer into sleep from 9am to 10:30am. When I log back in, I should see the 10 am task and my 11 am task should come up as expected at 11 am.
SOTE
Posts: 653
Joined: 15 Jun 2015, 06:21

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)  Topic is solved

22 Mar 2019, 12:19

partof wrote:
22 Mar 2019, 09:34
Thanks nou, this is indeed a better way to do it.

Sote, I run into this problem today. I put my computer into sleep and all my tasks were disorganized. I do not want ahk to wake up my computer. I only need my task to be display when I log back in my computer after a sleep. Ex: I have a task set for 10 am and 11am. I put my computer into sleep from 9am to 10:30am. When I log back in, I should see the 10 am task and my 11 am task should come up as expected at 11 am.
You also have to make sure your code specifies that if the time is later than scheduled, then it will run. if (time >= "3/21/2019 3:01 pm"). And make sure that every time that you login, your programs will run. You can have subroutines turn SetTimers on and off, in addition to GUIs where you input the time.

Code: Select all

#noenv
#persistent
SetTimer, WillDo, 45000 ; check time every 45 seconds.
Return

WillDo:

formattime, time,, M/d/yyyy h:mm tt ; gets system time and puts it in desired format.
if (time >= "3/21/2019 3:01 pm") ;calls DoThat subroutine at specified time or later.
{
	gosub, DoThat
}
Return

DoThat:

msgbox, We did something!
SetTimer, WillDo, Off
Return
partof
Posts: 94
Joined: 16 Jan 2016, 08:38

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

28 Mar 2019, 03:33

Thanks Sote for this code, I didn't think about do it that way. It's better than relying on a count down!
electrified
Posts: 24
Joined: 18 Jan 2019, 11:06

Re: Run a script every specified weekday at specified time (use ahk instead of task manager)

28 Mar 2019, 05:27

You can try something like this:

Code: Select all

#Persistent

SetTimer, Chronos, 500

Chronos:
FormatTime, TimeToMeet,,HHmmss

If A_YYYY = 2019									; <============= Sets the year in which the MsgBox will initiate
	{
		If A_MM = 03								; <============= Sets the month in which the MsgBox will initiate
			{
				If A_DD = 29						; <============= Sets the day in which the MsgBox will initiate
					{
						If TimeToMeet = 134530		; <============= Sets the hour, minute and second in which the MsgBox will initiate
							{
								MsgBox "TEXT"		; <============= Can be replaced with any bit of code
							}
						If TimeToMeet = 000000		; <============= Another instance for the same date but different time
							{
								MsgBox "TEXT2"
							ExitApp
							}
					}
			}
	}
Return
This particular script will initate the code...in this case a MsgBox, at exactly 13:45:30 tomorrow. This is dependant on the system clock though

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], WilburBr and 160 guests