Automatically reload AutoHotkey script when modified

非 Y 不嫁゛ 提交于 2020-01-01 10:35:08

问题


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) {
        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"
    }
}


来源:https://stackoverflow.com/questions/45468335/automatically-reload-autohotkey-script-when-modified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!