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.
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.
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. ``
.
%A_ScriptName~\.[^\.]+$~.exe%
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
)CodeVersion := "1.2.3.4", company := "My Company"
;@Ahk2Exe-Let version = %A_PriorLine~U)^(.+"){1}(.+)".*$~$2%
;@Ahk2Exe-Let company = %A_PriorLine~U)^(.+"){3}(.+)".*$~$2%
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.{1}
in the first regex was changed to {3}
in the second regex to select after the third "
to extract the company name.)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 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.
.bmp
, .dib
.cur
(not yet supported).ico
.htm
, .html
, .mht
.manifest
. If the name for the resource is not specified, it defaults to 1
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\]Name | The *.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.) |
Changes the executable subsystem to Console mode.
;@Ahk2Exe-ConsoleApp
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. |
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. |
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%
|
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. |
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. 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 name s must first be set by the expression or command. |
Specifies a program to be executed after a successful compilation, before any compression is applied to the .exe.
;@Ahk2Exe-PostExec Program [parameters]
Program | The 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.) |
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. |
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. |
Value | The value to set the property to. |
The following properties are supported by this directive:
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. |
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. |
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. |
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. |
Changes details in the .exe's manifest. This directive is for specialised use only.
;@Ahk2Exe-UpdateManifest RequireAdmin [, Name, Version]
RequireAdmin | Set 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.) |
Changes the resource language used by AddResource
.
;@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). This directive is positional and affects all AddResource directives that follow it. |