AutoHotkey 101

Autohotkey is a program that lets you use simple text commands to control almost everything on your computer. It has all the key programming features that let you do things like search through a string of text for commas, open all the Word files in a folder, rename every file that has “blah” to “super” or whatever you else you fancy. It even lets you build simple programs with menus. Autohotkey script files are just text files with a .ahk extension. To edit an Autohotkey script, simply open the file in a word program or right click on it and click edit or open with notepad. (Notepad++ is the best editor I like but there is also a Scite4 editor for Autohotkey that you can find at ahkscript.org that has Autohotkey specific highlighting and code completion). In order to run scripts with a .ahk extension you need Autohotkey installed. One could setup these scripts to be compiled and run on a computer that doesn’t have Autohotkey installed. To compile a script right click on it and click compile to create a .exe file. The compile process should pull in all the included and necessary library files as well. The library files are stored in the Autohotkey/Lib folder. http://www.ahkscript.org is the new active Autohotkey site. The biggest learning curve item in Autohotkey is learning the difference between expressions and literal strings and when each is expected and how to switch between them for ease of use. The documentation is great at explaining this.

Autohotkey is an interpreted language that is weakly typed. You can use it to do functional programming or object oriented programming or a mixture of the two. Autohotkey has a preprocessor that analyses your code before it runs it to check for things that for sure won’t work. It checks for things like missing or extra brackets or braces mostly. After that your script gets executed top down line by line. Functions can be included anywhere in your script and get picked up by the preprocessor. Functions don’t get executed unless they are called/used somewhere in your code. Use := to define and change variable values. Any statement/expression that uses := means that the names are treated as variables and strings need to be quoted. Use = to do a Boolean compare without changing the value of the variable.

Being an interpreted language that basically means that you don’t have to compile scripts to run them but you do need Autohotkey installed if you don’t compile it. That also means that the executable files can run a lot slower than a systems programming language like C++. That is still really fast though!

Weakly typed means that you don’t have to declare variable types before you use them. It also means that your variable content can change types. Ie. You can have a variable called one_awesome_name that first has the number 0 (in Autohotkey this means false/empty/uninitialized) which can then be set to the string “an awesome value” (any value other than 0 is considered true) or even a pointer to a memory address that contains a COM object. In C++ you couldn’t do that. It would either be NULL, INT, or string or something like that and you wouldn’t be allowed to switch in between generally.

Object oriented means that you can create and use objects. Objects are variables with structured content that you can use to group data or functionality to a single variable. You can use objects (otherwise called classes) to get functionality like fruit_instance.colour:=”red”. Ie. The variable fruit_instance has an attribute (or property) called colour that can be set to a string called red. You can add functionality to an object by adding a function to it. Object/class functions are called methods in most programming languages. Ie. Nerd_module.math_function(3, 4, 5) might add 3 numbers and return the sum.

A namespace is the scope of a variable name. It is how a programming language recognizes and gives access to variable names throughout the code. Autohotkey has a really simple setup for its namespace.

-Everything that is declared in a function or class or method is local to that function/block of code designated by the open and closed braces {}.

-Everything declared outside of a function or class is accessible anywhere else outside of a function or class.

-Anything declared with the global keyword is accessible anywhere.

You can #include filename.ahk at the top of a script file to basically paste the contents of another script file starting at that line. This is useful for packaging functionality into functions and then giving other script files access to use those functions. I have done that in my scripts. This helps keep each individual file more readable.

You can do

If (something=something_else)

Do something

Or

If (something=something_else) {

Do something…

Do something_else…

}

Ie. After if or else statements, if you just want to execute one line of code, you can just put it below the if or else statement. If you want to link more than one line of code you need brackets. Loop and while statements need the opening bracket to be on the line below it I think. For statements can have the opening bracket on the same line I think.

Comments in code are portions of text that are not executed and are just to inform the reader of something. The rest of a line starting with ; is a comment. Everything between /* and */ is a comment even over multiple lines.

Ex.

Msgbox actual code that gets executed ;msgbox this will not get executed

/*

Comment line 1

Comment line 2

*/

More code…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s