Script compiler directives allow the user to specify details of how a script is to be compiled via Ahk2Exe. Some of the features are:
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 ;
).
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 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. ``
.
%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.
Adds a resource to the compiled executable. (Also see UseResourceLang below)
;@Ahk2Exe-AddResource FileName , ResourceName
*type Filename
. If omitted, Ahk2Exe automatically detects the type according to the file extension.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
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'
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
.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
. 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.)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
Cont
key-word.Shows a message box with the supplied text, for debugging purposes.
;@Ahk2Exe-Debug Text
%
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]
.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.;@Ahk2Exe-Obey U_bits, = %A_PtrSize% * 8 ;@Ahk2Exe-Obey U_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, ...
U_
).Obeys isolated AutoHotkey commands or expressions, with result in U_Name
.
;@Ahk2Exe-Obey Name, CmdOrExp , Extra
U_
) to receive the result.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
.
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 (or after) any Compression is applied to the .exe.
;@Ahk2Exe-PostExec Program [parameters] , When, WorkingDir, Hidden, IgnoreErrors
"%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.)(Optional) Leave blank to execute before any Compression is done. Otherwise set to a number to run after compression as follows:
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.
Overrides the custom EXE icon used for compilation. (To change the other icons, see the AddResource example.)
;@Ahk2Exe-SetMainIcon IcoFile
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
The name of the property to change. Must be one of those listed below.
Property | Descrição |
---|---|
CompanyName | Changes the company name. |
Copyright | Changes the legal copyright information. |
Descrição | 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. |
Nome | 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.) |
Versão |
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, |
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
Changes details in the .exe's manifest. This directive is for specialised use only.
;@Ahk2Exe-UpdateManifest RequireAdmin , Name, Version, UIAccess
Changes the resource language used by AddResource. This directive is positional and affects all AddResource directives that follow it.
;@Ahk2Exe-UseResourceLang LangCode
0x
prefix. The default resource language is US English (0x0409).