Timer Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Axorus
Posts: 1
Joined: 23 Jul 2021, 13:04

Timer Script

23 Jul 2021, 13:12

Help with Timer Script
Hi! I'm fairly new to coding with Autohotkey so please bear with me in my lack of breadth in explaining this issue or coding it properly.

I am trying to make a script that is able to accept an input time interval in minutes and then automatically calculate the next time to send a message.

I don't want to use a timer method because it is imperative that the timer acts on the closest millisecond to :00 seconds and I find that settimer and a sleep loop are unreliable.

Here is my code as of now:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Event  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#Persistent
CoordMode, Mouse, Screen
SetTitleMatchMode, 2
SetKeyDelay, 0, 0
getx := 0
gety := 0

amsg := []
amsg[1]:= "blah"
bmsg := []
bmsg[1]:= "blah"
Loop {
	if (A_min = 59 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
	if (A_min = 09 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
	if (A_min = 19 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
	if (A_min = 29 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
	if (A_min = 39 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
	if (A_min = 49 and A_sec = 59 and a_MSec >= 975) {
		MouseGetPos, getx, gety
		WinActivate, Google Chrome
		Random, rand, 1, 20
		MouseMove,  3082, 1362
		MouseClick, left
		Send, % bmsg[rand]
		Send, {Enter}
		MouseMove, %getx%, %gety%
		Sleep, 1001
	}
}
return
This is a timer built to work on a 10 minute interval, so the script I would want would be able to store whatever interval I want into a variable and then calculate how to go forth from there. I think I should set a variable to dictate the START time for the thing I am trying to do and that would make the portion of calculating the next times to act easier.

For example, I would want to put in a start time of :10 and an interval of :03 and have it act at :13, :16, :19, :22, and so on.

Also, I had attempted to put the code within the if statements inside a function and repeat that, but it did not send messages the same as the method listed above. Any help?
User avatar
mikeyww
Posts: 26856
Joined: 09 Sep 2014, 18:38

Re: Timer Script

27 Jul 2021, 22:09

Sure, you can use a variable. A demonstration is below.

Code: Select all

min1 = 08
While (A_Min != min1) {
 SoundBeep, 1700
 Sleep, 50
}
Send x

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doanmvu, jomaweb, mikeyww and 195 guests