Use Variables/Parameters to pass Methods or Properties in COM w/AutoHotkey

I had wanted to be able to make a function where I pass Methods or Properties in COM.  (this makes it much more flexible and eases updating / maintaining of code.)  Unfortunately it was not as straight-forward as I’d hoped.

Thankfully maestrith, Author of AutoHotkey Studio, had already solved this problem and shared the solution with me!  The code which shows how to pass Methods or Properties in COM is listed below.  While in the video I demonstrate connecting to IE and perform web scraping, the solution is equally applicable to any COM object like Excel, etc.

pwb := WBGet()
Property:="OuterHTML"

Get_ID(pwb,"site-description","OuterHTML")

Get_ID(PXL,ID,Property){
ID_Var:= PXL.document.GetElementByID(ID)
MsgBox % ID_Var[Property]
}
;~  MsgBox % pwb.document.GetElementByID("site-description").OuterHTML
;~  MsgBox % pwb.document.GetElementByID("site-description").InnerHTML

return


pwb := WBGet()
MsgBox % WebScraping_Get(pwb,"class","widget_tag_cloud","0","")
MsgBox % WebScraping_Get(pwb,"id","tag_cloud-3",,"oh")

;***********Getting data from page*******************
WebScraping_Get(PXL,Method,Thing,Index="",Property="OuterHTML"){
;***********Set Methods and Properties to static so do not have to rebuild each time it is called*******************
static Methods:={id:"getElementByID",class:"getElementsByClassName",tag:"getElementsByTagName",name:"GetElementsByName"}
static Properties:={it:"InnerTEXT",ot:"OuterText",ih:"InnerHTML",oh:"OuterHTML",vl:"Value",si:"selectedIndex",ck:"checked"}
Doc:= PXL.document
if (method !="id")
      return Doc[Methods[Method]](Thing)[Index][Properties[Property]] ;Arrays
else
      return Doc[Methods[Method]](Thing)[Properties[Property]] ;Not an Array
}
return

Demonstration Video to pass Methods or Properties in COM

Comments are closed.