Sometimes, it happens that many AutoHotkey scripts remain in the background of the system and needs to close them. The following function would be handy for such task.
It can kill not only AutoHotkey scripts but also other applications by specifying the process name.
F1:: ; Press F1 to close all processes whose name is AutoHotkey.exe.
Process, Exist
nThisPID := ErrorLevel ;the script PID
TrayTip, KillAHKScripts, Closing AutoHotkey Scripts, 5, 1
if (nKilled := KillAllProcessByName("AutoHotkey.exe", nThisPID) )
TrayTip, KillAHKScripts, % "Killed " nKilled " process(es).", 5, 1
nKilled := ""
Return
KillAllProcessByName(strProcessName, strExceptPIDs="") {
;[Parameters]
; strProcesName : the proess name to be closed. e.g. AutoHotkey.exe
; strExceptPIDs : a list of eception PIDs not to be closed delimited by commas. e.g. 1234,5678,987
;[Return Value]
; the count of closed processes.
nCount := 0
Loop {
nClosedPID := False
for process in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID from Win32_Process Where Name='" strProcessName "'") {
nLoopPID := process.ProcessID
if nLoopPID not in %strExceptPIDs%
{
Process, Close, % process.ProcessID
if (nClosedPID := ErrorLevel)
nCount++
}
}
} Until !nClosedPID
Return nCount
}
This can be extended to leave the oldest instance but kill others. For example, multiple instances of explorer.exe also remain in the background when they are launched via Notepad++. In the following code, the function, FindOldestProcessByName(), finds the oldest process of the given name. Pressing F2 lets you close instances of explorer.exe except the one owning the task bar and the desktop.
F2:: ; Press F2 to close all Explorer.exe but leave the oldest one.
sExplorer := "Explorer.exe"
TrayTip, Kill Explorers, Closing %sExplorer%, 5, 1
if (nKilled := KillAllProcessByName(sExplorer, FindOldestProcessByName(sExplorer)))
TrayTip, Kill Explorers, % "Killed " nKilled " process(es).", 5, 1
nKilled := ""
Return
KillAllProcessByName(strProcessName, strExceptPIDs="") {
;[Parameters]
; strProcesName : the proess name to be closed. e.g. AutoHotkey.exe
; strExceptPIDs : a list of eception PIDs not to be closed delimited by commas. e.g. 1234,5678,987
;[Return Value]
; the count of closed processes.
nCount := 0
Loop {
nClosedPID := False
for process in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID from Win32_Process Where Name='" strProcessName "'") {
nLoopPID := process.ProcessID
if nLoopPID not in %strExceptPIDs%
{
Process, Close, % process.ProcessID
if (nClosedPID := ErrorLevel)
nCount++
}
}
} Until !nClosedPID
Return nCount
}
FindOldestProcessByName(strProcessName) {
For oProc in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID,CreationDate from Win32_Process Where Name='" strProcessName "'") {
if !nOldestPID {
nOldestPID := oProc.ProcessID
strOldestPIDCreationDate := oProc.CreationDate
} else if (strOldestPIDCreationDate > oProc.CreationDate) {
nOldestPID := oProc.CreationDate
strOldestPIDCreationDate := oProc.CreationDate
}
}
return nOldestPID
}