Your first script
You should have chosen and installed your AutoHotkey version now. We’ll do our first script.
Create a script file
If you have used the AutoHotkey classic or AutoHotkey_L installer, right-click on your desktop or any folder and select New > AutoHotkey Script from the context menu. Open the newly created file in your editor. If you haven’t run an installer, just open your favorite editor (or use Windows Notepad) and save it as a file with the extension “.ahk”.
In the editor, type the following code:
1 ; any AutoHotkey version 2 MsgBox Hello, World!
Save your file again now.
Make sure you chose the correct encoding. If you have installed AutoHotkey classic or AutoHotkey_L ANSI, use, of course, ANSI, otherwise use UTF-8.
- In Windows Notepad, this setting is available under
- In Notepad++, it’s in the menu bar under
Execute your script
If you have run an installer, just double-click the file for executing it. Otherwise use the command line to run
[Path/To/YourAutoHotkey].exe "[Path/to/your/script].ahk". You can also drag your script file on the executable in Windows Explorer or on your desktop.
For IronAHK on Linux / Ubuntu / Mac, run
mono IronAHK.exe [Path/to/our/script].ahk.
You should see a box appear similar to the screenshot below:
The first word in your code above,
MsgBox, is an AutoHotkey command that instructs AutoHotkey to show that box. The rest of the code is, as you might have guessed, the message to display.
Extending that example
You can customize the MsgBox further:
Use the following code:
1 ; any AutoHotkey version 2 MsgBox, 36, a question, Would you like to say "Hello, World"?
As in all programming and scripting languages, you can add comments to AutoHotkey source code as well:
1 ; any AutoHotkey version 2 /*********************************** 3 This is my first script! 4 Impressive, isn't it? 5 ************************************ 6 */ 7 MsgBox ; this displays a box saying "Click OK to continue"
So you can see: the character for one-line comments is the semicolon (
;). This can of course be used in the same line as code, but it must be at the end and there must be at least one space or tab before the semicolon.
For multi-line comments, you use
*/. There can be more text on the same line as the comment start (as in the example), but the comment-end must be alone in a line!
Now, maybe you want to make a box saying “1, 2, 3”? This should be as easy:
1 MsgBox 1, 2, 3
But what’s that? The MsgBox looks more like this:
Well, AutoHotkey uses commas to separate parameters from each other. As you saw above, the
MsgBox command has more than one parameter, so it takes 1, 2 and 3 as three parameters. How to prevent that?
AutoHotkey has, as most other languages, so-called escape sequences. Those prevent some characters from being read as syntax. AutoHotkey’s escape character is not the backslash (
\) as in some other languages, it is the backtick (`).
The allowed escape sequences are:
`,- a literal comma. If not escaped, this might be read as parameter delimiter.
`%- a literal percent sign. If not escaped,
%is used for variables. We’ll look at that later.
- “ - a literal backtick. If not escaped, this is used as escape char.
`;- a literal semicolon. As you just learned, this is usually used for comments. If there’s no space or tab to the left of the semicolon, this is not used.
`n- a literal newline. As AutoHotkey has no explicit line end char, each code line ends at the next newline. An exception are continuation sections.
`r- a carriage return. This sign is used in Windows before each newline.
`b- a literal backspace
`t- a horizontal tab
`v- a vertical tab, which corresponds to Ascii value 11. It can also be manifest in some applications by typing Control+K.
`a- an alert (bell), which corresponds to Ascii value 7. It can also be manifest in some applications by typing Control+G.
`f- a formfeed, which corresponds to Ascii value 12. It can also be manifest in some applications by typing Control+L.
""- Within an expression, two consecutive quotes enclosed inside a literal string resolve to a single literal quote.
`'- This replaces the above in AutoHotkey v2.
So now we can do the above example:
1 MsgBox 1`, 2`, 3
And it works!