SEARCH

How-To Geek

How to Save Time by Automating Tedious Tasks with AutoHotkey

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

Comments (20)

  1. FUBAR

    When I try to play the video, I have this error message : “This a private video. If you have been sent this video, please make sure you accept the sender’s friend request.”

  2. 3rev

    Same msg ““This a private video. If you have been sent this video, please make sure you accept the sender’s friend request.”

  3. michel

    Your title here should be, “How to Save Time On One Specific Task You Probably Won’t Ever Encounter in Autohotkey”.

    and you didn’t even explain everything:if the function retrieves the state of the key F8, why do you need the extra “state” in the expression? Shouldn’t it be “GetKeySate, F8″? And what does “if state = D” mean? Yes, I can guess, but what use it a tutorial if I have to guess what you mean?

    This is typical of every article I’ve ever seen about Autohotkey: it’s written for people who already know how to use it.

  4. neil

    whatever happened to the program that recorded the keystroke activity once, then could repeat it.
    Very simple, no special language to learn.
    I don’t remember its name —can anyone help?
    Thanks.

  5. Bingoman

    video doesn’t work man………

  6. Shlomi

    Same msg ““This a private video. If you have been sent this video, please make sure you accept the sender’s friend request.”

  7. Daok

    Video is buggy!

  8. Shank

    Wow… how did this even make it on LifeHacker?

  9. Butch mayhew

    Video works for me now. Nice simple walk through thanks.

  10. daddy su

    You guys are missing the point. It is just an example of how you can use autohotkey to automate a mundane task. It’s not trying to teach you the autohotkey syntax.

  11. Sue D. Nymme

    Why would anyone use AutoHotkey when AutoIt has all the power without ahk’s horrendous syntax inconsistencies?

  12. kindrudekid

    I have to login to access internet…

    to make it login at my windows startup i wrote a python script…

    but problem is the cookies expire after 8 hours…

    can some one temme a autohotkey script to run my login.py file peridically after 6 hours? say?

  13. jakelwaters

    I, for one, thought that this was a useful article.

    Haters gon’ hate.

  14. jihony

    I know I am not a programmer, so, the use of GetKeyState function lost me. What happen if there isn’t this function?

  15. Arion

    ; Get the state of key F8 and store the value in variable “state” (It could store a letter “U” (for Up) or “D” (for (you guessed it) Down))
    GetKeyState, state, F8

    ; If the value store in the variable “state” is equal to “D”, break the script
    if state = D
    break

    Wasn’t it supposed to be “if state == D” (note the double equal signs instead of a single one) in AutoHotkey syntax?

  16. Sue D. Nymme

    Who knows? AHK syntax is arbitrary. Is there a rule for when a keyword has a comma after it? Is there a rule for whether D is a variable or a literal character?

  17. Alex LaFroscia

    @ michel

    If you want to learn how to use it, go to their website and learn. Don’t expect How-To-Geek to explain the inner workings of Autohotkey every single time they write a post about it.

  18. Tourtek

    Was it called “AutoMouse” or something similar?

  19. Newbie

    This was great. I didn’t know anything about autokey, and I could follow the concept completely since the steps were explained one at a time. Thanks for making it so clear. Now I’m excited to try and create my own script.

  20. notrequired

    whatever happened to the program that recorded the keystroke activity once, then could repeat it.
    Very simple, no special language to learn.
    I don’t remember its name —can anyone help?
    Thanks.

    ghostmouse 2.0

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!