# Script Compiler Directives [v1.1.33+]

## Table of Contents

- Introduction
- Directives that control the script behaviour:

- IgnoreBegin
- IgnoreEnd
- IgnoreKeep

- Directives that control executable metadata:

- Introduction
- AddResource: Adds a resource to the .exe.
- Bin: Specifies the binary version of AutoHotkey to use.
- ConsoleApp: Sets Console mode.
- Cont: Specifies a directive continuation line.
- Debug: Shows directive debugging text.
- ExeName: Specifies the location and name for the .exe.
- Let: Sets a user variable.
- Obey: Obeys a command or expression.
- PostExec: Runs a program after compilation.
- SetMainIcon: Sets the main icon.
- SetProp: Sets an .exe property.
- Set: Sets a miscellaneous property.
- UpdateManifest: Changes the .exe's manifest.
- UseResourceLang: Changes the resource language.

## Introduction

Script compiler directives allow the user to specify details of how a script is to be compiled via Ahk2Exe. Some of the features are:

- Ability to change the version information (such as the name, description, version...).
- Ability to add resources to the compiled script.
- Ability to tweak several miscellaneous aspects of compilation.
- Ability to remove code sections from the compiled script and vice versa.

The script compiler looks for special comments in the source script and recognises these as Compiler Directives. All compiler directives are introduced by the string `@Ahk2Exe-`, preceded by the comment flag (usually `;`).

## Directives that control the script behaviour

It is possible to remove code sections from the compiled script by wrapping them in directives:

    MsgBox This message appears in both the compiled and uncompiled script
    ;@Ahk2Exe-IgnoreBegin
    MsgBox This message does NOT appear in the compiled script
    ;@Ahk2Exe-IgnoreEnd
    MsgBox This message appears in both the compiled and uncompiled script

The reverse is also possible, i.e. marking a code section to only be executed in the compiled script:

    /*@Ahk2Exe-Keep
    MsgBox This message appears only in the compiled script
    */
    MsgBox This message appears in both the compiled and uncompiled script

This has advantage over A_IsCompiled because the code is completely removed from the compiled script during preprocessing, thus making the compiled script smaller. The reverse is also true: it will not be necessary to check for A_IsCompiled because the code is inside a comment block in the uncompiled script.

## Directives that control executable metadata

### Introduction

In the parameters of these directives, the following escape sequences are supported: ````, ``,`, ``n`, ``r` and ``t`. Commas always need to be escaped, regardless of the parameter position. "Integer" refers to unsigned 16-bit integers (0..0xFFFF).

If required, directive parameters can reference the following list of standard built-in variables by enclosing the variable name with `%` signs:

Group 1: A_AhkPath, A_AppData, A_AppDataCommon, A_ComputerName, A_ComSpec, A_Desktop, A_DesktopCommon, A_MyDocuments, A_ProgramFiles, A_Programs, A_ProgramsCommon, A_ScriptDir, A_ScriptFullPath, A_ScriptName, A_Space, A_StartMenu, A_StartMenuCommon, A_Startup, A_StartupCommon, A_Tab, A_Temp, A_UserName, A_WinDir.

Group 2: A_AhkVersion, A_IsCompiled, A_IsUnicode, A_PtrSize.

In addition to these variable names, the special variable A_WorkFileName holds the temporary name of the processed .exe file. This can be used to pass the file name as a parameter to any PostExec directives which need to access the generated .exe.

Also, the special variable A_PriorLine contains the source line immediately preceding the current compiler directive. Intervening lines of blanks and comments only are ignored, as are any intervening compiler directive lines. This variable can be used to 'pluck' constant information from the script source, and use it in later compiler directives. An example would be accessing the version number of the script, which may be changed often. Accessing the version number in this way means that it needs to be changed only once in the source code, and the change will get copied through to the necessary directive. (See the RegEx example below for more information.)

As well, special user variables can be created with the format `U_Name` using the Let and Obey directives, described below.

In addition to being available for directive parameters, all variables can be accessed from any RT_MENU, RT_DIALOG, RT_STRING, RT_ACCELERATORS, RT_HTML, and RT_MANIFEST file supplied to the AddResource directive, below.

If needed, the value returned from the above variables can be manipulated by including at the end of the built-in variable name before the ending `%`, up to 2 parameters (called p2 and p3) all separated by tilde `~`. The p2 and p3 parameters will be used as literals in the 2nd and 3rd parameters of a RegExReplace function to manipulate the value returned. (See RegEx Quick Reference.) Note that p3 is optional.

To include a tilde as data in p2 or p3, preceded it with a back-tick, i.e. ``~`. To include a back-tick character as data in p2 or p3, double it, i.e. ````.

RegEx examples:

-     %A_ScriptName~\.[^\.]+$~.exe%

  This replaces the extension plus preceding full-stop, with `.exe` in the actual script name.
  (`\.[^\.]+$~.exe` means scan for a `.` followed by 1 or more non-`.` characters followed by end-of-string, and replace them with `.exe`)

- Assume there is a source line followed by two compiler directives as follows:

      CodeVersion := "1.2.3.4", company := "My Company"

      ;@Ahk2Exe-Let U_version = %A_PriorLine~U)^(.+"){1}(.+)".*$~$2%

      ;@Ahk2Exe-Let U_company = %A_PriorLine~U)^(.+"){3}(.+)".*$~$2%

  These directives copy the version number `1.2.3.4` into the special variable `U_version`, and the company name `My Company` into the special variable `U_company` for use in other directives later.
  (The `{1}` in the first regex was changed to `{3}` in the second regex to select after the third `"` to extract the company name.)

Other examples: Other working examples which can be downloaded and examined, are available from here.

### AddResource

Adds a resource to the compiled executable. (Also see UseResourceLang below)

    ;@Ahk2Exe-AddResource FileName , ResourceName

FileName
  The filename of the resource to add. The type of the resource (as an integer or string) can be explicitly specified by prepending an asterisk to it: `*type Filename`. If omitted, Ahk2Exe automatically detects the type according to the file extension.
ResourceName
  (Optional) The name that the resource will have (can be a string or an integer). If omitted, it defaults to the name (with no path) of the file, in uppercase.

Here is a list of common standard resource types and the extensions that trigger them by default.

- 2 (RT_BITMAP): `.bmp`, `.dib`
- 4 (RT_MENU)
- 5 (RT_DIALOG)
- 6 (RT_STRING)
- 9 (RT_ACCELERATORS)
- 10 (RT_RCDATA): Every single other extension.
- 11 (RT_MESSAGETABLE)
- 12 (RT_GROUP_CURSOR): `.cur` (not yet supported)
- 14 (RT_GROUP_ICON): `.ico`
- 23 (RT_HTML): `.htm`, `.html`, `.mht`
- 24 (RT_MANIFEST): `.manifest`. If the name for the resource is not specified, it defaults to `1`

Example: To replace the standard icons (other than the main icon):

    ;@Ahk2Exe-AddResource Icon1.ico, 160  ; Replaces 'H on blue'
    ;@Ahk2Exe-AddResource Icon2.ico, 206  ; Replaces 'S on green'
    ;@Ahk2Exe-AddResource Icon3.ico, 207  ; Replaces 'H on red'
    ;@Ahk2Exe-AddResource Icon4.ico, 208  ; Replaces 'S on red'

### Bin

Specifies the Binary version of AutoHotkey to be included in the generated .exe. (This overrides anything specified in the GUI or CLI.) This directive can be specified many times if necessary, but only in the top level script file (i.e. not in an #Include file). The compiler will be run at least once for each Bin directive found. (If an actual comment is appended to this directive, it must use the `;` flag. To truly comment out this directive, insert a space after the first comment flag.)

    ;@Ahk2Exe-Bin [Path\]Name , [Exe_path\][Name], Codepage

[Path\]Name
  The *.bin file to use. If no extension is supplied, `.bin` is assumed. A DOS mask may be specified for Name, e.g. `ANSI*`, `Unicode 32*`, `Unicode 64*`, or `*bit` for all three. If no path is specified, the standard *.bin files in the compiler folder are searched. The compiler will be run for each *.bin file that matches. Any use of built-in variable replacements must only be from group 1 above.
[Exe_path\][Name]
  (Optional) The file name to be given to the .exe. Any extension supplied will be replaced by `.exe`. If no path is specified, the .exe will be created in the script folder. If no name is specified, the .exe will have the default name. (This parameter can be overridden by the ExeName directive.)
Codepage
  (Optional) Overrides the default codepage used to process script files. (Scripts should begin with a Unicode byte-order-mark (BOM), rendering the use of this parameter unnecessary.)

### ConsoleApp

Changes the executable subsystem to Console mode.

    ;@Ahk2Exe-ConsoleApp

### Cont

Specifies a continuation line for the preceding directive. This allows a long-lined directive to be formatted so that it is easy to read in the source code.

    ;@Ahk2Exe-Cont Text

Text
  The text to be appended to the previous directive line, before that line is processed. The text starts after the single space following the `Cont` key-word.

### Debug

Shows a msgbox with the supplied text, for debugging purposes.

    ;@Ahk2Exe-Debug Text

Text
  The text to be shown. Include any special variables between `%` signs to see the (manipulated) contents.

### ExeName

Specifies the location and name given to the generated .exe file. (Also see the Bin directive.)

    ;@Ahk2Exe-ExeName [Path\][Name]

[Path\][Name]
  The .exe file name. Any extension supplied will be replaced by `.exe`. If no path is specified, the .exe will be created in the script folder. If no name is specified, the .exe will have the default name.

Example:

    ;@Ahk2Exe-Obey U_bits, = %A_PtrSize% * 8
    ;@Ahk2Exe-Obey U_type, = "%A_IsUnicode%" ? "Unicode" : "ANSI"
    ;@Ahk2Exe-ExeName %A_ScriptName~\.[^\.]+$%_%U_type%_%U_bits%

### Let

Creates (or modifies) one or more user variables which can be accessed by `%U_Name%`, similar to the built-in variables (see above).

    ;@Ahk2Exe-Let Name = Value , Name = Value, ...

Name
  The name of the variable (with or without the leading `U_`).
Value
  The value to be used.

### Obey

Obeys isolated AutoHotkey commands or expressions, with result in `U_Name`.

    ;@Ahk2Exe-Obey Name, CmdOrExp , Extra

Name
  The name of the variable (with or without the leading `U_`) to receive the result.
CmdOrExp

  The command or expression to obey.

  Command format must use Name as the output variable (often the first parameter), e.g.

      ;@Ahk2Exe-Obey U_date, FormatTime U_date`, R D2 T2

  Expression format must start with `=`, e.g.

      ;@Ahk2Exe-Obey U_type, = "%A_IsUnicode%" ? "Unicode" : "ANSI"

  Expressions can be written in command format, e.g.

      ;@Ahk2Exe-Obey U_bits, U_bits := %A_PtrSize% * 8

  If needed, separate multiple commands and expressions with ``n`.

Extra
  (Optional) A number (1-9) specifying the number of extra results to be returned. e.g. if extra = 2, results will be returned in `U_name`, `U_name1`, and `U_name2`. The values in the `name`s must first be set by the expression or command.

### PostExec

Specifies a program to be executed after a successful compilation, before (or after) any Compression is applied to the .exe.

    ;@Ahk2Exe-PostExec Program [parameters] , When

Program [parameters]
  The program to execute, plus parameters. To allow access to the processed .exe file, specify the special variable A_WorkFileName as a quoted parameter, such as `"%A_WorkFileName%"`. If the program changes the .exe, the altered .exe must be moved back to the input file specified by `%A_WorkFileName%`, by the program. (Note that the .exe will contain binary data.)
When

  (Optional) Leave blank to execute before any Compression is done. Otherwise set to a number to run after compression as follows:

  - 0 - Only run when no compression is specified.
  - 1 - Only run when MPRESS compression is specified.
  - 2 - Only run when UPX compression is specified.

Example 1: (To use these examples, first download BinMod.ahk and compile it according to the instructions in the downloaded script.)

This example can be used to remove a reference to "AutoHotkey" in the generated .exe to disguise that it is a compiled AutoHotkey script:

    ;@Ahk2Exe-Obey U_au, = "%A_IsUnicode%" ? 2 : 1    ; Script ANSI or Unicode?
    ;@Ahk2Exe-PostExec "BinMod.exe" "%A_WorkFileName%"
    ;@Ahk2Exe-Cont  "%U_au%2.>AUTOHOTKEY SCRIPT<. DATA              "

Example 2: This example will alter a UPX compressed .exe so that it can't be de-compressed with `UPX -d`:

    ;@Ahk2Exe-PostExec "BinMod.exe" "%A_WorkFileName%"
    ;@Ahk2Exe-Cont  "11.UPX." "1.UPX!.", 2

There are other examples mentioned in the BinMod.ahk script.

### SetMainIcon

Overrides the custom EXE icon used for compilation. (To change the other icons, see the AddResource example.)

    ;@Ahk2Exe-SetMainIcon IcoFile

IcoFile
  (Optional) The icon file to use. If omitted, the default AutoHotkey icon is used.

### SetProp

Changes a property in the compiled executable's version information. Note that all properties are processed in alphabetical order, regardless of the order they are specified.

    ;@Ahk2Exe-SetProp Value

Prop

  The name of the property to change. Must be one of those listed below.

  Property | Description
  --- | ---
  CompanyName | Changes the company name.
  Copyright | Changes the legal copyright information.
  Description | Changes the file description.
  FileVersion | Changes the file version, in both text and raw binary format. (See Version below, for more details.)
  InternalName | Changes the internal name.
  Language | Changes the language code. Please note that hexadecimal numbers must have an `0x` prefix.
  LegalTrademarks | Changes the legal trademarks information.
  Name | Changes the product name and the internal name.
  OrigFilename | Changes the original filename information.
  ProductName | Changes the product name.
  ProductVersion | Changes the product version, in both text and raw binary format. (See Version below, for more details.)
  Version | Changes the file version and the product version, in both text and raw binary format. Ahk2Exe fills the binary version fields with the period-delimited numbers (up to four) that may appear at the beginning of the version text. Unfilled fields are set to zero. For example, `1.3-alpha` would produce a binary version number of `1.3.0.0`. If this property is not modified, it defaults to the AutoHotkey version used to compile the script.

Value
  The value to set the property to.

### Set

Changes other miscellaneous properties in the compiled executable's version information not covered by the SetProp directive. Note that all properties are processed in alphabetical order, regardless of the order they are specified. This directive is for specialised use only.

    ;@Ahk2Exe-Set Prop, Value

Prop
  The name of the property to change.
Value
  The value to set the property to.

### UpdateManifest

Changes details in the .exe's manifest. This directive is for specialised use only.

    ;@Ahk2Exe-UpdateManifest RequireAdmin , Name, Version, UIAccess

RequireAdmin
  Set to 1 to change the executable to require administrative privileges when run. Set to 2 to change the executable to request highest available privileges when run. Set to 0 to leave unchanged.
Name
  (Optional) The name to be set in the manifest.
Version
  (Optional) The version to be set in the manifest.
UIAccess
  (Optional) Set to 1 to make UIAccess true in the manifest.

### UseResourceLang

Changes the resource language used by AddResource. This directive is positional and affects all AddResource directives that follow it.

    ;@Ahk2Exe-UseResourceLang LangCode

LangCode
  The language code. Please note that hexadecimal numbers must have an `0x` prefix. The default resource language is US English (0x0409).