Home of Macro Scheduler - Macro Tools and Automation Software
Marcus’ Macro Blog
Mostly tips, tutorials, articles and news about Macro Scheduler & Windows Automation
Download Macro Scheduler
Free 30 Day Trial

Top Tips for Reliable Macros

October 27th, 2008 by Marcus Tettmar

Over the years I’ve helped many people improve their automation scripts. The most common two causes of scripts failing to work correctly and consistently are timing and focus issues. Here I’ve tried to summarise the main things you should do to make your scripts more reliable.

Try to avoid mouse clicks.

Mouse clicks require screen positions and while there are ways to specify window relative positions, or find the position of objects, if you can use the keyboard instead, it’s a lot less hassle. And most of the time you CAN use the keyboard instead of the mouse. You can TAB from object to object, field to field. You can press ALT to see the shortcut keys and select menu items and click buttons. You can toggle radio boxes with the SPACE bar. You can CTRL-TAB through tab pages. You can position the cursor, highlight text and copy to the clipboard, all with the keyboard. You can often select an item in a list box or combo box simply by typing the item you want to select.

For more info on keyboard shortcuts see:
http://www.mjtnet.com/blog/2006/01/16/keyboard-shortcuts/

Don’t automate mouse clicks or keystrokes just to start an application!

I’ve seen people write code to locate a desktop icon, then double click it, and even double click on a Windows Explorer icon, click through a series of folders and finally double click on an EXE. While this may work just fine, it’s rather long winded and seems a bit pointless when you can run an app with just one line of code. A desktop shortcut is just a shortcut to another file, usually an executable, sometimes with command line parameters. Right click on the shortcut and select Properties to see the target. Copy that and paste it into a Run command. E.g.:

Run>"C:\Program Files\Mozilla Firefox\firefox.exe"

One line of code is much easier to read, easier to maintain later and easier to debug. It’s also faster.

The other day I saw a Citrix app which the user would normally start by double clicking on the desktop icon which would present an Explorer style window appear. The user would then select one of the apps from the list in the Explorer Window. Rather than use image recognition to find the icons and send a bunch of mouse clicks we found that if you right click on one of the apps you could create a desktop shortcut. We did that and examined the shortcut to see what the actual command line was to run that app. We probably could have read the docs instead but we used the desktop shortcut to tell us and simply copied the target into Macro Scheduler, before deleting the desktop shortcut. We now had one line in Macro Scheduler to run the app directly.

How to automate a Save As or File Open dialog.

Need to manipulate a Save As or File Open dialog box? It seems many people think you HAVE to use the mouse to click through a series of folders to find the location of the file. WRONG! Ignore the folder browse area of the file dialog. All you have to do is type a full path into the file name box and hit Enter. Doesn’t matter what the current folder is that the dialog is showing you. Just type the path of the file you want:

WaitWindowOpen>Save As
Send>g:\some folder\some sub folder\some file.txt
Press Enter

That’s it! No mouse clicks necessary!

Make the script wait for events to complete.

Remember - scripts run faster than humans can type! Unless you build delays into the script it’ll just issue each step milliseconds after the last one. But apps are designed for humans who type more slowly and subliminally wait for events to complete - we don’t start typing into a window until we can see the window on the screen. We know that when we select File/Save As we need to wait for the Save As box to appear before we can enter the filename. So we need to make scripts do the same thing.

The most important command you should use in EVERY script is WaitWindowOpen. Whenever we issue a command which will make a new window open, use WaitWindowOpen to make the script wait for that window to appear before it continues:

//select File/Print
Press ALT
Send>fp
Release ALT

//wait for the print dialog
WaitWindowOpen>Print

//Press enter key
Press Enter

Without that WaitWindowOpen command the Press Enter will most likely be sent long before (in computer time) the print dialog has appeared. So the document won’t print.

Equally, use WaitWindowClosed when you do something that causes the window to disappear.

Sometimes it’s not a window you need to wait for, you might want to use WaitScreenImage to wait for any visual cue, or WaitScreenText, or WaitCursorChanged. Or you might need to wait for a file to appear, so you might create a little loop with IfFileExists in it …. it could be anything. Determine what events the script needs to be aware of and make it wait for them.

Be Specific.

The window functions can take an asterisk to look for a substring match for situations where the window title is not static, or not completely known. E.g.:

WaitWindowOpen>Internet Explorer*

But try to be as specific as you can. There are many hidden windows on your system so if your text matches one of those first it’ll stop at that one, not the one you want. Make the text more specific if possible, and/or use WF_TYPE to make Macro Scheduler look for only visible windows.

As an example, the following could return too quickly:

WaitWindowOpen>Microsoft Excel*

Why? Because “Microsoft Excel” is the window of the container application. But just after Excel starts you’ll see a window title something like “Microsoft Excel - Book1″. When the app starts, for a split second, faster perhaps than you can detect with the human eye, the window title might first be “Microsoft Excel” and THEN “Microsoft Excel - Book1″. So the above WaitWindowOpen line might return too quickly. You might not know what the first workbook will be, but you could do:

WaitWindowOpen>Microsoft Excel -*

Be as specific as you can for the final window title that you expect to see before you start automating it.

Slow scripts down! You can always speed them up later.

So you’ve followed the above advice and you’ve made the script wait for events to complete. You may still need some small delays. Let’s say you’re sending a bunch of keystrokes to a form, you’re tabbing through a window sending data to each field:

Press Tab
Send>Firstname
Press Tab
Send>Surname
Press Tab
Send>address
...

It’s not working? It works sometimes but not others? Here’s what is happening. The app is being overwhelmed by those keystrokes. It might do some “OnChange” style processing - as text is entered into a field it could be doing some processing internally. As mentioned before the script runs way faster than a human can type. The text is being sent to the app too fast. In real life there’d be a short delay between fields. So slow the script down and give the app a bit of a chance to catch up:

Press Tab
Wait>0.5
Send>Firstname
Press Tab
Wait>0.5
Send>Surname
Press Tab
Wait>0.5
Send>address
...

Half a second is probably all you need. It may not even need to be that much. But I always recommend that while you are developing a script and testing it you should err on the side of caution. Use plenty of waits. Get the script working. You can always reduce the waits later once you know it’s working. And if your script is going to be run unattended, say at night, then does it matter if it takes 5 seconds longer? Who’s watching it?

You can also use SK_DELAY to slow down the key send rate where you are sending characters together. SK_DELAY takes milliseconds:

Let>SK_DELAY=20
Send>Hello World

Sometimes you need to press tab a number of times to get to a field so you might do:

Press Tab * 6

But this sends the tabs in quick succession, and as I’ve said, the app may be requiring a bit of time between fields. So you might need to do the following instead:

Let>k=1
Repeat>k
  Press Tab
  Wait>0.2
  Let>k=k+1
Until>k=6

Make sure the correct window is focused!

When you press a key on the keyboard that keystroke will land in whatever the active window is. That’s the way your computer works. If Notepad is active and you type “Hello”, you’ll see “Hello” inside Notepad. If some other app is active … I don’t know what “Hello” would do in ficticious app number 2! Macro Scheduler controls your computer, so the same thing happens. That’s the way your computer works. So when you make Macro Scheduler send some text you’re going to want to make sure the correct window is focused first:

SetFocus>Notepad*
Send>Hello World

Build scripts in chunks.

When I develop a new macro I do it a few lines at a time. The first thing I might want the macro to do is open an application and wait for it to be ready. So I’ll do that bit manually and work out what paths, window titles and keystrokes I need to make that happen. I’ll write those lines of code. Then I’ll run the script and see if it gets me to where I want to go. Once those lines are doing the right thing I can work on the next phase. I’ll do a few more steps, add those to the script, then close the app down and run the script again.

Don’t try to build everything in one go, break the routine down into chunks and work build it up slowly.

Use the debugger!

The debugger is your friend. The debugger lets you step through the script line by line and watch the script working. You can see the value of variables as they are created and modified. So you can see just what each line is doing and find problems instantly. For a tutorial on using the debugger see:
http://www.mjtnet.com/blog/2006/05/17/use-the-debugger/

There’s also the variable explorer (Tools/Variable Explorer). Quite handy for finding variable related problems without having to run the script.

___
More resources:
http://www.mjtnet.com/blog/2006/01/17/how-to-start-writing-an-automation-script/
http://www.mjtnet.com/blog/2008/03/21/round-up-of-learning-resources/

SOAP and WSDL Interaction

October 24th, 2008 by Marcus Tettmar

A recent post in the forums asks how to consume a SOAP service with Macro Scheduler. View the post for some techniques and examples.

Workflow Designer 1.2 Update

October 23rd, 2008 by Marcus Tettmar

MacroScript Workflow Designer 1.2 is now available.

Workflow Automation - Click to View a Video Demo

Workflow Designer lets you create flowcharts and attach Macro Scheduler code to each step. You can then run your flowcharts.

This update fixes a few bugs including:

  • Fixed: Pressing stop button could sometimes cause a crash
  • Fixed: Non-visible blocks in large diagrams not being executed

    Evaluation Downloads | Registered Downloads

  • SSH with Macro Scheduler

    October 22nd, 2008 by Marcus Tettmar

    As you know Macro Scheduler has Telnet, FTP and HTTP functions built in (and with v11 FTP/HTTP will support SSL). But what if you want to run some other secure protocol such as SSH?

    We’ve taken the view not to reinvent the wheel and add every possible secure communications protocol to Macro Scheduler, especially as there are some great components already available that you can use in your scripts. Rather than add bulk that won’t always get used, I’d prefer to leave the security stuff to the experts and use a plug-in component when needed, such as one of the Active-X components from WeOnlyDo Software.

    WeOnlyDo make Active-X components for all kinds of things, such as SSH and SFTP as well as the more general protocols Telnet, POP3, SMTP, amongst others. I recently downloaded their SSH component to help out a customer who needed a macro to access an SSH server, and was impressed with how easy it was to implement.

    You can download the SSH component here.

    Here’s an example I put together which logs into an SSH server, changes directory to /usr/bin and performs a directory listing, returning and displaying the results:

    VBSTART
    Function SSHExample(server,username,password
      Dim ssh, a, resp
    
      Set ssh = CreateObject("WeOnlyDo.wodSSHCom.1")
    
      ssh.HostName = server
      ssh.Login = username
      ssh.Password = password
    
      ' set Blocking to true or false - blocking means the script is synchronous and waits for each method to complete
      ssh.Blocking = True
    
      ' Protocol: 0: Raw, 1: Telnet, 2: SSH1, 3: SSH2, 4: SSHAuto (Auto Negotiate)
      ssh.Protocol = 4
    
      ' remove ANSI codes from received data
      ssh.StripANSI = true
    
      'now connect
      ssh.Connect
    
      'wait for initial prompt
      ssh.WaitFor("regex:[\]\$] $”)
    
      ‘issue a few commands, returning the output from the final one as the function result
      ssh.Execute “cd /usr/bin” & vbCRLF, “regex:[\]\$] $”
      SSHExample = ssh.Execute(”ls -al” & vbCRLF, “regex:[\]\$] $”)
    
      ‘finally disconnect
      ssh.Disconnect
    End Function
    VBEND
    
    Input>server,Server:
    Input>username,Username:
    Input>password,Password:
    
    // here we call the above VBS function to connect and run the command “ls -al”
    VBEval>SSHExample(”%server%”,”%username%”,”%password%”),response
    
    //output the response
    MessageModal>response

    Notice the Execute statements above which execute a command and wait for the specified prompt to appear. The regex is simply waiting for any “]” or “$” found at the end of a line, which in my case signifies a prompt. Instead of using regex you can provide the exact prompt.

    The component is nicely documented in the help file, with useful examples, so you should find it pretty easy to get up and running.

    If you use any other components that work nicely with Macro Scheduler, let me know.

    Beta Updates

    October 21st, 2008 by Marcus Tettmar

    Macro Scheduler 11 Beta build 015 is now available. You can grab it from your account now. For details of fixes see: http://www.mjtnet.com/usergroup/viewtopic.php?t=5018

    If you download the beta please keep an eye on the Beta Forum for update announcements. Each build will be announced there. Please also report any issues to the beta forum.

    Macro Scheduler 11 Beta Available

    October 15th, 2008 by Marcus Tettmar

    Having gone through a period of closed beta testing Macro Scheduler 11 Beta is now available to all registered customers. All registered users can log into their account and download the beta now.

    Just a summary of some of the main new features:

    • Unicode support in the UI as well as the script language.
    • A superb new script editor with better syntax highlighting, code folding and persistent bookmarks.
    • The script editor and macro properties windows have been combined into one - much easier to use - with everything under the one roof.
    • SSL/TLS support for the FTP functions as well as support for more flavours of FTP server.
    • SSL support for HTTPRequest.
    • Ability to easily embed any binary file into scripts and export them at runtime.
    • Ability to compile macros to console apps in order to write to STDOUT.

    You’ll find a fuller list and release notes here. Please read the release notes before installing.

    Please help test this version and report any issues to the Macro Scheduler Beta forum.

    Questions:

    When will the beta expire? The beta expires 31st December 2008.

    So, when will Macro Scheduler 11 be released? All being well beginning of January 2009, possibly sooner.

    And how much will it cost? Those with valid upgrade protection will get v11 at no additional cost. Otherwise you will need to purchase an upgrade. Current upgrade prices can be viewed on our upgrade page. There are no plans to change these prices at this stage. If you have v10 but don’t have upgrade protection you can purchase it by logging into your account.

    Packaging files with your EXEs

    October 6th, 2008 by Marcus Tettmar

    Every now and then people ask me how they can distribute other files along with their Macro Scheduler executables, such as DLLs or image files that your script requires.

    All software publishers face this problem and usually use an Install file creator, such as the free Inno Setup. You create a package of files and define how they are installed as well as what shortcuts to create etc. Most software you download is installed using software like this.

    For small packages you might get by with a simple zip file, or, better, a self extracting zip file. WinZip will create one of these for you. There’s also a tool that already comes with Windows called iexpress.exe. Click Start/Run and type iexpress.exe and hit Enter.

    This starts the IExpress Wizard which walks you through creating a self-extracting or self-installing package. It’s very simple to use. Just choose the files you want to be included in the package and select which one should be run on completion. You end up with an EXE you can give to your users/customers which when run will extract the files to a temporary folder and run your main script executable.

    Another way is to embed the files directly into your script and have your script extract them at runtime before it does anything else. Dick Lockey describes a way you can do that here. The next version of Macro Scheduler, version 11, makes this easier. It will include a tool to import any binary file into the script and a function called ExportData to create the file at runtime. If you use this method remember to write the files to somewhere you have access to. In a least privileged user environment that won’t necessarily be the EXE’s program folder. The temp folder might be safest. And if you do that, delete them afterwards.

    Macro Scheduler 11 Progress Update

    September 26th, 2008 by Marcus Tettmar

    Well we’ve been busy since my last post on code folding. The main changes include:

    * A completely overhauled script editor with even better code folding and bookmark support and improved syntax highlighting. The advanced editor and macro properties windows have been combined into one to make things much simpler and give easy access to things like the custom dialog designer and the debugger.

    Macro Scheduler 11 Editor

    * Macro Scheduler 11 has full Unicode support. Finally use any language characters in your scripts, detect Unicode text and output Unicode text.

    * Competely overhauled FTP functions with added TLS/SSL support and support for more flavours of FTP server.

    * Added SSL support to HTTPRequest

    * With v11 you can compile macros in “console app” mode and write to STDOUT

    * We’ve added the ability to embed any kind of file in a script, and export it at runtime.

    * And more.

    We’re just starting the initial stages of closed beta testing. Hopefully if all goes well we’ll be able to open the beta up in a month or so. In the mean time I’d be interested to hear from anyone with a need for TLS/SSL FTP support. If you have a TLS/SSL enabled FTP server and would like to do some testing please let me know.

    Did you know you can get paid for selling our software?

    September 13th, 2008 by Marcus Tettmar

    You too can sell Macro Scheduler through our Affiliate program.

    We are offering commissions of 20% on all sales made through your website. Through our Affiliate partner Shareasale.com you can join up immediately and start selling today. There are no set up fees and signing up is simple.

    Three easy steps to revenue:

    1. Sign up with www.shareasale.com
    2. Follow their instructions to place the Macro Scheduler link on your site
    3. Using Cookies and your Shareasale.com ID anyone who buys Macro Scheduler having come from your site earns you 20% commission

    How much could you earn?

    Single user licenses start at $115 and our 25 Pack Enterprise license is $4600, so you can earn from between $23 and $1150 per sale, and that’s not including popular options such as upgrade protection.

    For more info see:
    http://www.shareasale.com/shareasale.cfm?merchantID=10663

    How to move to a new PC painlessly

    September 8th, 2008 by Marcus Tettmar

    I recently bought a new PC. It’s one of those things I always put off. The thought of installing all my software again, finding the original disks or download links, copying over all the data and settings, and getting the new PC mirroring the old one sends shivers up my spine. It always seems to take days, and then you usually find some obscure component is missing and something just doesn’t work properly. Especially when it comes to getting development environments running in the same way.

    Vista and XP have a way to copy documents and settings, but that doesn’t help with the software. So I had a look around at other solutions that might help.

    I discovered that Acronis offers a plugin for True Image Echo called Universal Restore which is supposed to allow you to restore a backup of your PC to any other PC. In doing so you end up with an exact replica of the original PC. It works by detecting the hardware of the new PC and injecting the correct drivers into the Windows Hardware Abstraction Layer during restore. Sounds pretty amazing. I read a number of very positive reviews.

    Unfortunately I wasn’t able to get it to work. I kept getting errors during restore. But I think my unusual configuration maybe to blame. I would still recommend Acronis True Image for backups though. I didn’t really want an exact mirror image anyway - just specific applications and folders.

    Then I found out about PCmover from Laplink. Their website says:

    PCmover is the only migration utility that moves programs, files, and settings from your old PC to your new PC. Simply install PCmover on both your old and new computers and go! PCmover will determine which programs, files, and settings need to be moved, and when the transfer is complete, your new computer will have the personality and functionality of your old PC plus all of its own pre-installed software. Works with almost any Windows operating system, from Windows 95 to Vista.

    To be honest I was a little skeptical that it would work. If you’ve ever tried to just copy an application folder from one place to another you’ll know that it rarely works as there are so many other dependencies. So I was interested to know whether PCmover would live up to it’s claims.

    Unfortunately there is no trial version. But I decided to purchase a license anyway and give it a whirl.

    Well - I’m impressed - it works! You can let it transfer everything, or choose which applications and settings to migrate. You can undo a migration too. I initially let it transfer all the Windows settings and decided I didn’t want that so I undid the migration and started again, this time being more selective. I did a further migration to transfer a few other apps which I missed the first time around.

    Afterwards, about all I had to do was re-enter some license codes. But otherwise, everything worked as if I had installed it from scratch. Only it was effortless. And all my documents and data were there too.

    Next time though I reckon the way to go is virtual. My next machine will be a virtual machine. Then when it’s time to upgrade the hardware - when I buy a new physical PC - all I’d need to do is copy the virtual disk file over. Backups become easier to: just make a copy of the virtual disk files. The whole PC becomes more portable too. Copy the virtual machine files to your laptop. Now your laptop has a complete snapshot of your main environment. It’s the future! But you can do it now.

    If you’re new to virtual computing checkout the following products:

    We already run several virtual environments, for testing and development purposes. Much cheaper than multiple physical PCs and easier than multi-booting. And they can easily be copied to new hardware.

    So you see, it is finally possible to migrate to a new PC and avoid the pain!

    Sitemap | Terms and Conditions | Privacy Policy | © MJT Net Ltd 1997-2008 All Rights Reserved.

    Windows Vista and the Windows logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.