5

When testing AutoHotkey scripts, I sometimes forget to reload my scripts after making changes. This leads to me accidentally testing old, outdated versions of my scripts.

Instead of manually reloading the script, I would like to have scripts automatically reload if they have been modified.

How can I make AutoHotkey reload the current script any time a .ahk file is modified?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

3 Answers3

6

Somewhere near start of the script, in the auto-execute section

#SingleInstance force
FileGetTime ScriptStartModTime, %A_ScriptFullPath%
SetTimer CheckScriptUpdate, 100, 0x7FFFFFFF ; 100 ms, highest priority

Anywhere in the script (usually somewhere at the bottom):

CheckScriptUpdate() {
    global ScriptStartModTime
    FileGetTime curModTime, %A_ScriptFullPath%
    If (curModTime == ScriptStartModTime)
        return
    SetTimer CheckScriptUpdate, Off
    Loop
    {
        reload
        Sleep 300 ; ms
        MsgBox 0x2, %A_ScriptName%, Reload failed. ; 0x2 = Abort/Retry/Ignore
        IfMsgBox Abort
            ExitApp
        IfMsgBox Ignore
            break
    } ; loops reload on "Retry"
}
LogicDaemon
  • 466
  • 10
  • 22
  • @StevenVascellaro nope, check builtin variables here: https://autohotkey.com/docs/Variables.htm#prop – LogicDaemon Aug 03 '17 at 15:25
  • After adding this script, my AHK file is sometimes reloaded as two duplicate instances which run simultaneously – Stevoisiak Aug 03 '17 at 21:45
  • @StevenVascellaro strange, because reload command should have prevented that. Tried adding `#SingleInstance force` to start of the script? Also I edited the script to detect the condition. – LogicDaemon Aug 04 '17 at 06:07
  • @StevenVascellaro also, I added highest priority to start of the procedure to avoid another reload or restart while one is already queued – LogicDaemon Aug 04 '17 at 06:22
  • 1
    I found the script was less susceptible to errors when I changed `Sleep 100` to `Sleep 200` – Stevoisiak Aug 04 '17 at 16:39
  • hm, ok. Example in the manual uses 1000 ms delay, but that's too long to my taste. – LogicDaemon Aug 04 '17 at 18:38
  • a warning pops up saying that the `ScriptStartModTime` variable (in the line `If (curModTime <> ScriptStartModTime) {`) has not been assigned a value – Ooker Jan 22 '21 at 11:31
  • @Ooker have you added `FileGetTime ScriptStartModTime, %A_ScriptFullPath%` before `SetTimer`? It seeds the value. – LogicDaemon Jan 23 '21 at 08:30
  • @Ooker strange, I have no idea why. Probably missing some details. I suggest you ask separate question, you may post a link here. – LogicDaemon Jan 23 '21 at 17:50
2

This is how I've done it:

#If WinActive("AHK.ahk - Notepad") or WinActive("*AHK.ahk - Notepad")
    ~^s::
        Reload
    Return
#If

Checks if the current window is the script that I want autoreloaded whenever I hit Ctrl-S. The ~ means the default action of Ctrl-S (saving the file) is preserved, and then we simply reload it.

Codemonkey
  • 4,455
  • 5
  • 44
  • 76
0

I'm still new at AHK but here's what I've come with. If you're using Notepad++ to edit AHKS this will run any ahk that's open and currently in focus in Notepad++ on saving with Ctrl-S and won't effect any other file types.

This script only has to be in one running ahk to work on all ahks being modified in Notepad++.

This can be used on multiple text editor programs too like win Notepad just get rid of the ++ associated with Notepad++

    ~^s:: ; Saves and Runs ANY AHK open in Notepad++
    Sleep 150
    WinGetTitle, Title, A
    Needle := "ahk - Notepad++"
    IfInString, Title, %Needle%
     {  
     StringReplace, xxx, Title,- Notepad++, , All
      run %xxx% 
     return                         
     }      
    else
    return   
izak-a
  • 1
  • 1