SoFunction
Updated on 2025-04-12

Autoit Introduction Tutorial Summary Page 3/5


Note: Window title and window text parameters are always case sensitive.
1. Wait for window series commands/functions
Both AHK and AU3 provide a set of window waiting commands/functions with similar usage: WinWait/WinWaitActive/WinWaitClose.
They are used to wait for the window to appear, wait for the window to be activated, and wait for the window to be closed respectively. Since the parameters of these commands/functions are similar, we will only use WinWait as an example to illustrate.
AHK:
WinWait [, Window title, Window text, Timeout time, Exclude title, Exclude text]
AU3:
WinWait ( "Window Title" [, "Window Text" [, Timeout Time]] )
The function of WinWait is to no longer execute all subsequent statements before the target window appears.
Suppose we want to run the Notepad program and prompt the user when its window appears:
[Example 3.1.1]

AHK:
Run Notepad
WinWait, Untitled – Notebook
The MsgBox Notepad window has been opened!
AU3:
Run("Notepad")
WinWait("Unt title-Notpad")
MsgBox(0, "", "Notepad window has been opened!")

2. Activate window-related commands/functions
The way to make the target window an active window is to activate it. The available command/function is WinActivate:
AHK:
WinActivate [, window title, window text, exclude title, exclude text]
AU3:
WinActivate ( "Window Title" [, "Window Text"] )

3. Close the window
There are two ways to close a window. One is to close a window normally (WinClose), and the other is to forcefully close a window (WinKill):
AHK:
WinClose/WinKill [, window title, window text, timeout time, excluding title, excluding text]
AU3:
WinClose/WinKill ( "Window Title" [, "Window Text"] )

Now we can implement a relatively simple function, for example, we can open the system properties window and wait for it to appear. Activate it after the window appears, and then wait for 3 seconds before closing it:
[Example 3.1.2]

AHK:
Run, 
WinWait, System Properties
WinActivate, System Properties
WinWaitActive, System Properties
Sleep, 3000
WinClose, System Properties
WinWaitClose, System Properties
AU3:
Run("Control ")
WinWait("System Properties")
WinActivate("System Properties")
WinWaitActive("System Properties")
Sleep(3000)
WinClose("System Properties")
WinWaitClose("System Properties")

Suggestion: If the frequently appear in the program where these window titles are to be used, it will bring a problem: the readability of the script, maybe you will think, isn't this very intuitive? But the question is what if the repeated window title is a very long string? This will seriously affect the aesthetics of the entire code. And we have no way to understand the "foundation" of these window titles, and we don't know how this window title came about. And if we define a variable (assuming the variable name is "AppWindow1") to save this window title, we can use variables to represent it in the command/function, so that the purpose of making the code more clear. In addition, even if the target software changes its window title for some reason (such as upgrade), we can easily make changes.

4. A more accurate way to identify windows (mainly for AHK scripts)
The program will have at least one process when it is running. If you can obtain this process ID, it can ensure accurate identification of the window to a certain extent. In addition, each window has a defined window class name (Class, for example, the class name of the Notepad window is Notepad), so we can use this to exclude other window classes that are different from the target window. In fact, we have a more accurate method:
Each window (including controls) is assigned by Windows a unique identifier (ID) that can be distinguished from other windows, which we call a window handle (HWND).
One disadvantage of directly giving the window title to represent a window is that it cannot guarantee that the window will always be used as the operation target during the script running, because there is a high possibility that other "same name" windows (or windows that meet the matching conditions) will appear during this process. If we use this identifier to represent the window, we can naturally ensure that the operation windows of the command/function are always the same window.
Let's first understand the command/function to obtain a window handle:
AHK:
WinGet[, Output variable, ID, Window title, Window text, Exclude title, Exclude text]
AU3:
WinGetHandle ( "Window Title" [, "Window Text"] )
The window ID obtained by WinGet will be returned through the "output variable", and the return value of WinGetHandle is the window ID obtained.

When we perform automated operations, we must first run a program. How to obtain the window handle displayed after the program is successfully run? A safer way is to first obtain the process ID of the program, and then obtain its window handle based on this process ID. AHK supports using the process ID as the window title; but AU3 does not support this use, so you can only obtain the class name of the window first and then obtain the window handle based on the class name (not insurance enough):
[Example 3.1.3]
AHK:
Run, NotePad, , , ThisPID
WinWait, ahk_pid %ThisPID%

;The ahk_pid here indicates that the variable followed is the process ID
WinGet, ThisID, ID, ahk_pid %ThisPID%

;ThisID will save the obtained window handle
AU3:

Opt("WinTitleMatchMode", 4)
Run("Notepad")
$handle = WinGetHandle("classname=Notepad")
Now I forgot about AU3, because its window functions generally do not support the use of window handles as (window title) parameters.
As for how to use window handles in AHK, simply put, any command with the "Window Title" parameter can be replaced by window handles, such as:
[Example 3.1.4]
AHK:
Run, Notepad, , , ThisPID

;First obtain the process ID of the running Notepad program
WinWait, Untitled – Notepad ahk_pid %ThisPID%

;Waiting for the process window to appear
WinGet, ThisHWND, ID, Untitled – Notepad ahk_pid %ThisPID%

;Get window handle
WinActivate, ahk_id %ThisHWND%
;The ahk_id here indicates that the variable followed is a window handle
WinWaitActive, ahk_id %ThisHWND%
Sleep, 3000
WinClose, ahk_id %ThisHWND%
WinWaitClose, ahk_id %ThisHWND%
Previous page12345Next pageRead the full text