Using Spy++ and AutoIt to automate GUIs

I successfully figured out how to replace mouse movements and prayer with resolution-independent window handles: use Spy++ to survey the target window handles, and use AutoIt’s WinAPI.au3 lib to effect the automation.

Before we go into the feedback cycle, we’ll do some preliminary steps:

  1. Launch your target GUI program.
  2. Launch Spy++ and scroll among the harvest.
  3. Make note of the window handles.

The feedback loop is

  1. Write AutoIt functions using WinAPI.au3
  2. Try it out, sprinkling plenty of ConsoleWrite($hwnd & @CRLF) statements
  3. Refresh Spy++, Tree->Expand All, and scroll down to the relevant handles
  4. Repeat from 1.

Your first goal is to get the handle. Nothing else matters before this. Windows originate from a root Desktop window. It reminds me of a DOM tree. When you hear “tree,” think “traverse.” How do you travel among the nodes? Grab a nearby, easy parent:

$hwnd = WinActivate("Easy Title")
ConsoleWrite($hwnd & @CRLF) ; compare handle in Spy++

Now alternate:

$hwnd = _WinAPI_GetWindow($hwnd, $GW_CHILD)
ConsoleWrite($hwnd & @CRLF)
$hwnd = _WinAPI_GetWindow($hwnd, $GW_HWNDLAST)
ConsoleWrite($hwnd & @CLRF)
$hwnd = _WinAPI_GetWindow($hwnd, $GW_CHILD)
ConsoleWrite($hwnd & @CRLF)
$hwnd = _WinAPI_GetWindow($hwnd, $GW_HWNDLAST) ; are we there yet ?

Once you have the handle, you can try a couple strategies:

  • Use _SendMessageA() and emulate sniffed event messages from Spy++
  • Use _WinAPI_GetWindowRect(), unpack the coords, and use a targeted MouseClick – maybe a few pixels to the right and down for a stubborn combo box, for example.

Automating GUIs is street-fighting: there are no rules. At the very least, Spy++ and AutoIt will let you be systematic with your throat capture. Resolution independence is possible; free your users to use your app with zero coordinate fiddling!

http://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetWindow.htm
http://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetWindowRect.htm
http://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetClientRect.htm

Spyxx.exe is available in the Visual Studio 2013 Community Edition, but I have not tried it.