Autohotkey is a free and powerful tool that allows you to automate almost anything on your Windows computer in any program.
Computer Hope uses this tool daily to help answer common questions asked in e-mail quickly and perform other common repetitive tasks. If you do anything daily that requires you to repeat the same actions, we highly recommend using this tool. This page demonstrates some of this programs capabilities.
Caution: This tool can be used to automate tasks in gaming, some online games may consider this cheating and if caught it may result in a ban.
If you want to follow along with this documents examples, please download and install Autohotkey before following any of the below steps. Otherwise, skim this document for a better understanding of the program before downloading and installing it on your computer.
Edit the script
Script basics
Creating your first script
Scripting the mouse
Run a program
Using variables
Conditional statements
Creating a loop
Regular expressions
Additional information
Script basics
Creating your first script
Scripting the mouse
Run a program
Using variables
Conditional statements
Creating a loop
Regular expressions
Additional information
Edit the script
After Autohotkey is installed to create and edit a script right-click anywhere on the Desktop or folder, click New, and choose AutoHotkey script. Name the script whatever you want and then right-click the script file and choose Edit the script.
Tip: If you plan on always using the same scripts you can also load AutoHotkey at startup, right-click the AutoHotkey icon () in the Windows notification area, and click Edit this script. The default script (AutoHotkey.ahk) will open in your default text editor and allow you to add or change your own scripts.
Each time Autohotkey loads when your computer starts this default script will load this script.
Script
basics
Autohotkey includes two example scripts, the first one (shown below) will open the Autohotkey web page when you press the Windows Key and Z at the same time. Which can be done now if you have Autohotkey installed and the default autohotkey.ahk loaded. Otherwise, this line can be added to a new script, saved, and ran to allow this shortcut to work.
#z::Run www.autohotkey.com
Most scripts will be more than one line. However, in the
above example it is only one line and needs no additional commands. In
the below script example, the script has multiple lines, and as can be
seen must be finalized with the "return" command to prevent anything
below this script from being executed.
^!n::
IfWinExist Untitled - Notepad
WinActivate
else
Run Notepad
return
IfWinExist Untitled - Notepad
WinActivate
else
Run Notepad
return
Creating your first script
::Hello::Hello World{!} This is my first script.
;Example comment
In this first example, we are not using a shortcut, only the keyword "hello" to execute the script. Also, because "!" is a modifier key command for the Alt key it has been surrounded by curly brackets, which indicates the key, not a command. Finally, this script also contains a comment at the end, which is anything followed by a semicolon. All comments are ignored and used to help explain the code in the script.
Any time you make any changes to a script it must be reloaded or run in order for those changes to work.
To load the script double-click the script file or right-click the script file and choose Run Script. If you're editing the default autohotkey.ahk and Autohotkey is running reload the script by right-click on the Autohotkey icon () in the Windows notification area and choose the Reload This Script option.
Once the script has been loaded you should be able to type "hello" in the below text box and after pressing space or any punctuation the script type out "Hello World! This is my first script."
Tip: If you don't want to have to press the space or punctuation you can add an asterisk between the two first colons.
Next, in the below example we are creating a script that is executed with a shortcut key. Edit the script and add the below three lines to your script.
#F2::
send Hello World{!}
return
send Hello World{!}
return
After these three lines have been created save the file as the same file name and then reload the script. If done successfully you should be able to click in the below text box and press the Windows Key + the F2 function key at the top of the keyboard to print Hello World!
In addition to sending text any shortcut keys can also be added, data can be copied to and from the clipboard, and the script can sleep for any amount of time. Edit the script again and make the below changes to the script created earlier.
#F2::
send Hello World{!}
send {CTRLDOWN}{SHIFTDOWN}{HOME}{CTRLUP}{SHIFTUP}
send {CTRLDOWN}c{CTRLUP}{END}
example = %clipboard%
StringUpper,example,example
sleep, 1000
send, - new hello = %example%
return
send Hello World{!}
send {CTRLDOWN}{SHIFTDOWN}{HOME}{CTRLUP}{SHIFTUP}
send {CTRLDOWN}c{CTRLUP}{END}
example = %clipboard%
StringUpper,example,example
sleep, 1000
send, - new hello = %example%
return
In the above example, lines three and four have introduced how keys can be pressed in the script to perform other keyboard shortcuts. The third line this is pressing Ctrl+Shift+Home to highlight all text before the cursor, and the next line is pressing Ctrl+C to copy the highlighted text. Anytime a key is pressed down (e.g. {CTRLDOWN}) make sure it is let go with up (e.g. {CTRLUP}), otherwise it will remain down and cause problems.
The fourth line introduces a variable and the %clipboard% command which contains anything in your clipboard. With this line, all contents in the clipboard are assigned to the "example" variable.
The next command is making the example variable all uppercase by using the StringUpper command and assigning the uppercase text back to the example variable. The StringLower command could also be used to make everything lowercase.
Next, the sleep command is a great command for making the script sleep for any length of time. 1000 is equal to 1 second. This command is useful and often necessary if the script has to wait for the computer to open a program or window.
Finally, the last send command will add " - new hello =" with the hello world now all in uppercase. This revised version of the script can be tested again in the below text box.
Scripting the mouse
Although almost anything can be done using keyboard shortcuts, there are still times you may want to click somewhere on the screen. Using the click command you can click on any location of the screen as shown in the below example. To determine what the location of where you want to click use the Window Spy utility that can be opened by right-clicking the Autohotkey icon () and clicking Window Spy. As you move your mouse, the "In Active Window" will display the location of your mouse cursor's current position. Once you've determined where you want to click add the Click command with the location of where you want the mouse to click.
#F2::
Click 980,381
return
Click 980,381
return
With this command once the Windows key + F2 is pressed the mouse will click once at 980,381.
Run a program
Run, wordpad.exe, C:\My Documents, max
In the first example, this would open WordPad with the default directory C:\My Document, and open the window maximized.
Run, www.s3allinone.blogspot.com
Run, mailto:example@domain.com?subject=My Subject&body=Hello this is a body example.
Finally, this is yet one other example of the run
command, which is sending an e-mail using your default e-mail client and
sending the e-mail to example@domain.com with the subject "My Subject"
and the body of the message having "Hello this is a body example."
Using variables
In our first example, we will be using an integer variable to add two numbers together and display the results in a message box.
#F2::
example := 5+5
msgbox, Example is equal to %example%
return
example := 5+5
msgbox, Example is equal to %example%
return
In the above example, "example" is our variable name, := is assigning the integer expression as the value of 5+5 (10). Once the variable has been assigned we are using the msgbox command to open a message box and print its value. Whenever you are sending, printing, or assigning a variable it must begin and end with a percent symbol. After saving and reloading the above script when pressing Windows key + F2 you should see a message box similar to the example shown on this page.
In the next example we are assigning the variable a string value and again having the results displayed in a message box.
#F2::
example := "Nathan"
msgbox, Hello World! My name is %example%
return
example := "Nathan"
msgbox, Hello World! My name is %example%
return
If you wanted to have a variable with a string and an integer you can have an expression outside the quotes, as seen in the below example.
#F2::
example := "Example: " 5+5
msgbox, Mixed variable is %example%
return
example := "Example: " 5+5
msgbox, Mixed variable is %example%
return
Conditional statements
Conditional statements are also supported with AutoHotkey and support the operators and (&&), or (||), and not (!). Below are a few examples of how conditional statements can be used.
#F2::
example := 5
if example = 5
msgbox, true
else
msgbox, false
return
example := 5
if example = 5
msgbox, true
else
msgbox, false
return
You would think after seeing the first conditional statement example that you could put quotes around a string in the variable and conditional statement; however, this will not work. If you want to match a string, surround your expression with parentheses as shown in the below example.
#F2::
example := "computer"
if (example = "hope")
msgbox, true
else
msgbox, false
return
example := "computer"
if (example = "hope")
msgbox, true
else
msgbox, false
return
Creating a loop
If there is a script that you want repeat,
place the script into a loop, as seen in the below example script.
#F2::
loop, 5
{
send Hello World{!}
sleep 300
}
return
loop, 5
{
send Hello World{!}
sleep 300
}
return
Regular expressions
#F2::
example := "support@computerhope.com"
example:= RegExReplace(example, "@.*", "")
msgbox, Username is %example%
return
example := "support@computerhope.com"
example:= RegExReplace(example, "@.*", "")
msgbox, Username is %example%
return