How to Save Time by Automating Tedious Tasks with AutoHotkey

By Lowell Heddings on September 7th, 2010

Have you ever needed to perform the same mindless task over and over on your PC? Instead of wasting hours clicking buttons and hitting keys, this is the perfect time to use your AutoHotkey skills to make your PC do the work for you.

Note: This particular example is a real one that I used earlier today to save a small amount of time, but these are techniques that I’ve used many times over the years to literally save myself days worth of time.

The Scenario

I was trying to go through and clean out a bunch of incorrect broadcast messages in our email newsletter account, when I realized that their interface required me to manually click the Delete button and then confirm it on every single message—we’re talking about 300 incorrect messages that needed to be deleted. To make matters worse, the interface is extremely slow, which means I would have spent a good 30-40 minutes just clicking and making my carpal tunnel even worse.

image[11]

Instead of doing that, I created a new AutoHotkey script and quickly wrote up a script to do the work for me.

The first step was to identify exactly which clicks and keys I needed to automate—obviously the first step is to click on the X button, which brings up this Ajax confirmation dialog:

image[13]

Luckily the Delete button is automatically highlighted, so you can simply hit the Space key to confirm. Once the record has been deleted, everything slides up as if the row was never there. Knowing this, we’ll move on and create a script that automates clicking the X button, waiting 3 seconds for the confirmation dialog, presses the Space bar, and then waits another 3 seconds for the row to disappear.

Creating the Script

The first thing we’ll want to do is create a loop that will repeat the same actions a number of times—in this case, we’re estimating that we’ll need to repeat this 300 times, so we’ll use the Loop syntax like this:

  Loop 300
  {

  }

Now we’ll need to automate the click action, which is easy in AutoHotkey—you just type click. You can use a more advanced click syntax if you want, choosing exactly where you want it to click on the screen, or choosing the button click. For our purposes, we’ll just be using the default, which leaves us with this:

  Loop 300
  {
     click
  }

Now our script will click 300 times in a row, but unfortunately we’ve got that confirmation dialog to deal with, so now we’ll use the Send function to send the Space bar keystroke to the active window.

  Loop 300
  {
     click
     Send,{Space}
  }

If you look at the documentation you’ll see all of the syntax for special keys—regular keystrokes can be entered normally—for instance, if you wanted to type test and then end it with a Space, you’d use this:

Send, test{Space}

So now we’ve got a script that clicks the button and then hits the Space bar, which would be alright except the interface is slow, so we need to insert a small pause between each execution of the click and send functions. To accomplish this, we’ll use the Sleep function, which takes only one argument—the delay in milliseconds.

  Loop 300
  {
     sleep 3000
     click
     sleep 3000
     Send,{Space}
  }

Now we’ve got a script that will successfully delete the items, waiting 3 seconds before it starts so that you can move the mouse cursor over the first X, clicking the button, waiting 3 seconds, hitting the Space bar, and then waiting 3 seconds before it goes through the next set. You could use this simple script right now if you wanted to—but what if you want to stop the script?

What we’ll do is use the GetKeyState function to check whether you’ve hit a certain key—for testing, we’ll use the F8 key and add the following into the middle of the loop. This will detect whether the F8 key has been pressed, and then use the break to exit the loop.

GetKeyState, state, F8
   if state = D
     break

The Final Script

Here’s the final script all put together, which probably won’t help you too much since it’s specific to my scenario—but you can use it to create your own scripts by simply modifying the clicks and keystroke sending.

  Loop 300
  {
    GetKeyState, state, F8
    if state = D
        break
    sleep 3000
    click
    sleep 3000
    Send, {Space}
  }
  Return

To illustrate how this works in practice, here’s a quick video that shows it in action:

In this particular scenario, it took me about 3 minutes to throw together a working script—time saved: 27 minutes. Just enough time for me to record the video and write this article!

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/7/10
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!