Thursday, January 28, 2010

AutoHotkey AutoInclude Organizes, Consolidates Your AHK Workflow [Autohotkey]

Source: http://feeds.gawker.com/~r/lifehacker/full/~3/EmOu8gdrRyc/autohotkey-autoinclude-organizes-consolidates-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 2 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 Center — 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 organized, 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-Synchronized 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! Want to get your feet wet with AHK? Take a look at our beginner's guide to turning any action into a keyboard shortcut with AutoHotkey.