_

Useful stdlib functions.

Summary
_Useful stdlib functions.
_StdLib loader.
mDebug function.
SStruct function.
vStorage function, designed to use as stdlib or copy and enhance.
tTimer
dDelay/Thread function.
FatalExits the script with the message.
About

_

_(opt = "")

StdLib loader.

Parameters

optSpace separated list of script options.

Options

sSpeed, defaults to -1
mAffects how m function works: mm makes it use MsgBox (default), mo OutputDebug, m alone disables it.  Anything else will use FileAppend; for instance mout.txt! writes to out.txt file.  ! at the end is optional and if present, it will mark the file for deletition on scripts startup.  !can be also used with o mode to clear the DebugView log.  DebugView will be started if it doesn’t run, make sure its on the system PATH (there will be no error message if Run fails).
dDetect hidden windows.
eEscape exits the script.  Use ea to exit the script only if its window is active.
wSetWorkingDir %A_ScriptDir%
tTitle match mode: t1 (ts), t2 (tc), t3 (te), tr.

Example

_("s100 d e m") ;set speed to 100ms, detect hiden windows and disable m, exit on ESC.
_("mo w tc)     ;set m to use OutputDebug, set working directory to A_ScriptDir, set title match mode to c (contain, 2).
_("mout.txt!")  ;set m to use File out.txt and to clear it each time script is started.

Remarks

Includes #NoEnv and #SingleInstance force always.  Registers global variable _ to contain A_Space.

m

m(o1 = "~`a",
o2 = "~`a",
o3 = "~`a",
o4 = "~`a",
o5 = "~`a",
o6 = "~`a",
o7 = "~`a",
o8 = "~`a")

Debug function.

Parameters

o1..o8Arguments to display.

Remarks

m can use MsgBox (m mode), OutputDebug (o mode) or FileAppend to write messages.  See _ function for details.  In o mode, all arguments will be joined in single line.

Returns

o1.

Examples

if (x - m(y) = z)  ; Use m inside expressions for debugging.

S

S(ByRef S,  
 pQ,  
ByRef o1 = "~`a ",
ByRef o2 = "",
ByRef o3 = "",
ByRef o4 = "",
ByRef o5 = "",
ByRef o6 = "",
ByRef o7 = "",
ByRef o8 = "")

Struct function.  With S, you define structure, then you can put or get values from it.  It also allows you to create your own library of structures that can be included at the start of the program.

Define

SStruct definition.  First word is struct name followed by : and a space, followed by space separated list of field definitions.  Field definition consists of field name, optionally followed by = sign and decimal number representation of offset and type.  For instance, “left=4.1” means that field name is “left”, field offset is 4 bytes and field type is 1 (UChar).  You can omit field decimal in which case “Uint” is used as default type and offset is calculated from previous one (or it defaults to 0 if it is first field in the list).  Precede type number with 0 to make it signed type or with 00 to make it Float or Double.  For instance, .01 is “Char” and .004 is Float.  S will calculate the size of the struct for you based on the input fields.  If you don’t define entire struct (its perfectly valid to declare only parts of the struct you are interested in) you can still define struct size by including = and size after structs name.  This allows you to use ! mode later.

Define Syntax

pQ       :: StructName[=[Size]]: FieldDef1 FieldDef2 ... FieldDefN
FieldDef :: FieldName[=[Def]
Def      :: offset.[0][0]Type
Type     :: [0]1 | [0]2 | [0]4 | [0]8 | 004 | 008

Put & Get

SPointer to struct data.
pQQuery parameter.  First word is struct name followed by the mode char and a space, followed by the space separated list of field names.  If the first char after struct name is “<” or “)” function will work in Put mode, if char is “>” or “)” it works in “Get” mode.  If char is “!” function works in IPut mode (Initialize & Put).  For ! to work, you must define entire struct, not just part of it.  The difference between < and ( is that < works on binary data contained in S, while ( works on binary data pointed to by S. The same difference applies to > and ) modes.
o1..o8Reference to output variables (Get) or input variables (Put)

Put & Get Syntax

pQ :: StructName[>)<(!]: FieldName1 FieldName2 ... FieldNameN

Returns

  • In Define mode function returns struct size.
  • In Get/Put mode function returns o1.

Otherwise the result contains description of the error.

Examples

Define Examples:
        S("RECT=16: left=0.4 top=4.4 right=8.4 bottom=12.4")        ;Define RECT explicitly.
        S("RECT: left top right bottom")    ; Define RECT struct with auto struct size and auto offset increment. Returns 16. The same as above.
        S("RECT: right=8 bottom")           ; Define only 2 fields of RECT struct. Since the fields are last one, ! can be used afterwards. Returns 16.
        S("RECT: top=4)                     ; Defines only 1 field of the RECT. Returns 8, so ! can't be used.
        S("RECT=16: top=4)                  ; Defines only 1 field of the RECT and overrides size. Returns 16, so ! can be used.
        S("R: x=.1 y=.02 k z=28.004")       ; Define R, size don't care. R.x is UChar at 0, R.y is Short at 1, R.k is Uint at 3 and  R.z is Float at 28.
        S("R=48: x=.1 y=.02 k z=28.004")    ; Override struct size. Returns user size (48 in this case).
                                            ;  This is not the same as above as it states that z is not the last field of the R struct and that actual size is 48.

Get & Put Examples:
        S(b, "RECT< left right", x,y)       ; b.left := x, b.right := y (b must be initialized)
        S(b, "RECT> left right", x,y)       ; x := b.left, y := b.right
        S(b, "RECT! left right", x,y)       ; VarSetCapacity(b, SizeOf(RECT)), b.left = x, b.right=y
        S(b:=&buf,"RECT) left right", x,y)  ; *b.left = x, *b.right=y
        S(b:=&buf,"RECT( left right", x,y)  ; x := *b.left , y := *b.right

v

v( var = "",
 value = "~`a ",
ByRef o1 = "",
ByRef o2 = "",
ByRef o3 = "",
ByRef o4 = "",
ByRef o5 = "",
ByRef o6 = "")

Storage function, designed to use as stdlib or copy and enhance.

Parameters

varVariable name to retrieve.  To get up several variables at once (up to 6), omit this parameter.
valueOptional variable value to set.  If var is empty value contains list of vars to retrieve with optional prefix
o1 .. o6If present, reference to variables to receive values.

Returns

  • if value is omitted, function returns the current value of var
  • if value is set, function sets the var to value and returns previous value of the var
  • if var is empty, function accepts list of variables in value and returns values of those variables in o1 .. o5

Remarks

To use multiple storages, copy v function and change its name.

You can choose to initialize storage from additional ahk script containing only list of assignments to storage variables, to do it internally by adding the values to the end of the your own copy of the function, or to do both, by accepting user values on startup, and checking them afterwards.  If you use stdlib module without including it directly, just make v.ahk script and put variable definitions there.

Don’t use storage variables that consist only of _ character as those are used to regulate inner working of function.

Examples

v(x)        ; returns value of x or value of x from v.ahk inside scripts dir.
v(x, v)     ; set value of x to v and return previous value
v("", "x y z", x, y, z)             ; get values of x, y and z into x, y and z
v("", "prefix_)x y z", x, y, z) ; get values of prefix_x, prefix_y and prefix_z into x, y and z

t

t(ByRef v = "~`a ")

Timer

Parameters

vReference to output variable.  Omit to reset timer.

Returns

v

Example

t()
loop, 10000
    f1()
p := t()

loop, 10000
    f2()
t(k)
m(p, k)

d

d(fun,  
delay = "",
a1 = "",
a2 = "")

Delay/Thread function.  Thread is not OS thread, but AHK thread.

Parameters

funFunction to be executed.
delayDelay in ms.
a1, a2Parameters.

Example

d("fun1", "",  1)   ;execute asap in new thread with param 1
d("fun2", 500, "abc")   ;execute after 500ms in new thread with param "abc"

Fatal

Fatal(Message,  
E = 1,
ExitCode = "")

Exits the script with the message.

Parameters

EExpression.  By default 1.  The function will exit the script only when E is True.
MessageMessage to show.
ExitCodeExit code to return to the caller.

About

_(opt = "")
StdLib loader.
m(o1 = "~`a",
o2 = "~`a",
o3 = "~`a",
o4 = "~`a",
o5 = "~`a",
o6 = "~`a",
o7 = "~`a",
o8 = "~`a")
Debug function.
S(ByRef S,  
 pQ,  
ByRef o1 = "~`a ",
ByRef o2 = "",
ByRef o3 = "",
ByRef o4 = "",
ByRef o5 = "",
ByRef o6 = "",
ByRef o7 = "",
ByRef o8 = "")
Struct function.
v( var = "",
 value = "~`a ",
ByRef o1 = "",
ByRef o2 = "",
ByRef o3 = "",
ByRef o4 = "",
ByRef o5 = "",
ByRef o6 = "")
Storage function, designed to use as stdlib or copy and enhance.
t(ByRef v = "~`a ")
Timer
d(fun,  
delay = "",
a1 = "",
a2 = "")
Delay/Thread function.
Fatal(Message,  
E = 1,
ExitCode = "")
Exits the script with the message.
Close