?something had talked about doing, finally got around to.
slightly tested, may need some bug work.
essentially allows 'dropping' in visual basic objects and such, without having to use VB to manipulate them.
syntax basically:
Code:
1. make sure you call com_CoInitialize()
2. name the object HANDLE with '#_' prefix as in:
#_objWord := com_CreateObject("Word.Application")
3. Use 'VB like syntax' as you please.. ex:
#_objDoc := ez_invoke("objWord.Documents.Open", Master_File)
4. for assignments use "=":
ez_invoke("objWord.Visible=","True")
5. To reference sub items use "[]". (in VB "()" is used). ex:
mmtextfield3 := ez_invoke("objDoc.MailMerge.Fields[3].code.Text")
}} Kind of neat {{
6. To reference other objects prefix with "+" before object descriptor.
ez_invoke("ObjectX.+ObjectY")
i think that covers it, mostly.
additions:
A) Can use ' or "" for sub items as in
ez_invoke("object.property[""Whatever is X""]") or ez_invoke("obj.prop['this is "OK" too'].method[3]")
B) Can use =+ for assigning one object with value of another's pointer as in ez_invoke("this.object =+ secondobject.property.method", with,these,params, optional)
oh,
7. reinvoke() to rebuild reference table. For example, if you've opened a new doc, "document.page(1)" points to old and needs a reinvoke.
So, for example: if #_objDoc gets a new handle (ie. new document), need:
ez_reinvoke("objDoc.MailMerge.Fields[3].code.Text")
first time you access. (after that use regular ez_invoke() to take advantage of chache --> ie. faster!).... need to reinvoke based on right-most object/property/method. (ie. if you've reinvoked objDoc...Fields[4] you still need to reinvoke objDoc Fields[3] and objdoc..Fields[4].code.text first time you access them. ) Suggested to just use new object handle when object or key branch changes and just use invoke. eg: #_objDoc2, then #_objDoc3, etc...
UPDATED: ez_reinvoke@(skip#, "Object.xx.xxx")
ie. to reinvoke at the 4th object, would use:
ez_reinvoke@(4,"object.o1.o2.o3.o4.o5")
it will also RELEASE that object *IF* you set Ez_Extra_Safe_Revoke global var to 1 or (2 for release *all* objects in chain).
8. copy and place in EZ.ahk in LIB folder.
9. NEEDS SEAN'S COM.AHK !!!!!!
note: object handle is called #_Object.. but inside the ez_invoke you just call it Object ie. drop the #_ for reference inside of ez_invoke.
complete handle tree is returnedas : #_Object#_Inner_object#_Property#_etc is returned so you can directly access any individual handle... etc.
updated to allow ["Whatever is here"],
such as ez_invoke("objVoice.GetVoices[""Name=Microsoft Mary""].Item", 0 ) and =+ assignments, and ' inplace of " if you desire.
but reserves a var per invoke so if the "..." changes often or much, best to use regular ez_invoke to set another var to value, and then use that.
anyhow
newest June 2008
ver
Code:
ez_revoke(object, skip = 1, xtra_safe = 0) { ;ez_revoke("object.this.that.whatever.x",3) ==> starts w/ object #4
global
static illegal := "[^\w#@$?\[\]]"
local Released
local myobject, position, resul, result, resultisQ, resulted, resultedQ, resultedQS, assem
;skip--
;if skip < 1
; skip = 1
if Ez_Extra_Safe_Revoke ;; Global Var Ez_Extra_Safe_Revoke to override globally
xtra_safe = 1
myobject := "#_" . regexreplace(object, "\.", "#_", parts)
position = 1
loop {
if !(position := regexmatch(myobject, "\#_(?<t>\w+)(?<tisQ>\=)?(?:\[(?:(?<ted>\w+)|""(?<tedQ>[^""]*)""|'(?<tedQS>[^']*)')\])?", resul, position)
+ strlen(resul) )
break
assem .= regexreplace(resul,illegal,"?")
;;debug("assem inside revoke is " assem)
;debug("resul " resul "`nassem " assem)
;Xbug("assem is " assem )
;debug(a_index "vs skip at " skip)
if (a_index <= skip)
continue
if !Released and (%assem% > 0) { ; (a_index = 1) and
if Xtra_safe or (a_index = 1) ; ie. release only if object unless extra_safe mode..
com_Release(%assem%)
if !Xtra_safe
Released = 1
; @%assem% = 1
}
;debug(a_index "-->" assem "==" %assem%)
%assem% =
}
}
ez_release(object) {
if !(object := regexreplace(object,"\..*$"))
return
Full_object = #_%object%
if !(ObjectP := %Full_Object%)
return
com_Invoke(ObjectP, "Quit")
return com_release(ObjectP)
}
;ez_invoker & _reinvoker are SUPPOSED to release as they build--> am not sure is working or note... needs careful test!!!!
ez_reinvoke@( skip , object , arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe" ) {
ez_revoke(object,skip)
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA )
}
ez_reinvoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe" ) {
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,1 )
}
ez_reinvoker( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe" ) {
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,-1 )
}
ez_invoker( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe" ) {
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,-2 )
}
ez_invoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe", revoke = "" ) {
;debug(object "--: " arg1)
if instr(object,"=+") and regexmatch(object,"^\s*(?<object>.+?)\s*=\+\s*(?<param>.+)$",_)
return revoke ? ez_reinvoke(_object "=", "+" ez_invoke(_param, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,revoke) ) : ez_invoke(_object "=", "+" ez_invoke(_param, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8, arg9,argA,revoke ) )
;
if !( (instr(object,".+") ? regexmatch(object,"^\s*(?<object>.+?)\.(?<obj>\+)(?<param>.+?)\s*$",_) : regexmatch(object,"^\s*(?<object>.+?)((?<param>\w+=?)(?:\[(?<dualparam>\w+|""[^""]*""|'[^']*')\])?(?<dparamQ>=)?"
;;(?<param>\w+|\[(?:\w+|""[^""]*"")\]=?"
;; (?<param>\w+(?:\[(?<dualparam>\w+|""[^""]*"")\])?=?"
; . "(?:\."
; . "(?<param>"
; . "(?:\w+"
; . "(?:\["
; . "(?:""[^""]*""|\w+)"
; . "\])?"
; . "(?:\.\w+)?"
; . ")+"
; . "[=]?)"
. ")?\s*$",_) ))
;regexmatch(object,"^\s*(?<object>.+?)\.(?<obj>\+)?(?<param>[\w=]+)\s*$",_) )
return
;debug("object is" _object "`nparam is " _param)
/*
if ! object or !( pos := instr(object,".",0,0))
return
param := substr(object,pos + 1)
object := substr(object,1, pos - 1)
*/
if ( _object != _object + 0 )
_object := ez_invoke_findhandle(_object,revoke)
if _obj {
if ( _param != _param + 0 )
_param := ez_invoke_findhandle( _param , revoke) ; "#_" .
_param := "+" . _param
}
; if (A_FormatInteger = "H") {
; ;swapback = 1
; SetFormat, Integer, D
; tmp := Com_Invoke(_object, _param , arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
; setFormat, Integer, H
; return tmp
; } else
return (_param = "") ? Com_Invoke(_object, arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA) : (_dualparam = "") ? Com_Invoke(_object, _param , arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA) : Com_Invoke(_object, _param . _dparamQ,regexreplace( _dualparam,"^['""]|['""]$"), arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
}
ez_invoke_findhandle(object,revoke ="") {
global
static illegal := "[^\w#@$?\[\]]"
; static idCounter = 0
local myobject, handle, parts, resul, result, resulted, resultisQ, resultedQ, resultedQS,position, assem, value, Ovalue,OOvalue, assem_old, revoker ; , mystart,the_Obj,the_,the_rest
;if !regexmatch(myobject,"^(?<obj>\w+)(?<rest>.*)$",the_)
; return
;the_obj := "#_" . the_obj
myobject := "#_" . regexreplace(object, "\.", "#_", parts)
;myobject := "#_" . %the_obj% . regexreplace(the_rest, "\.", "#_", parts)
;Xbug( "my object is " myobject "--> " %myobject%)
if revoke {
if (revoke < 0) {
revoke += 2
revoker = 1
;debug("revoker is " revoker)
}
; idCounter++
}
;debug( (@%myobject%))
if !(revoke or parts or regexmatch(myobject, illegal) ) and ((handle := %myobject%) > 0 ) ; and !(@%myobject%) ; or (strlen(myobject) > 254)
return handle
;hmmm
; if !Revoke && !parts && !regexmatch(myobject, illegal) && ((handle := %myobject%) > 0 )
; if !(revoke or parts or regexmatch(myobject, illegal) ) and ((handle := %Smyobject%) > 0 )
; if !(revoke or parts or regexmatch(myobject, illegal) ) and (Smyobject := regexreplace(myobject,illegal,"?")) and ((handle := %Smyobject%) > 0 ) ; or (strlen(myobject) > 254)
position = 1
loop {
if !(position := regexmatch(myobject, "\#_(?<t>\w+)(?<tisQ>\=)?(?:\[(?:(?<ted>\w+)|""(?<tedQ>[^""]*)""|'(?<tedQS>[^']*)')\])?", resul, position)
+ strlen(resul) )
break
assem .= regexreplace(resul,illegal,"?")
;debug("resul " resul "`nassem " assem)
;Xbug("assem is " assem )
if (value := %assem%) and (!revoke or (a_index = 1) ) and (Ovalue := value) { ; (strlen(assem) < 254) and; (revoke or @%assem%)
;Xbug( "value = assem ==> " value)
continue
}
if !Ovalue
return
/*
; this.that[3].#[rr].
if !Ovalue {
if (a_index = 1) {
value := substr(assem,3)
if (Ovalue := %value%) > 0
continue
}
return
}
*/
;debug(" result is" result " and resulted is" resulted . resultedQ "`n ovalue is" Ovalue )
if revoke
assem_old := %assem%
if revoker
OOvalue := Ovalue
;Xbug("going to set Ovalue with current ovalue = " ovalue " and result at " result)
@%assem% =
%assem% := Ovalue := (resulted .= resultedQ . resultedQS) ? com_invoke(Ovalue, result . resultisQ, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result)
if revoker {
if (assem_old > 0) and (assem_old <> Ovalue) ; and (a_index > 1)
com_release(assem_old)
;debug(" assem old" assem_old)
if (OOValue > 0) and (OOValue <> Ovalue) and (a_index > 2)
com_release(OOvalue)
;debug("OOvalue " OOvalue)
}
;debug("after revoker and a index is" a_index)
;%assem% := Ovalue := (resulted .= resultedQ . resultedQS) ? (result = "_") ? com_invoke(Ovalue, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result . resultisQ, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result)
;debug( resulted ? "Ovalue, result, resulted, " Ovalue "," result "," resulted : "")
;debug("assem" assem "`nresulted ? " resulted "`nOvalue , result" Ovalue "," result)
;Xbug("set assem on Ovalue <<-" Ovalue)
}
handle := %assem%
;debug(handle "is result")
return handle
}
; -------------------------------------------------------------------
Com_RegisterCallback(FunctionName, Options = "", ParamCount = "", EventInfo = "") {
global VProtectOFF
address := (EventInfo = "") ? (ParamCount = "") ? (Options = "") ? RegisterCallback(FunctionName) : RegisterCallback(FunctionName,Options) : RegisterCallback(FunctionName,Options, ParamCount) : RegisterCallback(FunctionName,Options, ParamCount, EventInfo)
;if (Address := RegisterCallback(FunctionName, Options , ParamCount , EventInfo ) ) and VProtect
if !VProtectOFF
DllCall("VirtualProtect", "uint", Address, "uint", 8, "uint", 0x40, "uint*", 0)
return Address
}
new ver
Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EZ_INVOKE() ;;;;;;;;;;;;;;;;;;;;;;;;
;
;
; for z = xxx.xx[3] // z[3] use xxx.xx[3].item[3]//
;
ez_reinvoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe" ) {
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,1 )
}
ez_invoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe",argA="vT_NoNe", revoke = "" ) {
;debug(object "--: " arg1)
if instr(object,"=+") and regexmatch(object,"^\s*(?<object>.+?)\s*=\+\s*(?<param>.+)$",_)
return revoke ? ez_reinvoke(_object "=", "+" ez_invoke(_param, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA,revoke) ) : ez_invoke(_object "=", "+" ez_invoke(_param, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8, arg9,argA,revoke ) )
;
if !( (instr(object,".+") ? regexmatch(object,"^\s*(?<object>.+?)\.(?<obj>\+)(?<param>.+?)\s*$",_) : regexmatch(object,"^\s*(?<object>.+?)((?<param>\w+=?)(?:\[(?<dualparam>\w+|""[^""]*""|'[^']*')\])?(?<dparamQ>=)?"
;;(?<param>\w+|\[(?:\w+|""[^""]*"")\]=?"
;; (?<param>\w+(?:\[(?<dualparam>\w+|""[^""]*"")\])?=?"
; . "(?:\."
; . "(?<param>"
; . "(?:\w+"
; . "(?:\["
; . "(?:""[^""]*""|\w+)"
; . "\])?"
; . "(?:\.\w+)?"
; . ")+"
; . "[=]?)"
. ")?\s*$",_) ))
;regexmatch(object,"^\s*(?<object>.+?)\.(?<obj>\+)?(?<param>[\w=]+)\s*$",_) )
return
;debug("object is" _object "`nparam is " _param)
/*
if ! object or !( pos := instr(object,".",0,0))
return
param := substr(object,pos + 1)
object := substr(object,1, pos - 1)
*/
if ( _object != _object + 0 )
_object := ez_invoke_findhandle(_object,revoke)
if _obj {
if ( _param != _param + 0 )
_param := ez_invoke_findhandle( _param , revoke) ; "#_" .
_param := "+" . _param
}
; if (A_FormatInteger = "H") {
; ;swapback = 1
; SetFormat, Integer, D
; tmp := Com_Invoke(_object, _param , arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
; setFormat, Integer, H
; return tmp
; } else
return (_param = "") ? Com_Invoke(_object, arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA) : (_dualparam = "") ? Com_Invoke(_object, _param , arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9,argA) : Com_Invoke(_object, _param . _dparamQ,regexreplace( _dualparam,"^['""]|['""]$"), arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
}
ez_invoke_findhandle(object,revoke ="") {
global
static illegal := "[^\w#@$?\[\]]"
local myobject, handle, parts, resul, result, resulted, resultisQ, resultedQ, resultedQS,position, assem, value, Ovalue
myobject := "#_" . regexreplace(object, "\.", "#_", parts)
;Xbug( "my object is " myobject "--> " %myobject%)
if !(revoke or parts or regexmatch(myobject, illegal) ) and ((handle := %myobject%) > 0 ) ; or (strlen(myobject) > 254)
return handle
;hmmm localize SmyObject :>
; if !Revoke && !parts && !regexmatch(myobject, illegal) && ((handle := %myobject%) > 0 )
; if !(revoke or parts or regexmatch(myobject, illegal) ) and ((handle := %Smyobject%) > 0 )
; if !(revoke or parts or regexmatch(myobject, illegal) ) and (Smyobject := regexreplace(myobject,illegal,"?")) and ((handle := %Smyobject%) > 0 ) ; or (strlen(myobject) > 254)
position = 1
loop {
if !(position := regexmatch(myobject, "\#_(?<t>\w+)(?<tisQ>\=)?(?:\[(?:(?<ted>\w+)|""(?<tedQ>[^""]*)""|'(?<tedQS>[^']*)')\])?", resul, position)
+ strlen(resul) )
break
assem .= regexreplace(resul,illegal,"?")
;debug("resul " resul "`nassem " assem)
;Xbug("assem is " assem )
if (value := %assem%) and (!revoke or (a_index = 1) ) and (Ovalue := value) { ; (strlen(assem) < 254) and
;Xbug( "value = assem ==> " value)
continue
}
if !Ovalue
return
/*
; this.that[3].#[rr].
if !Ovalue {
if (a_index = 1) {
value := substr(assem,3)
if (Ovalue := %value%) > 0
continue
}
return
}
*/
;debug(" result is" result " and resulted is" resulted . resultedQ "`n ovalue is" Ovalue )
;Xbug("going to set Ovalue with current ovalue = " ovalue " and result at " result)
%assem% := Ovalue := (resulted .= resultedQ . resultedQS) ? com_invoke(Ovalue, result . resultisQ, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result)
;%assem% := Ovalue := (resulted .= resultedQ . resultedQS) ? (result = "_") ? com_invoke(Ovalue, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result . resultisQ, regexreplace(resulted,"\Q#_\E",".")) : com_invoke(Ovalue, result)
;debug( resulted ? "Ovalue, result, resulted, " Ovalue "," result "," resulted : "")
;debug("assem" assem "`nresulted ? " resulted "`nOvalue , result" Ovalue "," result)
;Xbug("set assem on Ovalue <<-" Ovalue)
}
handle := %assem%
return handle
}
; ---------------
old ver
Code:
ez_reinvoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe" ) {
return ez_invoke( object, arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,1 )
}
ez_invoke( object, arg1="vT_NoNe",arg2="vT_NoNe",arg3="vT_NoNe",arg4="vT_NoNe",arg5="vT_NoNe",arg6="vT_NoNe",arg7="vT_NoNe",arg8="vT_NoNe",arg9="vT_NoNe", revoke = "" ) {
if !object or ! ((instr(object,"+") ? regexmatch(object,"^\s*(?<object>[^+]+?)\.(?<obj>\+)(?<param>[\w=.]+)\s*$",_) : regexmatch(object,"^\s*(?<object>.+?)\.(?<param>[\w=]+)\s*$",_) ) )
;regexmatch(object,"^\s*(?<object>.+?)\.(?<obj>\+)?(?<param>[\w=]+)\s*$",_) )
return
/*
if ! object or !( pos := instr(object,".",0,0))
return
param := substr(object,pos + 1)
object := substr(object,1, pos - 1)
*/
if ( _object != _object + 0 )
_object := ez_invoke_findhandle(_object,revoke)
if _obj {
if ( _param != _param + 0 )
_param := ez_invoke_findhandle("#_" . _param , revoke)
_param := "+" . _param
}
return Com_Invoke(_object, _param , arg1 ,arg2 ,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
}
ez_invoke_findhandle(object,revoke ="") {
global
local myobject, handle, parts, resul, result, resulted, position, assem, value, Ovalue
myobject := "#_" . regexreplace(object, "\.", "#_", parts)
;Xbug( "my object is " myobject "--> " %myobject%)
if ( (handle := %myobject%) > 0 ) and (!revoke or !parts)
return handle
position = 1
loop {
if !(position := regexmatch(myobject, "\#_(?<t>\w+)(?:\[(?<ted>\w+)\])?", resul, position)
+ strlen(resul) )
break
assem .= resul
;Xbug("assem is " assem )
if (value := %assem%) and (!revoke or (a_index = 1) ) and (Ovalue := value) {
;Xbug( "value = assem ==> " value)
continue
}
if !Ovalue
return
/*
if !Ovalue {
if (a_index = 1) {
value := substr(assem,3)
if (Ovalue := %value%) > 0
continue
}
return
}
*/
;Xbug("going to set Ovalue with current ovalue = " ovalue " and result at " result)
%assem% := Ovalue := resulted ? com_invoke(Ovalue, result, resulted) : com_invoke(Ovalue, result)
;Xbug("set assem on Ovalue <<-" Ovalue)
}
handle := %assem%
return handle
}
; ©2007. You can use this code only for your personal use, or if you publish your script source on this forum.
; If you think this is a cool function, keep in mind it's just a wrapper for Sean's *very* cool invoke()
example:
Code:
Speak(text, rate = "", voice# = "") {
global
static objVoice
if !objVoice {
com_init()
objVoice := #_objVoice := com_CreateObject("SAPI.SpVoice")
}
if rate !=
ez_invoke("objVoice.rate=",rate)
if voice# !=
ez_invoke("objVoice.Voice =+ objVoice.GetVoices[Name].Item",voice#) ; This number controls voice
;ez_invoke("objVoice.Voice=", "+" ez_invoke("objVoice.GetVoices[Name].Item",voice#)) ; This number controls voice
if text !=
ez_invoke("objVoice.Speak", text)
return
}