reload all running ahk scripts periodically

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

reload all running ahk scripts periodically

27 Sep 2021, 04:55

this script below is supposed to reload ALL running ahk scripts every 30 mins. However, it now only reloads the script that I placed the code in. Any idea why?

Code: Select all


setTimer, reload_all, 1800000  ; every 30 minutes ; in auto-exe


reload_all:
winGet, running, list, ahk_class AutoHotkey  ; get running scripts  
loop % running
    {
    winGetTitle, title, % "ahk_id " running%a_index%
    script := regExReplace(title, "\s-\sAutoHotkey\s.*")
    if (script = a_lineFile)    ; skip this script
        continue

    run, % script " /restart"
    sleep 90
    }
sleep 300
reload    ; reload this script
return
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: reload all running ahk scripts periodically

27 Sep 2021, 06:19

Hallo,
try:

Code: Select all

setTimer, reload_all, 1800000  ; every 30 minutes ; in auto-exe
Return
reload_all:
DetectHiddenWindows, On
WinGet, Script, List, ahk_class AutoHotkey
Loop, %Script%
{
	ID := Script%A_Index%
	If (ID = A_ScriptHwnd)
	{
		Script%A_Index% =
		Continue
	}
	WinGetTitle, Title, ahk_id %ID%
	Script%A_Index% := StrReplace(Title, " - AutoHotkey v" A_AhkVersion)
	WinClose, ahk_id %ID%
	WinWaitClose, ahk_id %ID%
}
Loop, %Script%
	IF Script%A_Index%
		Run,% Script%A_Index%
Reload
Return
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: reload all running ahk scripts periodically

27 Sep 2021, 06:37

seems to be working great for now, thanks!
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: reload all running ahk scripts periodically

01 Oct 2021, 20:50

Rohwedder wrote:
27 Sep 2021, 06:19
Hallo,
try:

Code: Select all

setTimer, reload_all, 1800000  ; every 30 minutes ; in auto-exe
Return
reload_all:
DetectHiddenWindows, On
WinGet, Script, List, ahk_class AutoHotkey
Loop, %Script%
{
	ID := Script%A_Index%
	If (ID = A_ScriptHwnd)
	{
		Script%A_Index% =
		Continue
	}
	WinGetTitle, Title, ahk_id %ID%
	Script%A_Index% := StrReplace(Title, " - AutoHotkey v" A_AhkVersion)
	WinClose, ahk_id %ID%
	WinWaitClose, ahk_id %ID%
}
Loop, %Script%
	IF Script%A_Index%
		Run,% Script%A_Index%
Reload
Return
Hi, I revised the code so that it can get my current active window, and restore focus to the same window after my scripts refresh. However, it is not working as expected. any idea why?

Code: Select all

reload_all:
DetectHiddenWindows, On
WinGet, winid
WinGet, Script, List, ahk_class AutoHotkey
Loop, %Script%
{
	ID := Script%A_Index%
	If (ID = A_ScriptHwnd)
	{
		Script%A_Index% =
		Continue
	}
	WinGetTitle, Title, ahk_id %ID%
	Script%A_Index% := StrReplace(Title, " - AutoHotkey v" A_AhkVersion)
	WinClose, ahk_id %ID%
	WinWaitClose, ahk_id %ID%
}
Loop, %Script%
	IF Script%A_Index%
		Run,% Script%A_Index%
Reload
WinActivate ahk_id %winid%
Return
Rohwedder
Posts: 7616
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: reload all running ahk scripts periodically

02 Oct 2021, 03:07

After a reload the script has lost all its memories (=variables)!
Try:

Code: Select all

#SingleInstance Force
IF IdOld := A_Args[1]
{
	Sleep, 500
	WinActivate, ahk_id %IdOld% ;reactivates the previously active window
}
Return

q:: ;for my Test
reload_all:
IdOld := WinExist("A") ;ID of the currently active window
DetectHiddenWindows, On
WinGet, winid
WinGet, Script, List, ahk_class AutoHotkey
Loop, %Script%
{
	ID := Script%A_Index%
	If (ID = A_ScriptHwnd)
	{
		Script%A_Index% =
		Continue
	}
	WinGetTitle, Title, ahk_id %ID%
	Script%A_Index% := StrReplace(Title, " - AutoHotkey v" A_AhkVersion)
	WinClose, ahk_id %ID%
	WinWaitClose, ahk_id %ID%
}
Loop, %Script%
	IF Script%A_Index%
		Run,% Script%A_Index%
Run, "%A_ScriptFullPath%" %IdOld%
;The old script starts the new script and passes IdOld
ExitApp
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: reload all running ahk scripts periodically

03 Oct 2021, 04:48

I tested the above, but it still loses focus on my last window after reloading..
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: reload all running ahk scripts periodically

03 Oct 2021, 05:22

Another approach could be to use one script to run your other scripts. A sort of script manager.

How to:

Write a script and add in the scripts you want it to handle. (And an array to hold the pid of the running scripts)

Code: Select all


PIDList := [] 
ScriptsToControl := []

ScriptsToControl.Push(  "Path to my script"  )
ScriptsToControl.Push(  "Path to another script"  )
ScriptsToControl.Push(  "You get the point"  )

Next, have your script run all of your scripts.

Code: Select all


Loop, % ScriptsToControl.Length()	{

	Try	{
		Run, % ScriptsToControl[ A_Index ],,, pid
		PIDList.Push( pid )
	}Catch	{
		MsgBox, % "Failed to run: " ScriptsToControl[ A_Index ]
	}
}
	

Next set a timer for however long you want and have it close and rerun your scripts. You can also track your window before it does so.

Code: Select all



SetTimer, ReSet, 30 * 60 * 1000
return

ReSet:
	Winget, ActiveWindow , ProcessPath , A
	Loop, % PIDList.Length()	{
		Process, Close, PIDList[ A_Index ]
	}
	PIDList := []
	Loop, % ScriptsToControl.Length()	{

		Try	{
			Run, % ScriptsToControl[ A_Index ],,, pid
			PIDList.Push( pid )
		}Catch	{
			MsgBox, % "Failed to run: " ScriptsToControl[ A_Index ]
		}
	}
	WinWait,  % "ahk_exe " ActiveWindow 
	WinActivate, % "ahk_exe " ActiveWindow 
	return


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Freddie, gongnl, mikeyww, mmflume, OrangeCat, ShatterCoder and 89 guests