Keeping count of AHK script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Keeping count of AHK script

Post by Fulminare » 29 Jul 2021, 03:08

Hi

I have a functioning AHK script

I want a message box to show how many times that script has been opened in a month

Eg. when the script runs , a message box should display something as follows :

"Jul

6"


Now I know that the script has run 6 times in the month of July


once August begins, the counting should reset and start from 1 as opposed to continuing from the previous month's count

I found the following code , but how to include month specific counting ?

Code: Select all

; v1
scriptCount(){
	static count := 0
	static wasIncremented := scriptCount() ; sets the script count
	local scriptCode, oldCount
	if !wasIncremented {
		fileRead scriptCode, % A_ScriptFullPath
		if !scriptCode
			return
		if !regexmatch(scriptCode, "O)\QscriptCount(){\E\s+static count := (\d+)", oldCount)
			return
		if !scriptCode:=regexreplace(scriptCode, oldCount[1], oldCount[1]+1, , 1, oldCount.pos(1))
			return
		fileDelete % A_ScriptFullPath
		fileAppend % scriptCode, % A_ScriptFullPath
		count++
		return true
	}
	return count
}
msgbox % "script run number: " scriptCount()	; gets the script count
[Mod edit: [code][/code] tags added.]



I would be very grateful if anyone can kindly assist me

Regards

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Keeping count of AHK script

Post by mikeyww » 29 Jul 2021, 08:20

You could use IniWrite and IniRead to store simple strings such as "year=2021", "month=07" and "runs=5". When the script runs, if the current year and month match what is in the INI file, you increment the number of runs. If the year or month do not match, then you update the year and month, and set the runs to 1.

Fulminare
Posts: 369
Joined: 26 Jun 2021, 20:15

Re: Keeping count of AHK script

Post by Fulminare » 29 Jul 2021, 08:38

@mikeyww

Thank you for your reply

I have no knowledge of coding

However, I will try to go in the direction you have pointed at by reading about it

Regards.

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Keeping count of AHK script  Topic is solved

Post by mikeyww » 29 Jul 2021, 09:02

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini"), track := "Track"
IniRead, year , %ini%, %track%, year
IniRead, month, %ini%, %track%, month
IniRead, runs , %ini%, %track%, runs, 0
If (year != A_YYYY || month != A_MM) {
       IniWrite, %A_YYYY%   , %ini%, %track%, year
       IniWrite, %A_MM%     , %ini%, %track%, month
       IniWrite, % runs := 1, %ini%, %track%, runs
} Else IniWrite, % ++runs   , %ini%, %track%, runs
MsgBox, 64, Number of runs this month, %runs%

Post Reply

Return to “Ask for Help (v1)”