Automatically reload AutoHotkey script when modifi

2019-05-07 02:27发布

问题:

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?

回答1:

Somewhere near start of 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) {
        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"
    }
}


标签: autohotkey