Autohotkey Scripting

AutoHotkey Scripting Thoughts: A Script Is…

Late evening, it’s raining, I think a thunderstorm is coming because I’ve just seen the flashes of lightning.Autohotkey Scripting

There is always a good chance for rain here in Denmark.

We also have a saying: “There is no bad weather, only bad attire” — but tonight I’m still happy to be making  my script indoors.

Anyway, listening to Distant Thunder from mynoise.net and waiting for a thunderstorm is awesome.

I even decided to have a late evening cup of coffee, not something I normally do.

What is a AutoHotkey script?

Remember, that at it’s most basic form, a AutoHotkey script is nothing more than lines of plain text in a file, read by the AutoHotkey interpreter, simply a series of commands (even a single command) that you could have performed in a number of different ways yourself. But instead of having to do the steps manually, they are typed out, step by step and stored in a text file with the .ahk extension.

By default, Windows doesn’t have the ability to execute Autohotkey scripts, it needs to be downloaded. To then use and run an Autohotkey script you can do one of two things, one install Autohotkey or two keep it portable, I use the installed version. ( they are the same the installer just associates .ahk with Autohotkey.exe and creates folder and a start menu entry )

Most normal users can safely and maybe more easily use the installed version. This is shown here: https://www.youtube.com/watch?v=HN0B6K9hRWA

Not installing does not prevent you from writing a script and running it with AutoHotkey.exe, simply highlight the script file in explorer, right click it and pick run with and find Autohotkey.exe, this still runs any AutoHotkey command—nor does using the portable version keep you from compiling your scripts (making them into exe’s).

 

AutoHotkey Elements of a script

Elements of a script

The top of an Autohotkey script is called the “Auto-execute Section” read more about that here.

I try to always add a couple of comment lines at the top of my script.

The comments can be the name of the script, when I made it

And a few other notes that I might add.

If you use the installed version you will have an option in the right-click menu to make a New AutoHotkey Script

This will just make a new text file with the .ahk extension for you.

If you used the New AutoHotkey Script option the script file comes with a few lines already at the top

I do not always use that option and don’t always need the pre written lines

But there is nothing wrong with having them and some are even a good idea to always have in new scripts.

I then add in my comments section

Only when I share the script do I give it a version like 1.0.

So if I revise a script

I can change the version number in the comments section.

But if it is only my own original version of the script

I do not have a version number at all.

This is because I rarely keep editing the same script file if its first done.

The words that I add to my scripts are designed to make it easier for me to remember what my script is for.

I know that you can also change your windows search settings so that it does full text indexing of all of your .ahk files.

For example, if you type MsgBox in your search box, it would find all of your AutoHotkey scripts where you have used the MsgBox Command.

My comments are free-form notes and fully optional.

As an example: I made a script to help someone on the Autohotkey forum,

Then I try to remember to copy the URL for that topic and paste it in my comments section.

Also, if I copied multiple parts from someone else

I will provide credit to them in my comments section.

If the script is used in one of the Jszapp.com Blog posts

I would most likely also put the URL to the post in the comments section.

Armed with that, I can always find what it was for.

 

Body of a script is where all the scripted routines are.

Here is where I have all my Hotkeys, Hotstrings, Windows commands, Sending of keystrokes, Mousemoving, Running of programs, Functions and other code blocks

One way to see it is that this section of the script is where you make the magic happen

lots of people like myself have started with a single hotkey routine

written in a simple notepad window.

An AutoHotkey script can be used for much more than hotkeys and hotstrings

But they are a great place to start

As you get instant gratification when you see your routine work with nothing more than the press of a keyboard key.

So keep making those small hotkey and hotstring routines until you have a task that calls for more

Click to Tweet: The productivity boost you can get from just a couple of scripted routines is amazing.

 

101 Reusable code

Making re-usable code

In addition to writing quick one-off scripts and examples

I also write re-usable functions and routines and single class here or there

I try to take time out of my day to write code snippets to help Autohotkey.com users

So I do what I can to make my code easy to share in the forums and blog posts.

When doing this I don’t normally use the Standard Library or User Library

Instead I use #Include to make the script behave as though the included lib/file’s content is physically present at that exact position (as though a copy-and-paste is done from the included file).

This helps other users know what lib or files are needed to make the script run without error!

I like the idea of  the Standard Library, to make it easier to reuse functions.

In this way:

You do not have to include or copy of the function into your script.

You can simply call it and use it.

The disadvantage to this approach is that you now have an external dependency

In addition:

You don’t have a clear view of where to find the function if you need to share or update it.

The disadvantage to using include is that it brings everything into your current scripting environment—all functions, variables, and other things that you may not want.

So, if I have a script that will only use a single or a few functions

I copy only the functions I want into it.

AutoHotkey Scripting Thoughts will continue next week when I will talk about the terminology used with Autohotkey

I invite you to follow me on Twitter and Facebook. If you have any questions, send me a message, You can use the contact from, or post your questions as a comment below.

See you soon.

Leave a Reply