如何關閉所有正在運行的腳本並稍後重新啟動它們?

如何關閉所有正在運行的腳本並稍後重新啟動它們?

為了更新 AutoHotkey,必須關閉所有正在執行的腳本。 AutoHotkey進程可以用一個簡單的腳本來終止(見下文),但我不知道如何隨意重新啟動相同的進程。目前,我在更新後重新啟動電腦(以啟動 Windows 啟動資料夾中的 AHK 腳本)。

Windows 顯然不允許同時啟動多個文件。因為我啟用了數十個 AutoHotkey 腳本,所以手動啟動它們是不可行的。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Loop
{
#Singleinstance force
Process, Close, Autohotkey.exe
}

答案1

; FileDelete, %A_Desktop%\my running scripts.ini

; Get a list of all running AHK scripts:
DetectHiddenWindows, ON
WinGet, id, list, ahk_class AutoHotkey 
Loop, %id% ; retrieves the  ID of the specified windows, one at a time
{
    this_ID := id%A_Index%
    WinGetTitle, title, ahk_id %this_ID%    
    SkriptPath := RegExReplace(title, " - AutoHotkey v" A_AhkVersion )
    If InStr(SkriptPath, A_ScriptFullPath)
        continue
    ; Store the path of each running script in an INI-file and terminate it:
    IniWrite, %SkriptPath%`n, %A_Desktop%\my running scripts.ini, my_running_scripts
    WinClose, %SkriptPath% ahk_class AutoHotkey 
}
; Run %A_Desktop%\my running scripts.ini

; Create a new script in the startup folder that starts the same scripts after rebooting:
FileAppend, 
(
    IniRead, my_running_scripts, `%A_Desktop`%\my running scripts.ini, my_running_scripts
    Loop, parse, my_running_scripts, ``n
        Run `%A_LoopField`%
    ; FileDelete, `%A_ScriptFullPath`%
    ExitApp
)
, %A_Startup%\my running scripts.ahk
ExitApp
return

如果您想從桌面手動重新啟動腳本,請替換%A_Startup%為。%A_Desktop%

相關內容