Windows Desktop Applications Automation using AutoIt

14-Jun-2018

Guest Post by Ahmed Memon

AutoIt is a language & framework to automate (mainly) Windows GUI application interactions. Though it has its own language but there are wrappers available for Python, C# etc too.

This write-up target developer who want to automate Windows Desktop Applications but either do not know the right tool or want to enhance their skills using AutoIt.

Just like with anything, there is either informed or amateur approach towards solving a problem. Developers who have never written Windows GUI applications will write AutoIt code that will depend too much on interactions with active GUI. While we can get maximum out of AutoIt when we rely less on such interactions. This amateur approach creates a real issue when we have to interact with multiple desktop applications within same AutoIt script.

One solution to address these issues is to use _WinAPI_PostMessage function, where applicable. We will primarily use this function to interact with menus, otherwise, our interactions may occur on the wrong window.

Intent:

Want to automate Notepad;

  • change display font
  • write some text and
  • save file

Downloads:

GUI Inspection:

In order to control GUI elements, we will need the Windows Name, Controls ClassNameNN & Automation Ids of Notepad application. If you don’t know how to find them using AutoIt Window Info and Inspect tools, watch this video (enable captions, if you want).

 

AutoIt Script

The code snippet is given here:

Explanation:

WinAPI.au3 & WindowsConstants.au3 contain definition or constants that will help to communicate particular command to window without visible interaction.

Global variables $hWnd$hWndFont & $hWndSaveAs will hold handles of Notepad Application, Font window & Save As windows, respectively. While s in $sFontName & $sFileName indicates that they are string. The advantage of following Naming Convention is that it helps create team factor in individuals thus a guess work can be utilized when multiple people are working on same code. Another benefit is that even if you open code after months, you can instantly recognize why particular variable was used.

$__IDM_FONT & $__IDM_SAVE are Menu IDs for sub-menus Font and Save respectively. We’ve identified them using Inspect tool. $__IDC_FONT_NAME is the Control ID for text field for font name.

Run("notepad.exe") is use to open the Notepad application. We can further improve it by using Run("notepad.exe", "", @SW_MAXIMIZE), this will make sure when Notepad is opened up, its window is minimize by default, especially useful when the active window will disturb other application or if the computer (where this AutoIt script is being executed) distracts the person using it.

WinGetHandle() is used so that we can send control actions or PostMessage() to just related Window.

_WinAPI_PostMessage() is a wraper to Win 32 API function PostMessage(), this will click sub-menu item without need to have Notepad activated and visible. The function definition is within WinAPI.au3, that is why it is included at the top. WindowsConstants.au3 contain the associated integer for $WM_COMMAND constant

We’ve used ControlSend() function, with particular window handle, so that control command is executed within specific window, otherwise Send() function just send control command, even if (for any reason) your target window is not visible (minimized or something), the control might execute on wrong target if you use Send().

Sleep() functions are used multiple times, because it takes few milliseconds to load or close a window, for example, Font & Save As windows open after some milliseconds, same while Font window is getting closed. If we don’t use them the automation might not work properly.

Known Limitations:

The code is as brief as possible, there is room to enhance and make it more professional

  • We can open Notepad minimized
  • The Font & Save As windows will be maximized by-default even if Notepad is opened minimized, therefore we can use WinSetState() function to explicitly minimize these windows
  • If the file already exists, we can add few lines of code to test if it is asking for overwriting and then perform the desired action
  • We can close Notepad after the automation using WinClose()
  • If your script is going to be deployed on the variety of Windows operating system versions and different hardware specification machines, then add checks to see if an intended control has successfully executed. For instance, if I’ve to check if Notepad is really opened after Run("Notepad.exe") I would add something like:

Code Execution

Following animated gif shows what happen when you execute this code:

Further Reading

For those who are interested in learning more should visit AutoIt’s official wiki & forum or search over StackOverflow, however, make sure that you are coming up with intelligent (specific) question.

Credits

  1. Matt has pointed towards the use of PostMessage() Menu IDs
  2. AutoIt Forums got a bunch of helpful guys who respond promptly.

This article was originally published here.


If you like my work then you can donate me in bitcoins