Your first script

Let’s start

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
2
; any AutoHotkey version
MsgBox Hello, World!

Save your file again now.


Note:

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.


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: MsgBox screenshot

What happened?

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: 2nd MsgBox screenshot

Use the following code:

1
2
; any AutoHotkey version
MsgBox, 36, a question, Would you like to say "Hello, World"?

Comments

As in all programming and scripting languages, you can add comments to AutoHotkey source code as well:

1
2
3
4
5
6
7
; any AutoHotkey version
/***********************************
This is my first script!
Impressive, isn't it?
************************************
*/
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 /* and */. 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!

Escape sequences

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: screenshot

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:


So now we can do the above example:

1
MsgBox 1`, 2`, 3

And it works! screenshot