March 4, 2008

OnEvent – Dealing with Indeterminate Dialogs

Filed under: Automation, Scripting — Marcus Tettmar @ 2:34 pm

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:

Send>the_data
Wait>0.5
IfWindowOpen>Verification Alert
  Press Enter
Endif

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
..

SRT>CloseWarning
  Press Enter
End>CloseWarning

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.:
Run>Notepad.exe
WaitWindowOpen>Untitled - Notepad

//assuming the target window is now focused, get it's handle and process name
Let>WIN_USEHANDLE=1
GetActiveWindow>MyWindowHandle,x,y
GetWindowProcess>MyWindowHandle,pid,MyProcessName
Let>WIN_USEHANDLE=0

//now set up the event that is fired when a new window appears
OnEvent>WINDOW_NEWACTIVE,0,0,HandleNewWindow

..
..
.. rest of script here
..
..

//When a new window that does not belong to our process appears,
// set focus back to our window
SRT>HandleNewWindow
  Let>WIN_USEHANDLE=1
  GetActiveWindow>hwnd,x,y
  GetWindowProcess>hwnd,pid,winProcName
  If>winProcName<>MyProcessName
     SetFocus>MyWindowHandle
  Endif
  Let>WIN_USEHANDLE=0
End>HandleNewWindow

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.

February 29, 2008

Annotate PDF Documents

Filed under: General — Marcus Tettmar @ 9:19 am

PDF documents are everywhere, but most of us don’t have the means to edit them. I’ve often received forms or agreements in PDF format and have had to print them off to complete and sign them only to scan them again to send in an email. But there’s an end to this madness in the form of a neat and inexpensive piece of software called PDF Annotator from our friends at Grahl Software. PDF Annotator lets anyone edit directly on top of a PDF file – add notes, comments, corrections, drawings etc. Ideal for filling in forms, adding your notes to technical manuals, or correcting other people’s work.

PDF Annotator will be on Software Deal of the Day on Wednesday 5th March.

February 12, 2008

Take the elevator, not the stairs …

Filed under: Automation, General — Marcus Tettmar @ 4:05 pm

I just love this quote:

β€œThe IT division of First State Bank of Altus was on a theoretical 25th floor and everyone was taking the stairs. The implementation of Macro Scheduler Pro from MJT Net provided us with a much-needed elevator.”

– Garry Petzold, Chief IT Officer, First State Bank of Altus

Taken from our latest case study with First State Bank of Altus, which shows how automation with Macro Scheduler Pro has saved them up to $2000/month on ATM over-withdrawels and over $50,000 per year on staffing costs.

February 7, 2008

January 23, 2008

Macro Scheduler in PC Plus Magazine

Filed under: General — Marcus Tettmar @ 4:20 pm

If you’re in the UK watch out for next month’s issue of PC Plus magazine (issue number 266). A full, unrestricted, licensed, copy of Macro Scheduler 8.0 will be on the cover disc. And inside you’ll find a discount code for version 10 – the latest and greatest.

Macro Scheduler on New Software Deal of the Day Site

Filed under: General — Marcus Tettmar @ 3:51 pm

There’s a new Software Deal of the Day site about town. It is called, appropriately enough, Software Deal of the Day and it can be found at www.software-dod.com.

Each day the site features a different software title with a juicy big one-day-only discount. Unlike other similar sites they also offer a “Second Chance Discount” after the software has been featured.

Macro Scheduler will be offered on Software Deal of the Day on February 4th. So if you haven’t got a copy of Macro Scheduler yet, keep your eyes peeled for the one-day discount.

January 22, 2008

January 20, 2008

New Release – James Isaac

Filed under: Announcements, General — Marcus Tettmar @ 9:38 pm

James Isaac Tettmar James Isaac Tettmar was born Friday 18th January at 0938 GMT. Mother and baby both well. A great start to the year! πŸ™‚

January 17, 2008

Could Automation Save the Economy?

Filed under: Automation, General — Marcus Tettmar @ 1:14 pm

The editorial comment in the latest issue of Computing magazine discusses how IT may hold the key to preventing a recession:

“Financial institutions are looking for IT-enabled efficiency to help them through a sticky patch”

Regular Macro Scheduler users don’t need to be told how much automation can boost productivity and increase efficiency. Just look at how Iowa Laser have made efficiency savings of $75,000 every year since introducing Macro Scheduler to help streamline many important business processes.

In difficult economic times firms need to cut costs, become more efficient and more productive. By automating more of their business processes they can improve reliability and free up important resource to concentrate on their bottom line and become more competitive.

January 3, 2008

Screen Scrape Text Capture Example

Filed under: Automation, Scripting — Marcus Tettmar @ 6:59 pm

In this post I discussed the Text Capture commands and explained what kind of text can be captured.

To try out the text capture functionality launch the latest version of Macro Scheduler. Click New to create a new script and then in the Code Builder locate the “Text Capture Wizard”. Point the mouse at some text while holding the Shift key down.

You can see a video of this in action here.

This Wizard will let you determine whether or not the text you want to capture can be captured with the Text Capture commands. If it is not revealed when you move the mouse over it while holding Shift down then it must not be generated by Windows text out functions. See my previous post for an explanation.