Search code examples
autohotkey

How to get window handle by PID in autohotkey?


I want to get window handle by PID in autohotkey, because title of the window is always changing. If anyone wonder, I want to get handle of last.fm main window.


Solution

  • You can use the WinGet command with the Cmd paramter as ID.

    Cmd is the operation to perform, which if blank defaults to ID.
    ID: Retrieves the unique ID number of a window. Also known as the window handle (HWND).

    WinTitle can be a PID.

    WinGet, UniqueID, ID, ahk_pid %VarContainingPID%
    

    Another option is WinExist()

    UniqueID := WinExist("ahk_pid" . VarContainingPID)