How to automate common PC tasks

AutoIt
The AutoIt development environment provides everything you need to create, edit and compile scripts

Owning a PC is supposed to save you time, at least in theory: the computer takes over the simple, repetitive tasks, leaving you to get on with more important matters. But in practice, it doesn't always work out that way.

Many PC users follow the same routine every day, running the same sequence of programs, issuing the same keystrokes and performing the same tedious maintenance tasks, step by laborious manual step. Sound familiar? Then you need a way to automate these processes, making your PC life a little easier.

A scripting language that's more powerful than batch files, yet remains simple and accessible to everyone, expert and novice alike. A free tool that can run programs and simulate keypresses and mouse clicks to take the manual hassles out of just about any tedious task, and all in simple EXE files that you can share with others – no bulky runtime environment required. You need AutoIt, one of the best free PC automation tools around.

Getting started

AutoIt scripts start life as simple text files with AU3 extensions, and the easiest way to build them is with the SciTE Script Editor that comes bundled with the package. It understands the AutoIt language, so if you type RU, for example, then it'll display the first AutoIt function that begins with those letters: Run, which is used to launch programs.

Press [Tab] and it'll enter that function for you, then add a parenthesis – Run( – and it'll display information on the function options, and how they should be used. (See the online documentation if you need more details.)

auto-suggest

At first glance these functions can seem very complicated. The full syntax of the Run command, for instance, looks something like:

$PID = Run( "program" [, "workingdir" [, show_flag[, opt_flag ]]] )

You can specify the program you want to run, its working directory and its window type (hidden, maximised, minimised), as well as low-level details regarding your STDIN, STDERR and STDOUT streams, returning the process identifier as a result. Gulp.

Don't panic, though. Everything but the program name is totally optional, so in fact launching a program can be as simple as typing:

Run("Notepad.exe")

If the executable isn't in the current Windows path then you'll need to tell Windows exactly where to find it. That's easy, too – just type in the following for Office:

Run("C:\Program Files\Microsoft Office\Office12\Winword.exe")

It's actually really simple, then, although maybe not suitable for every occasion.

The Run command simply launches a program and moves on to the next line of your script, so if you've specified five programs in a row then they'll all launch almost simultaneously, which may not be what you want.

If you want to launch Notepad and then launch a back-up tool to save your work after Notepad has finished, you might use a sequence such as this:

RunWait("Notepad.exe")
Run("MyBackup.exe")

The RunWait function launches a process, then waits until that process has closed before continuing. This ensures that your chosen programs appear in sequence rather than all at once.

Automating CCleaner

Once you start having a go at this kind of scripting, all sorts of possibilities appear. If you've ever used the excellent hard drive cleanup tool CCleaner, for instance, then you'll know that you normally have to launch it manually, choose your options, click 'Run cleaner', confirm that you really want to delete files and so on. It's not all that difficult, but there are a few time-consuming processes involved.

CCleaner

However, if you launch CCleaner.exe with the /AUTO command-line switch by typing in Run("CCleaner.exe /AUTO") then the program simply runs with its current settings, without bothering you with any prompts or questions. It just beavers away on its own, doing its work invisibly in the background.

Once you know about that, you can construct another AutoIt script to take advantage of this handy feature:

RunWait("C:\Program Files\Mozilla Firefox\Firefox.exe")
Run("CCleaner.exe /AUTO")

Launch Firefox from this script, and every time you close the browser it'll automatically invoke CCleaner to clear your internet tracks. That's convenient, but we can go one better. Consider the following:

Opt("WinTitleMatchMode", 2)
$DoForever=1
While $DoForever=1
WinWait("Firefox")
WinWaitClose("Firefox")
Run("CCleaner.exe/AUTO")
Wend

The WinWait command tells the script to pause until it finds a window with 'Firefox' in its title (WinTitleMatchMode in the first line defines how that search is performed). WinWaitClose pauses the script until that window is closed, then launches CCleaner.exe.

On hitting the Wend statement, it checks that the variable $DoForever is still 1, then loops back to the WinWait statement and pauses again until a Firefox window appears.

You could leave this script running all the time – once compiled it can consume as little as 2.5MB of RAM. It will detect when you've finished a Firefox session, then automatically run CCleaner to clean up, with no manual steps involved at all. (Use the AutoIt system tray icon to pause or close the script if you no longer need it.)

The script is easily customisable – just change 'Firefox' to Chrome to detect Chrome browser windows, say – and the principle could be applied to many other situations, essentially any time you want to run a program automatically when another one has finished.