AutoIt WinActivate Function

How to activate windows using AutoIts WinActivate function.

7777 views

Edited: 2017-03-25 01:53

AutoIt's WinActivate function is used to activate a window, or change the focus to another window, so one can interact with it. A window which is in focus, might still be covered by a window that is set to always on top – so be careful.

If you want to check if a window is active, use AutoIt's WinActive function instead.

To get the title info from the window you want to activate, use the AutoIt Window Info Tool. You will find the info you need in the window tab.

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

WinActivate Example

WinActivate("[CLASS:SpotifyMainWindow]")

Sometimes you may need to wait for the window to appear, before you attempt to activate it, and that's where the WinWait function comes into play. The WinWait function will pause the script, until a specified window appears. In this case it will wait for 10 seconds, if the window doesn't appear inside of those ten seconds, it will exit the script.

$hWnd = WinWait("[CLASS:SpotifyMainWindow]", "", 10)
WinActivate($hWnd)

If you don't want it to exit, you can just remove the optional timeout parameter.

$hWnd = WinWait("[CLASS:SpotifyMainWindow]")
WinActivate($hWnd)

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