Script Compiler (Ahk2Exe) Directives [v1.1.??+]

Note: This file is depreciated! The updated documentation is now in the standard AutoHotkey.chm help file.

The script compiler accepts certain directives that allow further customisation the compiled script (.exe).
All compiler directives are introduced by the string @Ahk2Exe-, preceded by the comment flag.

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

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 example below for more information.)

As well, special user variables can be created with the format U_Name using the Let directive, 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:

AddResource

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

;@Ahk2Exe-AddResource FileName [, ResourceName]
FileNameThe filename of the resource to add. The type of the resource (as an integer or string) can be explicitly specified 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.

Bin

Specifies the Binary version of AutoHotkey to be included in the generated .exe. (Also see the ExeName directive.)
This directive can be specified many times if necessary, but only in the top level script file (i.e. not in an #Include file).
(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\]NameThe *.bin file to use. If no extension is supplied, .bin is assumed.
A DOS mask may be specified for the name part,
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
TextThe 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
TextThe 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.
An example:
;@Ahk2Exe-Obey bits, = %A_PtrSize% * 8
;@Ahk2Exe-Obey 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 [, ...]]
NameThe name of the variable (with or without the leading U_).
ValueThe value to be used.

Obey

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

;@Ahk2Exe-Obey Name, CmdOrExp [, Extra]
NameThe name of the variable (with or without the leading U_) to receive the result.
CmdOrExpThe command or expression to obey.
Expression format must start with =,
e.g. Obey type, = "%A_IsUnicode%" ? "Unicode" : "ANSI".
Command format must use Name as the output variable (often the first parameter),
e.g. Obey date, FormatTime date`, R D2 T2
(Expressions can be written in command format,
e.g. Obey bits, bits := %A_PtrSize% * 8.
If needed, separate multiple commands, 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 names must first be set by the expression or command.

PostExec

Specifies a program to be executed after a successful compilation, before any compression is applied to the .exe.

;@Ahk2Exe-PostExec Program [parameters]
ProgramThe program [plus parameters] to execute.
To allow access to the processed .exe file, specify the built-in variable %A_WorkFileName% as a parameter, between double-quotes. (See above.)
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.)

SetMainIcon

Overrides the custom EXE icon used for compilation.

;@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
PropThe name of the property to change. Must be one of those listed below.
ValueThe value to set the property to.

The following properties are supported by this directive:

CompanyNameChanges the company name.
CopyrightChanges the legal copyright information.
DescriptionChanges the file description.
FileVersionChanges the file version, in both text and raw binary format.
InternalNameChanges the internal name.
LanguageChanges the language code.
Please note that hexadecimal numbers must have an 0x prefix.
LegalTrademarksChanges the legal trademarks information.
NameChanges the product name and the internal name.
OrigFilenameChanges the original filename information.
ProductNameChanges the product name.
ProductVersionChanges the product version, in both text and raw binary format.
VersionChanges 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.

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
PropThe name of the property to change.
ValueThe 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]
RequireAdminSet to 1 to change the executable to require administrative privileges when run. Set to 0 to leave unchanged.
Name(Optional) The name to be set in the manifest. (Not normally used.)
Version(Optional) The version to be set in the manifest. (Not normally used.)

UseResourceLang

Changes the resource language used by AddResource.

;@Ahk2Exe-UseResourceLang LangCode
LangCodeThe language code.
Please note that hexadecimal numbers must have an 0x prefix.
The default resource language is US English (0x0409).
This directive is positional and affects all AddResource directives that follow it.