AutoHotkey Tip of the Week: How to Combine Multiple Scripts into One—August 19, 2019

Design Your Scripts to Either Run as Stand-alone Apps or Use the #Include Directive to Integrate into a Master Script without Modification

Light Bulb!Cover 200 Border

The cool thing about this tip is that, after you implement these techniques—without any additional changes—you can run your AutoHotkey script as an independent app or quickly add it to a master script. 

In “Chapter Eleven: How to Write Easy-Merge AutoHotkey Scripts” from the book Beginning Tips for Writing AutoHotkey Scripts, I outline steps for writing scripts which easily combine with other scripts without conflict. This allows the AutoHotkey user to run multiple apps without needing to launch each script individually. (It also prevents the accumulation of numerous AutoHotkey icons in the Windows System Notification Tray.) If you employ these basic design tips when writing your apps you’ll find that you don’t need to do any rewrites when combining useful scripts.

Library Benefits

After taking the following steps, you’ll be able to add your new script to another catchall script by simply inserting a GoSub command calling the new script’s encapsulated Auto-execute Label subroutine into the master Auto-execute section and adding the script (#Include directive) to the primary file. When writing a new script, do the following:

  1. Label and encapsulate the Auto-execute section.
  2. Create unique variable, label, and menu names.
  3. Add names to GUI (Graphical User Interface) pop-ups.
  4. Ensure no conflicting functions.
  5. Check for script-wide AutoHotkey commands.

After incorporating these ideas, your script will continue to run independently, as well as, allow for easy integration in another script.

Label and Encapsulate the Auto-Execute Section

The Auto-execute sectioncode which runs immediately upon loading—sits at the beginning of the script. Since a script can only contain one Auto-execute section, combining the routines from two or more scripts presents special problems.

By giving the Auto-execute section in your new script a Label name and enclosing it with the Return command, you can integrate it with another script by inserting a GoSub command calling that Label name into the Auto-execute section of the master script (e.g. Gosub, AutoExecLabel), thus eliminating the need to combine the two sections in the same file. For example, the Label LegalAutoExec: and the Return command encapsulates the initiating code:


Loop, Read, LegalInput.ini
    If A_Index = 1
    Input_Array := StrSplit(A_LoopReadLine, "=") 
    Input_String := Input_String . Input_Array[1] . ","   


To combining the above Auto-execute section taken from the LegalTerms.ahk script into a master script, insert:

GoSub LegalAutoExec

into the master script’s Auto-execute section and add:

#Include LegalTerms.ahk

to the #Include scripts section of your script. You can add the #Include directive almost anywhere in a script, but convenience suggests placing it toward the end of the file.

Create Unique Variable, Label, and Menu Names

To prevent conflicts between unrelated features in a multipurpose script, add unique app identifiers to all variable, Label, Menu, and function names. I usually use a word describing the script function (i.e. concatenate IP to variables in the IPFind.ahk scriptIPAddress).

Add Names to GUI (Graphical User Interface) Pop-ups

For easy identification (and preventing conflicts), give every GUI a unique name when adding controls. I usually use a GUI name which relates to the GUI’s purpose:

Gui, Hotstring:Font, s14 cBlue Bold , Arial
Gui, Hotstring:Add, Text, Section vText1 , Enter Hotstring
Gui, Hotstring:Font, s12 cBlack Norm
Gui, Hotstring:Add, Edit, w50 vNewString ys, 
Gui, Hotstring:Font, s14 cBlue Bold
Gui, Hotstring:Add, Text,  xs vText2, Enter Replacement Text

This code excerpted from the Auto-execute section (HotstringAutoExec:) of the InstantHotstring.ahk script.

Ensure No Conflicting Functions

Functions by the same name can only appear once in a script. If you use the same function in more that one script, either move it from the new script to the master script, add it to a master #Include function file, or rename it. If you want to maintain both stand-alone capability and easy-integration for the same script, rename the function using the method suggested in the “Create Unique Variable, Label, and Menu Names” topic above. This may occasionally add a redundant function to a master script, but the varied name prevents conflict.

Check for Script-wide AutoHotkey Commands

Look for commands which may affect every script in the master file (e.g. Reload, ExitApp). While effective when running a standalone script, these commands can act as a source of frustration in a master script.

For more details and examples, see “Chapter Eleven: How to Write Easy-Merge AutoHotkey Scripts” from the book Beginning Tips for Writing AutoHotkey Scripts.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)


2 thoughts on “AutoHotkey Tip of the Week: How to Combine Multiple Scripts into One—August 19, 2019

  1. Jack,
    I have learned quite a bit from your blog and your scripts. I still am at a loss when it comes to including scripts. I apparently am not following instructions very well.

    In the auto exec:
    GoSub hotstrings2

    Later in the script:
    #Include hotstrings2.ahk

    I have had to go in and correct redundancies in the two scripts. I understood I would have to do that. Once I did that, I got a whole string of errors. Yes, I should have written them down because you are not a mind reader, but I didn’t. I have returned to the way things were previously and I just run both scripts. What did I neglect to read in what you instructed, please?


    • I can understand the confusion. Both parts are required.

      GoSub hotstrings2

      runs the autoexec portion of the hotstrings2.ahk file but you must include the file in the combined script:

      #Include hotstrings.ahk

      The label “hotstring2:” must be added to the beginning of the hotstring2.ahk autoexec as a target for GoSub.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s