AutoHotkey AutoInclude Organises, Optimises Your AHK Workflow

Ed note: We love AutoHotkey around Lifehacker HQ due to its powerful Windows tweaking skills, so we were thrilled when reader Scott Rippey wrote in with this extremely smart script for managing your AHK scripts.

I'm ashamed to say that this is my first contribution to Lifehacker. I say "ashamed" because I've been taking and taking, and I haven't given anything in return! So I thought it was high-time I contributed my most helpful AutoHotKey script. I call it "AutoInclude".

There are two problems that it solves: First, as an avid reader of Lifehacker, and a huge user of AHK, I come across dozens of GREAT scripts that I want to incorporate into my own "main script", but it can be quite a hassle combining them all into a single AHK file. Take a look at all these scripts:

Second, I use AHK on my Work PC, my laptop, and my Media Centre — and I have different AHK needs for each of these locations, but I still want some AHK code to be shared across the board.

My AutoInclude script scans a list of folders for *.ahk, and creates a temporary AHK file that "#Include"s them all, and then executes the temporary file. It allows me to keep all my scripts VERY organised, allows me to determine which scripts are appropriate for each computer, and lets me edit my scripts very easily! Finally, I put all my AutoHotKey scripts into a Live-Mesh-Synchronised folder and share it across all my PC's.

On each PC, I modify the top of the file to only include the folders that apply to that system (such as All, XP and Tablet, versus All, Dual Monitor and Work) and I comment out the rest:

Finally, when I run the script, I only have a single AHK process running! I love it.

The only thing that is tricky is the fact that "#Include"ing a bunch of files can cause possible issues if the script needs an "Auto-Execute" section. The solution is as follows:

If a script needs to auto-execute, then the first line of the file should be the comment "; Auto-Execute", followed by the auto-execute code as usual, and there MUST be a "Return" before any other code or hot-keys. The AutoInclude script will put a label in front of the "#Include" statement, and will call a GoSub to that label, thereby auto-executing it!

Take a look at the auto-generated script:

You can download my AutoHotkey AutoInclude script here.

Thanks Scott!


Be the first to comment on this story!