The "Last Found" Window


This is the window most recently found by IfWin[Not]Exist, IfWin[Not]Active, WinWait[Not]Active, or WinWait. It can make scripts easier to create and maintain since the WinTitle and WinText of the target window do not need to be repeated for every windowing command. In addition, scripts perform better because they don't need to search for the target window again after it has been found the first time.

The "last found" window can be used by all of the windowing commands except WinWait, WinActivateBottom, and GroupAdd. To use it, simply omit all four window parameters (WinTitle, WinText, ExcludeTitle, and ExcludeText).

Each thread retains its own value of the "last found" window, meaning that if the current thread is interrupted by another, when the original thread is resumed it will still have its original value of the "last found" window, not that of the interrupting thread.

If the last found window is a hidden Gui window, it can be used even when DetectHiddenWindows is Off. This is often used in conjunction with "Gui +LastFound".

Examples

Run Notepad
WinWait Untitled - Notepad
WinActivate  ; Uses the last found window.

IfWinExist, Untitled - Notepad
{
    WinActivate  ; Automatically uses the window found above.
    WinMaximize  ; same
    Send, Some text.{Enter}
    return
}

IfWinNotExist, Calculator
    return
else
{
    WinActivate  ; The above "IfWinNotExist" also set the "last found" window for us.
    WinMove, 40, 40  ; Move it to a new position.
    return
}

Other Usages of the WinTitle Parameter

If multiple windows match the WinTitle/Text criteria of a windowing command such as WinMove, the one that is above the others in stacking order is used. For example, if the active/foremost window matches the criteria, it is used even if there are other matching windows beneath it.

Active Window (A): Almost all of the windowing commands can be told to operate upon the active window by specifying the letter A as the WinTitle parameter and omitting WinText, ExcludeTitle, and ExcludeText. In the following example, Win+UpArrow becomes a hotkey to maximize the active window: #Up::WinMaximize A

Window Class (ahk_class): All windowing commands can operate upon a class of windows by the class text shown by Window Spy or retrieved by WinGetClass. In the following example, a split-view Explorer window would be activated: WinActivate ahk_class ExploreWClass

Unique ID/HWND (ahk_id): All windowing commands can operate upon a specific window or control via its unique ID number. For example: WinActivate ahk_id %VarContainingID%. The ID of a window is typically retrieved via WinExist() or WinGet. The ID of a control is typically retrieved via ControlGet Hwnd, MouseGetPos, or DllCall. Also, ahk_id will operate upon controls even if they are hidden; that is, the setting of DetectHiddenWindows does not matter.

Process ID (ahk_pid): All windowing commands can operate upon the window(s) belonging a process identifier (PID). For example, WinClose ahk_pid %VarContainingPID% would close the topmost window of that process. A PID can be retrieved via WinGet or Run or Process.

Window Group (ahk_group): All windowing commands can operate upon the windows belonging to a window group by specifying ahk_group MyGroupName for the WinTitle parameter. The commands WinMinimize, WinMaximize, WinRestore, WinHide, WinShow, WinClose, and WinKill will act upon all the group's windows. By contrast, the other window commands such as WinActivate and IfWinExist will operate only upon the topmost window of the group.

Multiple Criteria: By contrast with ahk_group in the paragraph above (which broadens the search), the search may be narrowed by specifying more than one criterion within the WinTitle parameter. In the following example, the script waits for a window whose title contains My File.txt and whose class is Notepad:

WinWait My File.txt ahk_class Notepad
WinActivate  ; Activate the window it found.

When using this method, the text of the title (if any is desired) should be listed first, followed by one or more additional criteria. Criteria beyond the first should be separated from the previous with exactly one space or tab (any other spaces or tabs are treated as a literal part of the previous criterion).