WinGetText

Retrieves the text from the specified window.

WinGetText, OutputVar , WinTitle, WinText, ExcludeTitle, ExcludeText

Parameters

OutputVar

The name of the output variable in which to store the retrieved text.

WinTitle, WinText, ExcludeTitle, ExcludeText

If each of these is blank or omitted, the Last Found Window will be used. Otherwise, specify for WinTitle a window title or other criteria to identify the target window and/or for WinText a substring from a single text element of the target window (as revealed by the included Window Spy utility).

ExcludeTitle and ExcludeText can be used to exclude one or more windows by their title or text. Their specification is similar to WinTitle and WinText, except that ExcludeTitle does not recognize any criteria other than the window title.

Window titles and text are case-sensitive. By default, hidden windows are not detected and hidden text elements are detected, unless changed with DetectHiddenWindows and DetectHiddenText. By default, a window title must start with the specified WinTitle or ExcludeTitle to be a match, unless changed with SetTitleMatchMode.

Error Handling

[v1.1.04+]: This command is able to throw an exception on failure. For more information, see Runtime Errors.

ErrorLevel is set to 1 if there was a problem or 0 otherwise.

Remarks

If there is no matching window, OutputVar is made blank.

The text retrieved is generally the same as what Window Spy shows for that window. However, if DetectHiddenText has been turned off, hidden text is omitted from OutputVar.

Each text element ends with a carriage return and linefeed (CR+LF), which can be represented in the script as `r`n. To extract individual lines or substrings, use commands or built-in functions such as InStr() and SubStr(). A parsing loop can also be used to examine each line or word one by one.

If the retrieved text appears to be truncated (incomplete), try using VarSetCapacity(OutputVar, 55) prior to WinGetText (replace 55 with a size that is considerably longer than the truncated text). This is necessary because some applications do not respond properly to the WM_GETTEXTLENGTH message, which causes AutoHotkey to make the output variable too small to fit all the text.

The amount of text retrieved is limited to a variable's maximum capacity (which can be changed via the #MaxMem directive). As a result, this command might use a large amount of RAM if the target window (e.g. an editor with a large document open) contains a large quantity of text. To avoid this, it might be possible to retrieve only portions of the window's text by using ControlGetText instead. In any case, a variable's memory can be freed later by assigning it to nothing, i.e. OutputVar := "".

It is not necessary to do SetTitleMatchMode Slow because WinGetText always retrieves the text using the slow mode (since it works on a broader range of control types).

To retrieve a list of all controls in a window, use WinGet ControlList.

ControlGetText, WinGetActiveStats, WinGetActiveTitle, WinGetTitle, WinGetPos, #MaxMem

Examples

Opens the calculator, waits until it exists, and retrieves and reports its text.

Run, Calc.exe
WinWait, Calculator
WinGetText, text ; Use the window found by WinWait.
MsgBox, The text is:`n%text%