AHK Process Management

Post your working scripts, libraries and tools for AHK v1.1 and older
dbgba
Posts: 20
Joined: 02 Apr 2021, 22:11

AHK Process Management

17 Apr 2024, 06:54

20240417195303.jpg
(45.69 KiB) Downloaded 66 times

Code: Select all

; Hold Ctrl key to select multiple processes, double-click=exit target process, right-click=pop-up optional menu
#NoEnv  ; By dbgba
#NoTrayIcon
#SingleInstance Force
SetBatchLines -1
DetectHiddenWindows On

; AHK scripts self-start with administrator privileges, Kill processes require administrator privileges
if !(A_IsAdmin || InStr(DllCall("GetCommandLine", "Str"), ".exe"" /r"))
	Try RunWait % "*RunAs " (_:=A_IsCompiled ? "" : A_AhkPath " /r ") """" A_ScriptFullPath """" (_ ? "" : " /r")

Gui Font, s9, Microsoft YaHei UI
Gui Add, ListView, xm w702 r10 Grid HwndLVHwnd AltSubmit gLVListMouse, ScriptName|PID|ScriptPath
Loop, Parse, % "Exit|Reload|Pause|Suspend|Kill|Variables|Lines|Hotkeys|KeyHistory", |
	Gui Add, Button, % (A_Index=1 ? "" : "x+6") . " gMenuHandler", %A_LoopField%
Gui Add, Button, x+40 gSelectAll, SelectAll
Gui Add, Button, x+6 gCancelAll, DeselectAll
Gui Show,, AHK Process Management v1.13
RefreshList()
Loop, Parse, % "Exit|Reload|Pause|Suspend|Kill||Variables|Lines|Hotkeys|KeyHistory", |
	Menu, lvMenu, Add, % A_LoopField, MenuHandler

DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)  ; Automatically refreshes the list when the window changes
OnMessage(DllCall("RegisterWindowMessage", "Str", "ShellHook"), "ShellEvent")
Return

ShellEvent(wParam, lParam) {
	RefreshList()
}

LVListMouse(CtrlHwnd, GuiEvent) {
	Global LVHwnd
	if (A_GuiEvent="RightClick" And CtrlHwnd = LVHwnd And LV_GetNext())
		Menu, lvMenu, Show
	else if (A_GuiEvent="DoubleClick" And CtrlHwnd = LVHwnd And LV_GetNext())
		MenuHandler("Exit")
}

GuiClose:
	ExitApp

RefreshList() {
	Static selfPID := DllCall("GetCurrentProcessId"), idAmount

	WinGet, id, List, ahk_class AutoHotkey
	if (idAmount!=id) {  ; When the AHK process increases or decreases
		idAmount := id
		LV_Delete()
		Loop %id% {
			WinGet, this_pid, PID, % "ahk_id " this_id := id%A_Index%
			if (this_pid != selfPID) {
				WinGetTitle, this_title, ahk_id %this_id%
				fPath := RegExReplace(this_title, " - AutoHotkey v[\d.]+$")
				SplitPath, fPath, fName
				LV_Add("", fName, this_pid, fPath)
			}
		}
		LV_ModifyCol()
		LV_ModifyCol(1, "Sort")
	}
}

MenuHandler(ItemName) {
	Static WM_COMMAND := 0x111, cmd := {Exit: 65307, Reload: 65303, Pause: 65306, Suspend: 65305, Variables: 65407, Lines: 65406, Hotkeys: 65408, KeyHistory: 65409}

	(A_GuiControl!="" && ItemName := A_GuiControl) ; Get the name of the Gui button.

	if (ItemName = "Kill") {
		For _, obj in GetSelectedInfo()
			Process, Close, % obj.pid
	} else {
		For _, obj in GetSelectedInfo()
			PostMessage, WM_COMMAND, % cmd[ItemName],,, % obj.path " ahk_pid " obj.pid
	}
}

GetSelectedInfo() {
	RowNum := 0, arr := []
	While, RowNum := LV_GetNext(RowNum) {
		LV_GetText(pid, RowNum, 2)
		LV_GetText(path, RowNum, 3)
		arr.push( {pid: pid, path: path} )
	}
	Return arr
}

SelectAll() {
	LV_Modify(0, "Select")
}

CancelAll() {
	LV_Modify(0, "-Select")
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 48 guests