skip to main tutorial:
jeeswg's objects tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 12#p147112
see also:
jeeswg's object classes tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=54588
==================================================
CONTENTS
> LINKS (DOCUMENTATION)
> LINKS (FURTHER)
> FUNCTIONS/COMMANDS
> FUNCTIONS/COMMANDS (NOTES)
> COM.AHK
> AHK FILE OBJECT
> EXPLORER / EXPLORER WINDOWS
> INTERNET EXPLORER CONTROLS (INTERNET EXPLORER / HTML HELP)
> CUSTOM CLASSES
> CUSTOM CLASSES (CASE SENSITIVE/CASE INSENSITIVE ARRAYS)
> SCRIPTING.DICTIONARY OBJECT
> ACC LIBRARY (MSAA)
> HTM (WINHTTPREQUEST)
> HTM (HTMLFILE)
> IMAGES (WIA)
> TEXT TO SPEECH (SAPI)
> MS EXCEL, MS WORD
==================================================
> LINKS (DOCUMENTATION)
A list of key object-related links:
Size of htm in KB on the left (based on decompiled chm file v1.1.24.04).
[summary of functions]
https://autohotkey.com/docs/AHKL_Features.htm
https://autohotkey.com/docs/commands/index.htm
[contain 'obj' in file name][or appear in 'objects' folder]
[or pages with similar titles/related pages]
5 https://autohotkey.com/docs/misc/Arrays.htm
14 https://autohotkey.com/docs/objects/Object.htm
47 https://autohotkey.com/docs/Objects.htm
10 https://autohotkey.com/docs/objects/File.htm
7 https://autohotkey.com/docs/commands/FileOpen.htm
1 https://autohotkey.com/docs/commands/Func.htm
4 https://autohotkey.com/docs/objects/Func.htm
5 https://autohotkey.com/docs/objects/Functor.htm
2 https://autohotkey.com/docs/objects/Enumerator.htm
7 https://autohotkey.com/docs/commands/ComObjActive.htm
3 https://autohotkey.com/docs/commands/ComObjArray.htm
5 https://autohotkey.com/docs/commands/ComObjConnect.htm
1 https://autohotkey.com/docs/commands/ComObjCreate.htm
1 https://autohotkey.com/docs/commands/ComObjError.htm
2 https://autohotkey.com/docs/commands/ComObjFlags.htm
2 https://autohotkey.com/docs/commands/ComObjGet.htm
4 https://autohotkey.com/docs/commands/ComObjQuery.htm
4 https://autohotkey.com/docs/commands/ComObjType.htm
1 https://autohotkey.com/docs/commands/ComObjValue.htm
1 https://autohotkey.com/docs/commands/IsObject.htm
2 https://autohotkey.com/docs/commands/ObjAddRef.htm
0 https://autohotkey.com/docs/commands/ObjBindMethod.htm
[Catch,Finally,For,Try]
2 https://autohotkey.com/docs/commands/Catch.htm
3 https://autohotkey.com/docs/commands/Finally.htm
6 https://autohotkey.com/docs/commands/For.htm
4 https://autohotkey.com/docs/commands/Try.htm
[Throw]
3 https://autohotkey.com/docs/commands/Throw.htm
==================================================
> LINKS (FURTHER)
Concepts and Conventions | AutoHotkey
https://autohotkey.com/docs/Concepts.htm#objects
https://autohotkey.com/docs/Concepts.ht ... t-protocol
https://autohotkey.com/docs/Concepts.htm#caching
https://autohotkey.com/docs/Concepts.htm#methods
https://autohotkey.com/docs/Concepts.ht ... to-objects
Scripting Language | AutoHotkey
https://autohotkey.com/docs/Language.ht ... or-objects
COM Object Reference [AutoHotkey v1.1+] - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/5698 ... otkey-v11/
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=77
[share an object between 2 scripts]
ObjRegisterActive - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=6148
[hotkeys with function object][Hotkey, If, % FunctionObject]
log hotkeys, get extended hotkey info, interact with the *right* window (Hotkey command with FunctionObject example) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=29560
[identify the object type][code to replicate the AHK v2 'Type' function in AHK v1]
AutoHotkey-Future/Type.ahk at master · cocobelgica/AutoHotkey-Future · GitHub
https://github.com/cocobelgica/AutoHotk ... b/Type.ahk
type(v) for v1.1 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=2306
[check if array contains string]
if Var in MatchList not working with array - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=30150
Checks if a value exists in an array - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=23286
More Simple Array Commands - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=7619
AHK_L - Contains/HasValue Method - Suggestions - AutoHotkey Community
https://autohotkey.com/board/topic/8400 ... ue-method/
[search/compare/edit arrays]
More Simple Array Commands - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=7619
Array/object wishlist (count, printarr, contains, other). - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=7101
==================================================
> FUNCTIONS/COMMANDS
Alphabetical Command and Function Index | AutoHotkey
https://autohotkey.com/docs/commands/index.htm
Array https://autohotkey.com/docs/Objects.htm#Usage
Object https://autohotkey.com/docs/Objects.htm ... ive_Arrays
ComObjActive https://autohotkey.com/docs/commands/ComObjActive.htm
ComObjArray https://autohotkey.com/docs/commands/ComObjArray.htm
ComObjConnect https://autohotkey.com/docs/commands/ComObjConnect.htm
ComObjCreate https://autohotkey.com/docs/commands/ComObjCreate.htm
ComObjError https://autohotkey.com/docs/commands/ComObjError.htm
ComObjFlags https://autohotkey.com/docs/commands/ComObjFlags.htm
ComObjGet https://autohotkey.com/docs/commands/ComObjGet.htm
ComObjQuery https://autohotkey.com/docs/commands/ComObjQuery.htm
ComObjType https://autohotkey.com/docs/commands/ComObjType.htm
ComObjValue https://autohotkey.com/docs/commands/ComObjValue.htm
ComObject https://autohotkey.com/docs/commands/ComObjActive.htm
ComObject https://lexikos.github.io/v2/docs/comma ... Object.htm
FileOpen https://autohotkey.com/docs/commands/FileOpen.htm
Func https://autohotkey.com/docs/commands/Func.htm
IsObject https://autohotkey.com/docs/commands/IsObject.htm
ObjAddRef https://autohotkey.com/docs/commands/ObjAddRef.htm
ObjRelease https://autohotkey.com/docs/commands/ObjAddRef.htm
ObjBindMethod https://autohotkey.com/docs/commands/ObjBindMethod.htm
ObjGetBase https://autohotkey.com/docs/objects/Object.htm#GetBase
ObjRawGet https://autohotkey.com/docs/objects/Object.htm#RawGet
ObjRawSet https://autohotkey.com/docs/objects/Object.htm#RawSet
ObjSetBase https://autohotkey.com/docs/objects/Object.htm#SetBase
[deprecated]
ComObjEnwrap https://autohotkey.com/docs/commands/ComObjActive.htm
ComObjUnwrap https://autohotkey.com/docs/commands/ComObjActive.htm
ComObjMissing https://autohotkey.com/docs/commands/ComObjActive.htm
ComObjParameter https://autohotkey.com/docs/commands/ComObjActive.htm
[AHK v2 only]
Type https://lexikos.github.io/v2/docs/commands/Type.htm
also appear in index: (Catch, Finally, For, Try) (Throw, Exception)
[function versions of methods][methods (equivalent functions)]
Basic Object - Methods & Properties | AutoHotkey
https://autohotkey.com/docs/objects/Object.htm
Each method also has an equivalent function, which can be used to bypass any custom behaviour implemented by the object
ObjInsertAt / ObjRemoveAt
ObjPush / ObjPop
ObjDelete
ObjMinIndex / ObjMaxIndex / ObjLength / ObjCount
ObjSetCapacity / ObjGetCapacity
ObjGetAddress
ObjNewEnum
ObjHasKey
ObjClone
[deprecated]
ObjInsert
ObjRemove
==================================================
> FUNCTIONS/COMMANDS (NOTES)
list of every command/function/variable from across all versions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 42#p131642
list of every object type/property/method - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 47#p199847
note: deprecated: ComObjEnwrap, ComObjMissing, ComObjParameter, ComObjUnwrap
note: no such function: ComObj() does not appear anywhere in the documentation
custom function:
ComVar() [see: https://autohotkey.com/docs/commands/ComObjActive.htm]
functions/variables involving arrays (not necessarily a complete list):
A_Args [AHK v2] [this looks like the other 'A_' variables, but is an array]
Exception
IsFunc
String [checks object's ToString method] [AHK v2]
StrSplit
WinGetControls [AHK v2]
WinGetControlsHWND [AHK v2]
WinGetList [AHK v2]
object key words:
class
new
object syntax
obj.key
obj[key]
recreate ComObjMissing in AHK v1/v2:
m := ComObjMissing() ;AHK v1
m := ComObject(0xA, 0x80020004) ;AHK v1/v2
from:
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 36#p131336
functions that will be removed in AHK v2:
v2-changes
https://autohotkey.com/v2/v2-changes.htm
•ComObjMissing() (write two consecutive commas instead)
•ComObjUnwrap() (use ComObjValue() instead, and ObjAddRef() if needed)
•ComObjEnwrap() (use ComObject() instead, and ObjAddRef() if needed)
•ComObjXXX() where XXX is anything other than one of the explicitly defined ComObj functions (use ComObjActive() or ComObject() instead).
•ComObjParameter() -> ComObject()
==================================================
> COM.AHK
[note: this is not needed in AHK v1.1 since COM handling is built-in, however, the script is useful for understanding the code]
COM.ahk, best version? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 9&p=140512
==================================================
[to come]
> AHK FILE OBJECT
==================================================
> EXPLORER / EXPLORER WINDOWS
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31755
Explorer window interaction (folder windows/Desktop, file/folder enumeration/selection/navigation/creation) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=35041
Explorer column interaction (get/set: which appear, width, ascending/descending order) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=33129
[get file properties via objects e.g. length (duration), image dimensions]
[uses ComObjCreate("Shell.Application") and GetDetailsOf]
FileGetProperties - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6369 ... roperties/
==================================================
> INTERNET EXPLORER CONTROLS (INTERNET EXPLORER / HTML HELP)
jeeswg's Internet Explorer and HTML tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31766
Basic Webpage Controls with JavaScript / COM - Tutorial - Tutorials - AutoHotkey Community
https://autohotkey.com/board/topic/4705 ... -tutorial/
==================================================
[to come]
> CUSTOM CLASSES
__Get / __Set / __Call only take place for a key that does not yet exist
__Set:
consequences of a 'return' line being present:
if not present: 'key := value' takes place
if present: 'key := place' does not take place, you can either not set the key at all, or you can set the key manually, (however if you try to set the key in the normal way you will trigger __Set and get an infinite loop, so to bypass this, use ObjRawSet)
[custom classes]
Classes in AHK, Basic tutorial. - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 95#p141395
==================================================
> CUSTOM CLASSES (CASE SENSITIVE/CASE INSENSITIVE ARRAYS)
case sensitive/case insensitive arrays (maintain key order, no key autosort) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 84#p137384
[case sensitive, alphabetical order array]
Object
https://autohotkey.com/docs/objects/Object.htm
[case insensitive, creation order array]
[AHK_L] For Loop in order of key-value pair creation - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6179 ... /?p=389662
[case sensitive, alphabetical order array]
?
[case sensitive, creation order array]
Case sensitive variables possible? - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6182 ... -possible/
How make Associative array keys case sensitive - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/6184 ... sensitive/
==================================================
[to come]
> SCRIPTING.DICTIONARY OBJECT
COM Object Reference [AutoHotkey v1.1+] - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/5698 ... ntry357748
[Scripting.Dictionary]
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... p=396#p396
==================================================
> ACC LIBRARY (MSAA)
jeeswg's Acc tutorial (Microsoft Active Accessibility) (MSAA) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=40590
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=26201
==================================================
[to come]
> HTM (WINHTTPREQUEST)
[WinHttpRequest]
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... p=397#p397
download urls to vars, partially/fully, via WinHttpRequest - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26528
==================================================
[to come]
> HTM (HTMLFILE)
[HTMLFile]
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... p=398#p398
==================================================
[to come]
> IMAGES (WIA)
[WIA]
COM Object Reference - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... p=619#p619
==================================================
> TEXT TO SPEECH (SAPI)
[SAPI]
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... p=489#p489
SAPI parameters, rate, volume and async speech? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=33651
==================================================
[to come]
> MS EXCEL, MS WORD
[function: Excel_Get]
[Excel_Get and 'Word_Get']
[get access to a specific Excel/Word window for use as an object]
excel and com? - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7394 ... ntry469769
[for MS Excel and MS Word]
MS Office COM Interface Constants - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=22&t=1672
MS Office COM Basics - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=8978
[check if MS Office constants are defined]
Translate from VBA to COM? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 66#p145866
==================================================
jeeswg's objects tutorial
jeeswg's objects tutorial
Last edited by jeeswg on 25 Aug 2018, 14:54, edited 10 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: jeeswg's objects tutorial (not yet complete)
Code: Select all
;[updated: 2018-09-20]
;==================================================
;BASICS (CREATE/DELETE OBJECTS, GET/SET A KEY'S VALUE)
;INI TO ARRAY
;INI TO ARRAY (HANDY TRICK)
;OBJECT() V. ARRAY()
;KEY NAMES (NUMERIC V. STRING)
;FUNCTIONALITY OF OBJECT()
;REFER TO KEYS
;LOOPING THROUGH AN OBJECT'S KEYS AND VALUES
;FOR LOOP EXAMPLES
;A KEY CAN HAVE A VALUE OR CHILD KEYS, BUT NOT BOTH
;ARRAY STARTING AT 0/1/OTHER
;PUSH KEYS / ASSIGN MULTIPLE KEYS
;FREQUENCY COUNT (CASE INSENSITIVE) (SORT)
;FREQUENCY COUNT (CASE INSENSITIVE) (MAINTAIN ORDER)
;FREQUENCY COUNT (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
;REMOVE DUPLICATES (CASE INSENSITIVE) (MAINTAIN ORDER/SORT)
;REMOVE DUPLICATES (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
;==================================================
;BASICS (CREATE/DELETE OBJECTS, GET/SET A KEY'S VALUE)
;create an array/object (all 5 lines are equivalent)
obj := []
obj := Array()
obj := {}
obj := Object()
obj := StrSplit("")
;create an array/object (all 5 lines are equivalent)
obj := ["value1", "value2", "value3"]
obj := Array("value1", "value2", "value3")
obj := {1:"value1", 2:"value2", 3:"value3"}
obj := Object(1, "value1", 2, "value2", 3, "value3")
obj := StrSplit("value1,value2,value3", ",")
;4 further examples equivalent to the 5 above:
objtemp := StrSplit("1,value1,2,value2,3,value3", ",")
obj := Object(objtemp*)
objtemp := StrSplit("1=value1,2=value2,3=value3", ["=", ","])
obj := Object(objtemp*)
obj := Object(StrSplit("1,value1,2,value2,3,value3", ",")*)
obj := Object(StrSplit("1=value1,2=value2,3=value3", ["=", ","])*)
;refer to a key
MsgBox, % obj.1
MsgBox, % obj[1] ;same as above
Loop, 3
MsgBox, % obj[A_Index]
;delete an object
obj := ""
;list keys and values
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;==================================================
;INI TO ARRAY
;ini to array
vText := "
(
a=1
b=2
c=3
)"
oArray := {}
Loop, Parse, vText, `n, `r
{
oTemp := StrSplit(A_LoopField, "=")
oArray[oTemp.1] := oTemp.2
}
MsgBox, % oArray.c
MsgBox, % oArray["c"]
vList := "a,b,c"
vOutput := ""
Loop, Parse, vList, % ","
vOutput .= A_LoopField " " oArray[A_LoopField] "`r`n"
MsgBox, % vOutput
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;==================================================
;INI TO ARRAY (HANDY TRICK)
;ini to array
vText := "
(
a=1
b=2
c=3
)"
oArray := Object(StrSplit(vText, ["=", "`n"])*)
;two columns to array
vText := "
(
a 1
b 2
c 3
)"
oArray := Object(StrSplit(vText, ["`t", "`n"])*)
;note the * is for use with variadic functions:
;the following 4 lines are equivalent:
oArray := Object("a", "A", "b", "B", "c", "C")
oArray := Object(["a", "A", "b", "B", "c", "C"]*)
oTemp := ["a", "A", "b", "B", "c", "C"], oArray := Object(oTemp*)
oTemp := StrSplit("a,A,b,B,c,C", ","), oArray := Object(oTemp*)
;==================================================
;OBJECT() V. ARRAY()
;Differentiating Between Array and Object - Ask for Help - AutoHotkey Community
;https://autohotkey.com/board/topic/95303-differentiating-between-array-and-object/
;Objects and arrays are exactly the same thing in AutoHotkey, there is no difference. [a, b, c] is just syntax sugar for {1: a, 2: b, 3: c}.
;==================================================
;KEY NAMES (NUMERIC V. STRING)
;note: in AHK v2:
;obj["1"] is equivalent to obj[1]
;and you cannot create a string key called '1'
;the rest of the information in this section is mostly AHK v1-specific
;get key's value - where key name looks numeric
obj := {}
obj[1] := "value1" ;key where '1' is numeric
obj["1"] := "value2" ;key where '1' is string
MsgBox, % obj[1] " " obj["1"] ;value1 value2
MsgBox, % obj.1 ;value1
obj := {}
obj["1"] := "value2"
MsgBox, % obj.1 ;blank
;get key's value - where key name doesn't look numeric
obj := {}
var := "varcontents"
obj[var] := "value1"
MsgBox, % obj[var] ;value1
MsgBox, % obj.varcontents ;value1
MsgBox, % obj["varcontents"] ;value1
obj := {}
obj["a"] := "value1"
MsgBox, % obj.a ;value1
MsgBox, % obj["a"] ;value1
;define objects - where key name looks numeric
obj := {1:"value1", 2:"value2", 3:"value3"}
MsgBox, % obj.1 ;value1
obj := {"1":"value1", "2":"value2", "3":"value3"}
MsgBox, % obj.1 ;blank
obj := Object(1, "value1", 2, "value2", 3, "value3")
MsgBox, % obj.1 ;value1
obj := Object("1", "value1", "2", "value2", "3", "value3")
MsgBox, % obj.1 ;blank
obj := {0xF:"value15"}
MsgBox, % obj.0xF ;value15
MsgBox, % obj.15 ;value15
;define objects - where key name doesn't look numeric
obj := {a:"value1", b:"value2", c:"value3"}
MsgBox, % obj.a ;value1
obj := {"a":"value1", "b":"value2", "c":"value3"}
MsgBox, % obj.a ;value1
var := "varcontents"
obj := Object(var, "value1")
MsgBox, % obj.varcontents ;value1
obj := Object("a", "value1", "b", "value2", "c", "value3")
MsgBox, % obj.a ;value1
;==================================================
;FUNCTIONALITY OF OBJECT()
;0 parameters: create an empty array
obj := Object()
;2n parameters (where n is a positive integer): create an array with keys
obj := Object("key1", "value1", "key2", "value2", "key3", "value3")
;1 parameter: get a pointer to the object
address := Object(obj)
;1 parameter: get the object in a usable form
obj := Object(address)
;==================================================
;REFER TO KEYS
obj1 := {}
obj2 := {}
obj1.a := "A" ;this works
obj2["a"] := "A" ;this works
MsgBox, % obj1.a ;A
MsgBox, % obj2.a ;A
obj1.b.c := "BC" ;this does not work
obj2["b", "c"] := "BC" ;this works
MsgBox, % obj1.b.c ;blank
MsgBox, % obj2.b.c ;BC
obj1 := obj2 := ""
obj := {}
obj["a", "b", "c", "d", "e"] := "ABCDE"
MsgBox, % obj.a.b.c.d.e ;ABCDE
MsgBox, % obj.a.b["c"].d.e ;ABCDE
MsgBox, % obj["a"].b["c"].d["e"] ;ABCDE
MsgBox, % obj["a", "b"].c["d", "e"] ;ABCDE
var := "c"
MsgBox, % obj.a.b[var].d.e ;ABCDE
obj := {}
obj["a", "b", "c", "d", "e"] := "ABCDE"
MsgBox, % obj.a.b.c.d.e ;ABCDE
MsgBox, % obj.a.b["c"].d.e ;ABCDE
MsgBox, % obj["a", "b"].c["d", "e"] ;ABCDE
MsgBox, % obj["a"].b["c"].d["e"] ;ABCDE
MsgBox, % obj.a["b"].c["d"].e ;ABCDE
obj := {}
obj["a", "b", "c", "d", "e"] := "ABCDE"
obj2 := ["a", "b", "c", "d", "e"]
obj3 := ["a", "b", "c", "d"]
obj4 := ["b", "c", "d", "e"]
obj5 := ["b", "c", "d"]
MsgBox, % obj[obj2*] ;ABCDE
MsgBox, % obj[obj3*, "e"] ;blank
MsgBox, % obj[obj3*]["e"] ;ABCDE
MsgBox, % obj["a", obj4*] ;ABCDE
MsgBox, % obj["a", obj5*, "e"] ;blank
MsgBox, % obj["a", obj5*]["e"] ;ABCDE
;Functions
;https://autohotkey.com/docs/Functions.htm#VariadicCall
;Only the right-most parameter can be expanded this way. For example, Func(x, y*) is supported but Func(x*, y) is not.
;==================================================
;LOOPING THROUGH AN OBJECT'S KEYS AND VALUES
oArray := {a:"A", b:"B", c:"C"}
;generally this is how I would loop through an object:
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
;if values are not needed:
for vKey in oArray
vOutput .= vKey "`r`n"
;if keys are not needed (where '_' is a variable):
for _, vValue in oArray
vOutput .= vValue "`r`n"
;I would not do this (where 'each' is a variable):
for each, vValue in oArray
vOutput .= vValue "`r`n"
MsgBox, % vOutput
;if the object has numeric keys only, you could use the word 'index' instead of 'key', but I'd stick with 'key' always
vOutput := ""
oArray := ["A", "B", "C"]
;oArray := {1:"A", 2:"B", 3:"C"} ;equivalent
for vIndex, vValue in oArray
vOutput .= vIndex " " vValue "`r`n"
MsgBox, % vOutput
oArray := ""
;==================================================
;FOR LOOP EXAMPLES
;for loop examples (loop through an array's key-value pairs)
;list keys and values: linear array
oArray := ["a", "b", "c"]
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;list keys: linear array
oArray := ["a", "b", "c"]
vOutput := ""
for vKey in oArray
vOutput .= vKey "`r`n"
MsgBox, % vOutput
;list keys and values: associative array
oArray := {a:"A", b:"B", c:"C"}
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;list keys and values: linear array (via StrSplit)
oArray := StrSplit("abc,def,ghi", ",")
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;list keys and values: linear array (directly)
vOutput := ""
for vKey, vValue in ["a", "b", "c"]
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;list keys and values: associative array (directly)
vOutput := ""
for vKey, vValue in {a:"A", b:"B", c:"C"}
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;list keys and values: linear array (via StrSplit) (directly)
vOutput := ""
for vKey, vValue in StrSplit("abc,def,ghi", ",")
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;==================================================
;A KEY CAN HAVE A VALUE OR CHILD KEYS, BUT NOT BOTH
;an object's key can have a value or it can have child keys but not both
obj := {}
obj["a"] := "A"
MsgBox, % obj.a
obj["a", "b"] := "AB"
MsgBox, % obj.a.b ;(blank) ;obj.a is a value so cannot have subkeys
obj["a"] := {} ;if we make obj.a an object, it can now be given child keys
obj["a", "b"] := "AB"
MsgBox, % obj.a.b ;AB
obj := {}
obj["a", "b", "c"] := "ABC"
MsgBox, % obj.a.b.c ;ABC
obj["a", "b", "c", "d"] := "ABCD"
MsgBox, % obj.a.b.c.d ;(blank)
obj.a.b.Delete("c")
obj["a", "b", "c", "d"] := "ABCD"
MsgBox, % obj.a.b.c.d ;ABCD
;==================================================
;ARRAY STARTING AT 0/1/OTHER
;shift a linear array to start at a specific index
;array starting at 1
oArray := ["A", "B", "C"]
;array starting at 0
oArray := []
oArray.InsertAt(0, "A", "B", "C")
;array starting at 0 (one-liner)
(oArray := []).InsertAt(0, "A", "B", "C")
;array starting at 0 (alternative) (one-liner)
(oArray := ["A", "B", "C"]).RemoveAt(0)
;array starting at 11 (one-liner)
(oArray := []).InsertAt(11, "K", "L", "M")
;==================================================
;PUSH KEYS / ASSIGN MULTIPLE KEYS
;APPEND KEYS TO LINEAR ARRAY
;this will create keys 1, 2, 3
oArray := ["a", "b", "c"]
;we can add key 4 like so:
oArray.Push("d")
;or instead, we can add keys 4, 5, 6 like so:
oArray.Push("d", "e", "f")
;alternatively:
;we can assign key 4 like so:
oArray.4 := "d"
;or we can add(/overwrite) keys 4, 5, 6 like so:
for vKey, vValue in {4:"d", 5:"e", 6:"f"}
oArray[vKey] := vValue
;to list the keys
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;ADD(/OVERWRITE) KEYS TO ASSOCIATIVE ARRAY
;this will create keys a, b, c
oArray := {a:"A", b:"B", c:"C"}
;we can add(/overwrite) keys d, e, f like so:
for vKey, vValue in {d:"D", e:"E", f:"F"}
oArray[vKey] := vValue
;to list the keys
vOutput := ""
for vKey, vValue in oArray
vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput
;==================================================
;FREQUENCY COUNT (CASE INSENSITIVE) (SORT)
;q:: ;frequency count (case insensitive)
vText := "f,e,d,C,B,A,a,b,c,A,B,C"
oArray := {}
StrReplace(vText, ",",, vCount)
oArray.SetCapacity(vCount+1)
Loop, Parse, vText, % ","
if oArray.HasKey("z" A_LoopField)
oArray["z" A_LoopField]++
else
oArray["z" A_LoopField] := 1
;list all items
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey, vValue in oArray
vOutput .= vValue "`t" SubStr(vKey, 2) "`r`n"
MsgBox, % vOutput
;list items that appear n times
vNum := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey, vValue in oArray
if (vValue = vNum)
vOutput .= vValue "`t" SubStr(vKey, 2) "`r`n"
MsgBox, % vOutput
oArray := ""
return
;==================================================
;FREQUENCY COUNT (CASE INSENSITIVE) (MAINTAIN ORDER)
;q:: ;frequency count (case insensitive)
vText := "f,e,d,C,B,A,a,b,c,A,B,C"
oTemp := ComObjCreate("Scripting.Dictionary")
Loop, Parse, vText, % ","
{
vTemp := Format("{:L}", A_LoopField)
if oTemp.Exists("" vTemp)
oTemp.Item("" vTemp)++
else
oTemp.Item("" vTemp) := 1
}
;list all items
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
oArray := {}
oArray.SetCapacity(oTemp.Count)
Loop, Parse, vText, % ","
if !oArray.HasKey("z" A_LoopField)
{
oArray["z" A_LoopField] := 1
vOutput .= A_LoopField "`t" oTemp.Item("" Format("{:L}", A_LoopField)) "`r`n"
}
MsgBox, % vOutput
;list items that appear n times
vNum := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
oArray := {}
oArray.SetCapacity(oTemp.Count)
Loop, Parse, vText, % ","
if !oArray.HasKey("z" A_LoopField)
{
oArray["z" A_LoopField] := 1
vValue := oTemp.Item("" Format("{:L}", A_LoopField))
if (vValue = vNum)
vOutput .= A_LoopField "`t" vValue "`r`n"
}
MsgBox, % vOutput
oArray := oTemp := ""
return
;==================================================
;FREQUENCY COUNT (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
;w:: ;frequency count (case sensitive)
vText := "f,e,d,C,B,A,a,b,c,A,B,C"
oArray := ComObjCreate("Scripting.Dictionary")
;Sort, vText, CS D, ;add this line to sort the list
Loop, Parse, vText, % ","
if oArray.Exists("" A_LoopField)
oArray.Item("" A_LoopField)++
else
oArray.Item("" A_LoopField) := 1
;list all items
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey in oArray
vOutput .= oArray.Item(vKey) "`t" vKey "`r`n"
MsgBox, % vOutput
;list items that appear n times
vNum := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey in oArray
if (oArray.Item(vKey) = vNum)
vOutput .= oArray.Item(vKey) "`t" vKey "`r`n"
MsgBox, % vOutput
oArray := ""
return
;==================================================
;REMOVE DUPLICATES (CASE INSENSITIVE) (MAINTAIN ORDER/SORT)
;q:: ;remove duplicates (case insensitive)
vText := "f,e,d,C,B,A,a,b,c,A,B,C"
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
oArray := {}
StrReplace(vText, ",",, vCount)
oArray.SetCapacity(vCount+1)
;Sort, vText, CS ;add this line to sort the list
Loop, Parse, vText, % ","
if !oArray.HasKey("z" A_LoopField)
oArray["z" A_LoopField] := 1, vOutput .= A_LoopField "`r`n"
MsgBox, % vOutput
oArray := ""
return
;==================================================
;REMOVE DUPLICATES (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
;w:: ;remove duplicates (case sensitive)
vText := "f,e,d,C,B,A,a,b,c,A,B,C"
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
oArray := ComObjCreate("Scripting.Dictionary")
;Sort, vText, CS D, ;add this line to sort the list
Loop, Parse, vText, % ","
if !oArray.Exists("" A_LoopField)
oArray.Item("" A_LoopField) := 1, vOutput .= A_LoopField "`r`n"
MsgBox, % vOutput
oArray := ""
return
;==================================================
;REFERENCE COUNT
;get ref count (get reference count) (undocumented)
oArray := ["a", "b", "c"]
oArray2 := oArray
vCountRef := NumGet(&oArray + A_PtrSize)
MsgBox, % vCountRef
;get ref count (get reference count) (preferred)
oArray := ["a", "b", "c"]
oArray2 := oArray
vCountRef := NumGet(&oArray + A_PtrSize)
ObjAddRef(&oArray)
vCountRef := ObjRelease(&oArray)
MsgBox, % vCountRef
;==================================================
;KEY COUNT
;get key count (undocumented)
;For a normal AHK array to get its key count:
oArray := ["a", "b", "c"]
vCount := NumGet(&oArray + 4*A_PtrSize)
MsgBox, % vCount
;from:
;ObjCount() or ObjLength() or ObjLen() - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=3950&p=21579#p21578
;get key count (preferred)
;the .Count() method and ObjCount() functions have since been made available
oArray := ["a", "b", "c"]
MsgBox, % oArray.Count()
MsgBox, % ObjCount(oArray)
;get key count (also)
oArray := ["a", "b", "c"]
vCount := 0
for vKey, vValue in oArray
vCount := A_Index
MsgBox, % vCount
;==================================================
[2018-02-27] FREQUENCY COUNT (CASE INSENSITIVE) (SORT)
[2018-02-27] FREQUENCY COUNT (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
[2018-02-27] REMOVE DUPLICATES (CASE INSENSITIVE) (MAINTAIN ORDER/SORT)
[2018-02-27] REMOVE DUPLICATES (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
[2018-03-25] INI TO ARRAY (HANDY TRICK)
[2018-03-28] INI TO ARRAY
[2018-07-27] ARRAY STARTING AT 0/1/OTHER
[2018-08-13] REFERENCE COUNT
[2018-08-13] KEY COUNT
[2018-08-25] PUSH KEYS / ASSIGN MULTIPLE KEYS
[2018-09-18] FOR LOOP EXAMPLES
[2018-09-20] FREQUENCY COUNT (CASE INSENSITIVE) (MAINTAIN ORDER)
==================================================
2D ARRAY: REMOVE BLANK ROWS/COLUMNS
[2D Array] How to remove zero cols ? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 02#p203102
==================================================
INTERFACES: LINKS / GET METHOD LIST / USE METHODS DIRECTLY
iTypeInfo - Get info from TypeAttr Struct - Enum Com Members - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7103 ... ntry450288
ComObj iTypInfo (jethrow, Lexikos, SKAN, Maul-Esel...) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=15194
COM interface tutorial
https://maul-esel.github.io/tutorials/C ... faces.html
[advanced] Using raw COM interfaces in AHK - Tutorials - AutoHotkey Community
https://autohotkey.com/board/topic/8107 ... es-in-ahk/
[ITaskbarList example]
DllCall
https://autohotkey.com/docs/commands/DllCall.htm
[IProvideClassInfo example]
ComObjQuery()
https://autohotkey.com/docs/commands/ComObjQuery.htm
[IColumnManager and IFolderView2 examples]
Explorer column interaction (get/set: which appear, width, ascending/descending order) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=33129
[INetworkListManager example]
[COM] How to enumerate IEnumNetworkConnections in a more "native" way? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=15316
INTERFACES: GET METHOD LIST
E.g. to get the method list for IColumnManager or IFolderView2:
IColumnManager interface (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
IFolderView2 interface (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
- install Visual Studio (perhaps 7GB required) (or obtain the .h (header) files separately)
- go to: C:\Program Files (x86)\Windows Kits\8.1
- search .h files for the interface name (e.g. IColumnManager) and/or a method (e.g. GetColumnCount)
- e.g. it found one file: C:\Program Files (x86)\Windows Kits\8.1\Include\um\ShObjIdl.h
- search within that file for: struct IColumnManager
- e.g. it found: typedef struct IColumnManagerVtbl
- get the lines containing method names (e.g. I grabbed lines containing: STDMETHODCALLTYPE)
0 HRESULT ( STDMETHODCALLTYPE *QueryInterface )(
1 ULONG ( STDMETHODCALLTYPE *AddRef )(
2 ULONG ( STDMETHODCALLTYPE *Release )(
3 HRESULT ( STDMETHODCALLTYPE *SetColumnInfo )(
4 HRESULT ( STDMETHODCALLTYPE *GetColumnInfo )(
5 HRESULT ( STDMETHODCALLTYPE *GetColumnCount )(
6 HRESULT ( STDMETHODCALLTYPE *GetColumns )(
7 HRESULT ( STDMETHODCALLTYPE *SetColumns )(
E.g. for the nth method (0-based index), you use n*A_PtrSize e.g.:
DllCall(NumGet(NumGet(icm+0)+5*A_PtrSize), Ptr,icm, UInt,vFlags, UIntP,vCountCol) ;GetColumnCount
Note: the first parameter is a pointer to the interface, in this case IColumnManager, which is not mentioned in the link below:
IColumnManager::GetColumnCount method (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Last edited by jeeswg on 20 Sep 2018, 05:17, edited 21 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: jeeswg's objects tutorial (not yet complete)
Hi jeeswg. I think there is a lot of good stuff here, both in terms of links and the example of usage/syntax in the second post. Thanks for making the effort.

Spoiler
Cheers.Re: jeeswg's objects tutorial (not yet complete)
Yes! I appreciate the content presented by jeeswg, but the formatting makes it confusing an unpleasant to read. Please don't be offended.Helgef wrote:![]()
Tip on forum formatting, for links, you can specify the url inside the square brackets, eg,link text. It would remove some noise. I'm sure you write a script for correcting it![]()
Also, instead of [ header text ] maybe use bold or tags. There is also
[/c] for making horisontal lines, saves you the =...=.

Re: jeeswg's objects tutorial
Thanks I just read the entire example code.
Its very nice that you list tons of possibilities that we can see side by side and on top of each other.
It helps to understand what works and define slowly what is our own prefered style and logic.
Objects took me more than 6 months - starting to learn scripting from scratch before I was able to use them.
Now with more capacities I have read this and was able to make some things clearer in the whole picture of how to use objects.
Thank you
Its very nice that you list tons of possibilities that we can see side by side and on top of each other.
It helps to understand what works and define slowly what is our own prefered style and logic.
Objects took me more than 6 months - starting to learn scripting from scratch before I was able to use them.
Now with more capacities I have read this and was able to make some things clearer in the whole picture of how to use objects.
Thank you
Who is online
Users browsing this forum: No registered users and 4 guests