This post outlines a method I came up with to use a single AutoHotKey script to auto-load any number of scripts in a sub-folder. It also allows every script to have its own autoexec section, and to hook into a timer for event-type functionality.
AutoHotKey scripts that are 'persistent' need to be loaded into memory by Windows just one time. If you have several of them to load, you can get them all individually, but you end up with a system tray full of little green 'H' icons.
Using AutoHotKey's #Include
directive, you can use one 'main' script, and #Include
directives inside it to load the others you need in memory. This system has at least two drawbacks:
- You have to hand-edit your 'main' script every time you want to add (or remove) a script from your collection
- Only the 'main' script can have an Autoexec section. The others will not be able to load their own variables and other data when they are first run.
To overcome these limits, I came up with a solution that builds and writes a temporary file to disk and loads more files from that:
- one script (launcher.ahk) will:
- locate and prepare to
#Include
all the .ahk files in a given folder, - look for a special 'autoexec_' label prefix in each row of the file
- look for a special 'timer_' label prefix in each row of the file
- take all that information, and write a new script (includes.ahk) to be included later
- Lastly, Run the main.ahk script
- locate and prepare to
- a second script (main.ahk), that mostly just sets some parameters and then uses an #Include directive to pull in the includes.ahk file and whatever it contains (multiple Includes, autoexec sections, and timer sections).
The net effect of this system is that you can just drop .ahk files into a folder and have them auto-loaded. Each file can have its own autoexec section, and each file can have its own timer event (like watching for a window to become active).
I've attached the code for these files (launcher.ahk and main.ahk) below. To make them work as-is, put them in a folder together and create a subfolder named scripts
. The .ahk files to be auto-loaded go inside the scripts
folder.
I'll also zip up and include a complete package (autoloader.zip), with an example script for auto-loading that manipulates the Notepad window.
Inside autoloader.zip:
- launcher.ahk
- main.ahk
- ./scripts/notepad.ahk
To kick things off, just run the launcher.ahk script. It will build includes.ahk and then hand off control to main.ahk, which will load everything into memory.