Skip to content

Instantly share code, notes, and snippets.

@sidola
Created June 24, 2014 09:13
Show Gist options
  • Save sidola/41c00178340f2b8f8dc7 to your computer and use it in GitHub Desktop.
Save sidola/41c00178340f2b8f8dc7 to your computer and use it in GitHub Desktop.
AHK - Gets the PID from any ID
PIDfromAnyID( anyID="" ) { ; SKAN, 10-May-2014. http://ahkscript.org/boards/viewtopic.php?p=17974#p17974
Process, Exist, %anyID%
IfGreater, ErrorLevel, 0, Return ErrorLevel
DetectHiddenWindows, % SubStr( ( ADHW := A_DetectHiddenWindows ) . "On", -1 )
SetTitleMatchMode, % SubStr( ( ATMM := A_TitleMatchMode ) . "RegEx", -4 )
WinGet, PID, PID, ahk_id %anyID%
IfEqual, PID,, WinGet, PID, PID, %anyID%
DetectHiddenWindows, %ADHW%
SetTitleMatchMode, %ATMM%
Return PID ? PID : 0
}
; Examples
MsgBox % PIDfromAnyID() ; Own PID
MsgBox % PIDfromAnyID( "winlogon.exe" ) ; process name
MsgBox % PIDfromAnyID( 4 ) ; validating raw PID
MsgBox % PIDfromAnyID( "MyScript.ahk" ) ; PID of a running script
MsgBox % PIDfromAnyID( "MyScript.ahk ahk_class AutoHotkey" ) ; precise than above
MsgBox % PIDfromAnyID( "Torrent" ) ; part of window title
MsgBox % PIDfromAnyID( A_ScripthWnd ) ; Own PID
MouseGetPos,,, OutputVarWin
MsgBox % PIDfromAnyID( OutputVarWin ) ; PID of Window under mouse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment