Marcus' Macro Blog

Tips and News on Macro Recording and Automating Windows with Macro Scheduler

Archive for the ‘Scripting’ Category

Sending Email via Office 365

Sunday, October 29th, 2017 by Marcus Tettmar

We’ve had a few questions lately about how to send email using the SMTPSendMail function via the Office 365 SMTP server.

Until Macro Scheduler 14.4.02 which we have released today this wasn’t possible as explicit TLS was required and SMTPSendMail did not support it. We have now added this. So as long as you have updated to Macro Scheduler 14.4.02 (and you have a valid Office 365 account) here’s the code you need to send an email via the Office 365 mail server:


See also:

Regular Expressions (RegEx) In Window Commands

Tuesday, October 3rd, 2017 by Marcus Tettmar

Did you know you can use Regular Expressions inside any of Macro Scheduler‘s window functions (e.g. SetFocus, WaitWindowOpen, WaitWindowClosed) so that you can match a window title using RegEx?

As an example someone recently asked us how they would match a window from an old piece of software where the window title was always the letter “b” followed by any 8 digits, e.g. b23425461. Macro Scheduler uses the PCRE syntax, so to match a window title like this we could use this expression:


If you’re new to RegEx this means: match from the beginning of the string (^), then match a “b” character followed by any digit (\d) 8 times. {8} means match the proceeding token 8 times. So:


I’ve posted a little about Regular Expressions before. This post links to some useful resources if you are new to RegEx.

Regular Expressions for Dummies
My Most Used RegEx
Remove Tags From HTML with RegEx

OCR Functions Anyone?

Tuesday, September 19th, 2017 by Marcus Tettmar

A sneaky peak of some new Screen OCR functions we have in development:

Please note that this feature is currently in development and therefore the syntax and final implementation may differ on release.

Parsing XML with XMLParse and XPath

Wednesday, July 26th, 2017 by Marcus Tettmar

Prompted by a recent forum post from Phil Pendlebury the other day I have written a new article on using XMLParse in the knowledge base:

Parsing XML with XMLParse and XPath

Hope it’s helpful. I’m sure you’ll have more questions when you start using it in anger. So when you do, be sure to head over to the forums and ask for help. :-)

Script Development Services

Thursday, June 1st, 2017 by Marcus Tettmar

Did you know that we offer script development services? If you’re pushed for time or need a helping hand we can usually be of assistance. We often help people who don’t have the time or resource to build the automation themselves, or just want to get a jump start.

What sort of things do we do? It’s pretty much anything that needs automating, but a common scenario is data entry – usually reading data from Excel and entering it into either a web form or desktop app.

Recently we’ve written a script for a government agency which searches through hundreds of files looking for patterns and attaching them to emails; We’ve written a data entry script for a large oil company which reads through Excel and outputs the data to a web form; Helped a hospital automate entry of patient referrals into a web based system. We’ve reformatted CSV files; scraped data from a website; extracted data from PDF and output to Excel; Extracted patient data from an online dental patient database; assisted with extracting reports from an ordering system; and more.

So you can see we do all kinds of things. We have been automating processes for 20 years. If you have a project you could use some help with give us a shout and we’ll see what we can do.

HTTP Request With Custom Headers using Python

Monday, November 14th, 2016 by Marcus Tettmar

As you will hopefully already know by now we added the ability to use Python code a while back in Macro Scheduler 14.2.

A customer recently had a need to retrieve some data from a web service which requires some custom authentication headers to be sent with the request.

It’s really easy to do this using Python. Here’s a made-up example:


import urllib2

custom_headers={"X-Custom-Application-Id" : "4020c4b37ead0a834c0010a9",
                "X-Custom-REST-API-Key": "49eade9c-d70a-4d3d-b552-4bd9f05966fc"}

request = urllib2.Request(url, headers=custom_headers)
contents = urllib2.urlopen(request).read()

//Load the Python code to a variable

//Run the code and request the value of the contents variable ...

//content of request is now in "contents" variable.

For more on Python in Macro Scheduler see here and here.

Finding Window Titles You Cannot See

Thursday, September 15th, 2016 by Marcus Tettmar

Someone emailed today saying they were having problems trying to automate Internet Explorer 11 because it didn’t seem to have a window title.

Actually IE11 does have a window title. Each tab has a different window title. But you don’t see the title in the main title bar of the application.

By default applications show the window title in the title bar. Hence it’s name. But some apps manipulate the appearance of their title bar so that it doesn’t look like a regular Windows title bar. Indeed some apps have all borders removed so that you can’t SEE the title bar. But in all cases, the window will still have a title (unless it’s an empty string!).

So if you can’t see the window title, how do you find out what it is? Well, with Macro Scheduler there are several ways to find it. One is with the View System Windows Tool, which shows a list of all the windows currently available on the system, showing their captions and class names. Another is to use the Code Builders.

Here’s a video demonstrating these two methods. It also shows how I use a substring window match:

Toast Notification Window for your Scripts

Wednesday, September 7th, 2016 by Marcus Tettmar

Over in the forums dtaylor has posted a script library which allows you to show a small notification window in the lower right of your screen. Useful for reporting on progress during your script, in loops etc.

Grab it here.

OnEvent – Dealing with Indeterminate Dialogs – [Repost]

Wednesday, August 17th, 2016 by Marcus Tettmar

This is a repost. The original article is here.

Most of the time when we are automating a process we are able to predict the sequence of events. We are working with a deterministic process and a linear flow of actions. But there are occasions when things can happen during a process that we cannot predict precisely. E.g.:

  • We might know that a dialog or window may appear sometime during the process, but we cannot predict exactly when that will happen.
  • We may have a situation where after entering some data into a text field a dialog may, or may not appear.
  • There might be some other software running on the PC which randomly pops up an error box. And we need a way to clear that when it happens.

There are a number of ways we can deal with such situations.

Window Event Schedules

If you have a situation where a known window can randomly appear – say a known error box – which always has the same window title, the simplest approach is to use the Window Event schedule in the Advanced Scheduling properties. Simply create a macro which closes the box – perhaps all it has to do is press enter – and specify the window title under Advanced Options in the macro properties. Then whenever Macro Scheduler sees this window it will run the macro and clear it.

Synchronous Coding

In the case where a window may, or may not appear after entering some data into a field, say a data validation dialog, we could just deal with this after sending the text, in regular fashion – something like:

IfWindowOpen>Verification Alert
  Press Enter

So we simply send the data then IF the verification window appears, close it. But what if you have hundreds of data fields to enter? Dealing with each one would involve a lot of extra code.

OnEvent Window Event Handlers

Another way is to use the OnEvent function to create an event handler in your main script. There are three types of window events that can be monitored with OnEvent:

  • WINDOW_OPEN – monitors a specific known window title, or window title substring
  • WINDOW_NOTOPEN – fires the event handler when specified window closes
  • WINDOW_NEWACTIVE – fires the event handler when there’s a new foreground window

OnEvent is used to create an “event handler” which is just a subroutine which will be executed whenever the event occurs. So, for example, using OnEvent you can tell the script to run a subroutine whenever a specified window appears, whenever that may be, while the rest of the script is executing.

So let’s say we are working with an application which could, at any time, pop up a warning box titled “Connection Error”, and this can be cleared just by pressing enter to hit the default OK button:

OnEvent>WINDOW_OPEN,Connection Error,2,CloseWarning

.. rest of script here

  Press Enter

Of course there are a whole load of other things you can do. We may have a window whose title is always the same but the content differs and we need to react according to the content. In this case our event handler subroutine would have extra code in it to determine which type of dialog it is. We might do this using the text capture functions to read the text from the dialog, or using Screen Image Recognition to check for the presence of an object.

Maintaining Focus

Here’s an idea for an event handler which ensures the target application is always focused. If another application should steal focus at any point during the running of the script, it just grabs focus back again. It’s always good advice to use SetFocus before sending text. But if you have thousands of Send commands and want to slim down your script and make it more readable you could use this approach. Anyway, it’s just an example:

.. your code here to start and focus the app you want to automate, e.g.:
WaitWindowOpen>Untitled - Notepad

//assuming the target window is now focused, get it's handle and process name

//now set up the event that is fired when a new window appears

.. rest of script here

//When a new window that does not belong to our process appears,
// set focus back to our window

Note how this code gets the window handle and process name of your target window. Then whenever a new window appears the HandleNewWindow subroutine is fired which gets the process name of the active window. If the process name of the new active window is not the process name of your target window (i.e. the new window belongs to some other application) it sets focus back to your original window.

I hope this gives you a useful introduction to OnEvent event handlers and how they can be used to run code at any point during the script in response to events. OnEvent can also be used to detect files, dialog events, dialog changes and keyboard and mouse actions. For further information please see OnEvent in the help file.

Capture Screen Text using OCR

Friday, August 5th, 2016 by Marcus Tettmar

Here’s a way to get screen text from any application – even from an image – using OCR and a free open source tool called Tesseract.

First, you need to download and install Tesseract. You can get it here.

Tesseract is a command line utility. The most basic syntax is:

tesseract.exe input_image_file output_text_file

So you could call it from a Macro Scheduler script something like this:

//Capture screen to bmp file - you could instead capture only a window or use FindObject to get coordinates of a specific object

//run tesseract on the screen grab and output to temporary file
Run>"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" "%SCRIPT_DIR%\screen.bmp" "%SCRIPT_DIR%\tmp"

//read temporary file into memory and delete it

//Display the text in a message box

This example simply captures the entire screen. You probably wouldn’t normally want to do this. Instead you could capture a specific window:

//Capture just the Notepad Window
SetFocus>Untitled - Notepad
GetWindowPos>Untitled - Notepad,X1,Y1
GetWindowSize>Untitled - Notepad,w,h

Or even a specific object:

//capture just the editor portion of notepad ...
SetFocus>Untitled - Notepad
GetWindowHandle>Untitled - Notepad,hWndParent

Either way you then have a screen bitmap you can pass into Tesseract.

Once you’ve retrieved the text you would probably want to parse it, using e.g. RegEx. Here’s an article on a RegEx expression useful for parsing out data.

  • You are currently browsing the archives for the Scripting category.

      • Archives

    • Categories