修改后自动重新加载 AutoHotkey 脚本

Automatically reload AutoHotkey script when modified

测试 AutoHotkey 脚本时,我有时会忘记在更改后重新加载脚本。这导致我不小心测试了旧的、过时的脚本版本。

我不想手动重新加载脚本,而是希望脚本在修改后自动重新加载。

如何让 AutoHotkey 在 .ahk 文件被修改时重新加载当前脚本?

脚本开始附近的某个地方,在 auto-execute section

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

脚本中的任意位置(通常在底部某处):

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

我是这样做的:

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

检查当前 window 是否是我希望在我按下 Ctrl-S 时自动重新加载的脚本。 ~表示保留Ctrl-S的默认动作(保存文件),然后我们只需重新加载即可。

我还是 AHK 的新人,但这是我带来的。 如果您正在使用 Notepad++ 编辑 AHKS,这将 运行 任何在 Notepad++ 中打开且当前处于焦点的 ahk Ctrl-S 保存并且不会影响任何其他文件类型。

此脚本只需在一个 运行ning ahk 中即可处理在 Notepad++ 中修改的所有 ahks。

这也可以用于多个文本编辑器程序,例如 win Notepad,只需去掉与 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