January 17, 2006

Filed under: Automation,Scripting — Tags: — Marcus Tettmar @ 11:35 am

These are my tips for getting started with writing an automation script.  While I’m, writing this with our Windows Automation tool, Macro Scheduler, in mind, these tips will be appropriate whichever automation tool you are using.

The most important thing before attempting to write a routine to automate a software process is to be familiar with the process itself. Run through the full process several times and find the simplest path. If you’re using the mouse excessively try to find keyboard alternatives.

Write down every step you take. Make a note of the keys you press, note down the title of each new window and how long each step takes. Try to determine what indicates the completion of each step. Make a note of it. You’ll end up with a list of keystrokes and window titles. This is the basis of your script. You’re now most of the way there. This list will translate well into a Macro Scheduler script.

I find it is best to break the script down into manageable chunks. Don’t try to write the whole thing at once. Start by just scripting the first few steps. E.g. write the code that opens the app, waits for it to be active and sends the first keystroke. Run it and make sure the process ends up where you expected. Tweak it if necessary. Now add the next couple of steps. Run it again. And so on. Building the script up in this way will iron out issues as you go rather than leaving you desperately trying to hunt down the cause of an error amongst one long script. You can also use the debugger to step through the script line by line.

Use SetFocus! When sending keystrokes Macro Scheduler just simulates what you do when you press keys on the keyboard. When you do that the keystrokes land on the active window. So you need to make sure the window you want the keystrokes to land in is the active window. Use SetFocus to do this. Get into the habit of using SetFocus even after running a program with the Run Program command. When you start a program it nearly always becomes the active window, but other applications can steal the focus. So it’s a good habit to get into to use SetFocus before sending a set of keystrokes.

Try not to use absolute wait times to wait for events to complete. The process may take longer on another occasion and fail. Also remember that when you do something manually you will subconsciously wait for the outcome of each action. A script isn’t quite so clever and unless you tell it to wait for certain actions to complete it will blindly move on to the next step. Always use WaitWindowOpen after running a program or sending an event that causes a new window to appear. This will ensure the script waits for that window to appear before continuing. Use WaitWindowClosed after issuing a command that causes the window to close. There are ways to wait for all sorts of other events to take place too, some more advanced than others. You can wait for pixel colors to change, mouse cursors and windows to change, detect object captions and wait for files, and portions of the screen to change amongst other things. When starting an application with Run Program use RP_WAIT=2 to tell it to wait until the application is ready for input before continuing.

Sometimes you do need a small Wait between events. Sometimes you need to slow down the key send rate. Scripts run faster than a human can type and not all applications can cope with that many keystrokes in such a short space of time. So the odd Wait>0.5 here and there can help. You can also slow down the key send rate with SK_DELAY.

When issuing shortcut keys such as ALT-F for the File menu use lowercase for the underscored character. E.g.:

Press ALT
Release ALT

I have seen some applications fail to recognise shortcuts when Macro Scheduler sends the character in upper case. This is probably because it treats an upper case character send as having the Shift key pressed at the same time. So I always recommend issuing characters used in shortcut keystrokes in lower case.
It is best to avoid mouse events as much as possible, but if you do find you need to use the mouse for a particular action – maybe there’s no keyboard alternative – try to work out the relative mouse coordinates. Use the cursor monitor, set it to relative, and find the position relative to the window. Then use MouseMoveRel. This ensures that the mouse will always click on a position relative to the window rather than on an absolute screen position so that if the window opens in a different position next time around the macro will still work.

Clearly this article is all about automating via user simulation – by sending keyboard events to applications. Often there are better ways to automate an application such as via VBScript/ActiveX or DDE, or by reading data from files and databases. Before sitting down to automate an application by simulating user input stop to consider whether there are alternatives. For example, I’ve seen people write scripts to automate getting data from Excel by sending keystrokes to Excel to copy and paste. There’s no need to go to this trouble when Excel provides a DDE interface and can also be scripted with VBScript. See the sample script that comes with Macro Scheduler for an example of retrieving data from Excel directly. I’ve even seen macros which get data from a text file by driving Notepad with keystrokes. This is unnecessary and cumbersome when all you need to do is read data directly from the text file with the ReadLn command. If you need to get data from a database, you don’t need to send keystrokes to the query tool – you can read data directly into the script using VBScript/ADO/ODBC. Equally you can automate Internet Explorer elegantly via VBScript/ActiveX or with WebRecorder. There are usually at least two ways to automate something. Before writing your script stop and question whether there’s a better way.

Before writing your first script read Scripting Windows for Beginners in the Macro Scheduler help file/manual.

And browse the examples at the Scripts & Tips forum.

And if you don’t already have Macro Scheduler, you can download a trial version here.


  1. Such a powerful, powerful product that can do so much! Truly a wonderful thing.

    However, I think many people that use it are not programmers, and may be new to the Macro interface windows as well – so a much more robust tutorial section would be great. This blog area had some terrific tips!

    My problem, I’m pretty tech saavy, but I had to wade through endless forums to copy & paste a huge number of scripts that would aid in my particular needs for what I want to do. Spent maybe 3 hours and ended up with a bunch of text files that I intend to use status quo or modify to do my needed automation. This is so time consuming and frustrating because I still couldn’t find simple answers or scripts to some of my most in-depth questions, like if I make an executable that reboots the computer, after logging back in – how could I get scheduler to continue scripts off a checklist, continue rebooting & installing, and finally stop & permanently delete itself from the computer. (I know it’s an .exe so just have it delete itselft yeah) but what about the rest?

    I think it would be the most INCREDIBLE VALUE (in the world) TO ALL CUSTOMERS (and not just my needs) if a webpage were developed that made available ALL of the scripts posted in ALL of the forums! Sort them by type or function, or give options to sort them to users. Kind of a huge library of all scripts you would ever need to do most everything! This way a new user with a big project (like me) can get a large number of scripts that do the basic things relevant to their needs, quickly and not have to wade through endless forums or create a large number of custom scripts. Why re-invent the wheel?

    Getting ready to plunk down the $ for 8.0 full package, but was just overwhelmed by the amount of work it is going to take me to create the scripts I will need to automate installing software packages, running them to configure multitudes of settings, rebooting, and customizing XP user profiles.

    One burning thing I could not find in any forum, how to position desktop shortcut icons at specific locations on the desktop. Or how to create another user, say a limited user account, then log-off, log-on to the new account, change a bunch of default windows settings, log back off and back into the administrator account. Mainly we are 98% XP tech support company (as side note) Sorry if I posted this in wrong place but forums are crowded, and email directly to Marcus might not allow feedback here to this blog area for others who might be using schedular with the similar issues that I mentioned.


    Comment by Cosmo — January 18, 2006 @ 11:13 am

  2. A web page with a list of all the script snippets is a great idea and already exists – it’s the Scripts & Tips section of the forum here.
    The trouble is that the majority of the scripts and code snippets in the rest of the forum are examples written by other people to explain how to do something. Often they are untested pieces of code in answer to a poster’s question. Many are dependent on third party applications. We wouldn’t be able to vouch for them as useful independent scripts. They can only describe key concepts rather than solve specific problems.

    I do understand that when you have a complex process to script you need to spend time figuring out ways of doing what you want and finding the answers can take time. The problem with a tutorial is that it still won’t solve YOUR problem. A tutorial can only have generic examples which will work for everyone, and can only explain the key features and tools that will be useful in every scenario. Unfortunately you need to take these building blocks to start building your solution. Even if we happened to have a tutorial which solved all your problems, someone else will still have unanswered questions. So we try to concentrate on the most important concepts which will be used in every case. When I started programming in C++ I had exactly the same problem. And still I have found no book or tutorial which shows me code to program a scripting language. The thousands of C++ books that exist can only describe key concepts and code. No one can predict what someone is going to do with the language.

    There is one way you can get help for your specific scnario. Email us. Post a message to the forum. The possibilities of Macro Scheduler are endless and the number of solutions is infinite. It is impossible to provide examples which will solve everyone’s problem without the user needing to do a certain amount of work. But if you email our support team, and/or post a message to the forum, describing what you want to achieve we can talk through your precise requirements and pull all the pieces together and get you well on your way.

    We are working on producing training material and certification which would help enormously. Watch this space …

    Comment by marcus — January 18, 2006 @ 11:42 am

  3. Marcus,

    Thanks for replying, although I’ve only attempted MJT Net contact twice, both times your response was extremely fast and professional. Customer support is a HUGE plus in my opinion for any company to have, and I feel like I can count on it from mjtnet. I just plunked down my order for your 8.0 pro version.

    After I invest what looks like a ton of hours to create all the scripts I need to accomplish the project I have in mind for Macro Scheduler, would you object at all to me creating a wepage (perhaps on my company website) that lists all of the available scripts in all of your forums, for simple downloads (un-zipped maybe just .txt files) so that people wouldn’t have to wade through forums to get valuable scripts? But of course with a heavy disclaimer that they are not certified, un-tested, use at your own risk kind of thing?

    I excavated a lot of the scripts I need to modify for my use to make my project fly, from several other forums in addition to the scripts & tips forum.

    Ok, now to go spend my weekend learning even more about Macro Scheduler use, and you’ll probably hear from me (in the forums) requesting how to position icons on the desktop & use an .exe to automate software installs off a checklist, configures, and reboots, repeat several times, etc. (unless of course during my self training I can figure those things out.. 🙂

    Thank you for creating such a wonderful, and powerful product!

    Comment by Cosmo — January 19, 2006 @ 4:34 am

  4. I have no objections at all to you building a script repository on your site. That would be great. Go for it.

    Hmmm. Changing desktop icon positions. That’s an unusual request. Let me think about that and maybe I’ll post something later ….

    Comment by marcus — January 19, 2006 @ 7:53 am

  5. […] Cosmo asked in his comment on How to Start Writing an Automation Script how he could set the position of a desktop shortcut. This is an interesting question that hasn’t come up before and it took me a while to realise how it could be done. You may wonder why you’d even want to do it. Well if you’re automating software installations for your end-user or customer machines you may want to make sure the desktop icons are always in the same place. You may want to put a link to your support site in the top right of the screen, for example, so that your customer can always find it. […]

    Pingback by Windows scripting and Windows Macro Automation utilities - automate your PC with Windows scripting for Win9x/NT/ME/2000/XP. — January 19, 2006 @ 9:42 am

RSS feed for comments on this post. TrackBack URL

Leave a comment