AutoIt WinActive Function

The WinActive function checks if a window is currently active.

4298 views

Edited: 2017-03-25 01:54

The AutoIt WinActive function is used to check if a window is currently active. If are looking for a way to activate a window, look up WinActivate instead.

Checking if a window is active can be useful to avoid bogus clicks and key sends. Before input is send you would usually make sure, that the right application window is active.

Use this function in combination with conditional checks, to make your script do whatever you want, depending on the response from WinActive.

To fetch the information you need to identify the right application window, use the AutoIt window info tool that came with AutoIt. Activate the application window, and you'll be able to read the info from the WI tool. See screenshot below:

Screenshot showing the Window tab of the AutoIt Window Info tool.

Parameters

WindowTitle, hWnd or class of the window to check.
TextThe text of the window to check. Default is an empty string.

Return Value

SuccessThe handle of the window.
Failure0

WinActive example

; Coordinated to click on screen
$y = 100
$x = 100

If WinActive("[CLASS:SpotifyMainWindow]") Then
  MouseClick("left", $x, $y, 1)
EndIf

And checking by window title. The following is not the best example, since the title text of the window in Spotify, includes the title of the song you are currently playing, making it unreliable – but it clearly shows the point.

If WinActive("Spotify - some artist – some track name") Then
  MouseClick("left", $x, $y, 1)
EndIf

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials