cycle4passion

IE COM Stuff

Apr 22nd, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; https://autohotkey.com/board/topic/52685-winactivate-on-a-specific-ie-browser-tab/
  2. ; https://autohotkey.com/boards/viewtopic.php?f=6&t=30527
  3.  
  4. ; not all of these work
  5.  
  6.  
  7. IEGoOld(URL, TabName := "", show := True, timeout :=10,extrawait :=0,traytiptext := "")
  8. {
  9.     /*
  10. wb.FullScreen := True
  11. wb.AddressBar := False
  12. wb.ToolBar := False
  13. wb.MenuBar := False
  14. wb.StatusBar := False
  15. wb.StatusText := ""
  16. wb.Visible := true
  17. wb.LocationURL
  18. wb.Navigate(URL)
  19. wb.Left
  20. wb.Top
  21. wb.Height
  22. wb.Width
  23. wb.HWND
  24. wb.Busy
  25. wb.Fullscreen := true
  26. wb.GoBack
  27. wb.GoForward
  28. wb.GoHome
  29. wb.GoSearch
  30. wb.Navigate("URL")
  31. wb.Quit
  32. wb.Refresh
  33. wb.Stop
  34. */
  35.   AccOn := true
  36.   if  traytiptext
  37.     Traytip("","Connecting to " . traytiptext)
  38.   verbose := true
  39.     ;ComObjError(ShowComErrors)
  40.     if verbose { ; clear
  41.     oSciTE := ComObjActive("SciTE4AHK.Application")
  42.     oSciTe.Message(0x111,420) ; Clear
  43.   }
  44.    if (ACCOn and Tabname)  {  ; Search for pre-existing ie tab
  45.       For wb in ComObjCreate("Shell.Application").Windows  {
  46.           if verbose and InStr(wb.FullName, "iexplore.exe"), out("Checking (" . A_Index . ") '" . TabName . "' in '" .  wb.LocationName . "' Tab = " . InStr(wb.LocationName,TabName))        
  47.          If  Instr(wb.LocationName,TabName) and InStr(wb.FullName, "iexplore.exe") {
  48.             window:=0, client:=-4 ; constants for ie 11, varies on other versions
  49.             ControlGet hwnd, hwnd, , DirectUIHWND2, % "ahk_class IEFrame ahk_id " wb.hwnd ; get control hwnd
  50.             if not hwnd
  51.               ControlGet hwnd, hwnd, , DirectUIHWND3, % "ahk_class IEFrame ahk_id " wb.hwnd ; get control hwnd
  52.             if verbose, out("`tHandle for " . TabName . " @ " . hwnd)
  53.             client := Acc_ObjectFromWindow(hwnd, client) ;note - the client object should be the 4th child of the window
  54.             page_tab_list := Acc_Children(client)[1]
  55.             for each, child in Acc_Children(page_tab_list)  {
  56.               if verbose, out("`t`tLooking @ '" . TabName . "' in '" . child.accName(0) . "' : " .  InStr(child.accName(0),TabName) )
  57.               if InStr(child.accName(0),TabName) {
  58.                 if verbose, out("`t`t`tActivating " . child.accName(0))
  59.                 child.accDoDefaultAction(0) ; Selects correct tab
  60.                 Winactivate, % wb.LocationName
  61.                 return wb
  62.               }
  63.             }
  64.          }
  65.       }
  66.    }
  67.    ; No ie tab found, so create it
  68.    wb := ComObjCreate("InternetExplorer.Application")
  69. /*    if  not show {
  70.     wb.top := 0
  71.     wb.left := 0
  72.     wb.width := 0
  73.     wb.height := 0
  74.   }
  75.   */
  76.   if  (show = "transparent") ; transparent needed to allow ACC interaction, made small for less eye catching
  77.       WinSet, Transparent, 0, A
  78.   else  if (show) {
  79.     wb.Visible := true
  80.     WinMaximize, A
  81.   } else
  82.       wb.Visible := false
  83.    if verbose, out("Creating New '" . TabName . "'")
  84.    if (URL and wb.LocationURL != URL) {
  85.       wb.Navigate(URL)
  86.       IEWaitReady(wb,timeout,extrawait)
  87.    }
  88.    return wb
  89. }
  90.  
  91.  
  92. IEGet(Name="")
  93. {
  94.  
  95.    IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
  96.       Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs"
  97.       : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer.*" )
  98.    For Pwb in ComObjCreate( "Shell.Application" ).Windows
  99.       If ( Pwb.LocationName = Name ) && InStr( Pwb.FullName, "iexplore.exe" )
  100.          Return Pwb
  101. }
  102.  
  103.  
  104. IEGet2(name="") {
  105.     for obj in ComObjCreate("Shell.Application").Windows
  106.         If !obj.AddressBar && (obj.document.title==RegExReplace(name,".*\K\h-.*"))
  107.             break
  108.  
  109.     return obj
  110. }
  111.  
  112. TabWinID(tabName) {
  113.    WinGet, winList, List, ahk_class IEFrame
  114.    While, winList%A_Index% {
  115.       n:=A_Index, ErrorLevel:=0
  116.       While, !ErrorLevel {
  117.          ControlGetText, tabText, TabWindowClass%A_Index%, % "ahk_id" winList%n%
  118.          if InStr(tabText, tabName)
  119.             return, winList%n%
  120.       }
  121.    }
  122. }
  123.  
  124. TabActivate(hwnd, tabName) {
  125.    ControlGet, hTabUI , hWnd,, DirectUIHWND1, ahk_id %hwnd%
  126.    Acc := Acc_ObjectFromWindow(hTabUI) ;// access "Tabs" control
  127.    If (Acc.accChildCount > 1) ;// more than 1 tab
  128.       tabs := Acc.accChild(3) ;// access just "IE document tabs"
  129.    While (tabs.accChildCount >= A_Index) {
  130.       tab := tabs.accChild(A_Index)
  131.       If (tab.accName(0) = tabName)  ;// test vs. "tabName"
  132.          return tab.accDoDefaultAction(0) ;// invoke tab
  133.    }
  134. }
  135.  
  136. IESelTab(tabName) {
  137.    WinGet, winList, List, ahk_class IEFrame
  138.             WinActivate, ahk_class IEFrame
  139.             Send, ^1
  140.    While, winList%A_Index% {
  141.       n:=A_Index, ErrorLevel:=0
  142.       While, !ErrorLevel {
  143.          ControlGetText, tabText, TabWindowClass%A_Index%, % "ahk_id" winList%n%
  144.          if InStr(tabText, tabName) {
  145.             ;WinActivate, ahk_class IEFrame
  146.             ;Send, ^%A_Index%
  147.        
  148.     return
  149.         }
  150.       }
  151.    }
  152. }
  153.  
  154. ; AutoHotkey_L:
  155.  
  156. WBGet(WinTitle="ahk_class IEFrame", Svr#=1) { ; based on ComObjQuery docs
  157.     static  msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
  158.     ,   IID := "{0002DF05-0000-0000-C000-000000000046}" ; IID_IWebBrowserApp
  159. ;   ,   IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWindow2
  160.     SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
  161.     if (ErrorLevel != "FAIL") {
  162.         lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
  163.         if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
  164.             DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
  165.             return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
  166.         }
  167.     }
  168. }
  169.  
  170.  
  171.  
  172. ; AHK Basic:
  173.  
  174. WBGet(WinTitle="ahk_class IEFrame", Svr#=1) ; based on Sean's GetWebBrowser function
  175. {
  176.     static msg, IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IWebBrowserApp
  177.     if Not msg
  178.         msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
  179.     SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
  180.     if (ErrorLevel != "FAIL") {
  181.         lResult:=ErrorLevel, GUID:=COM_GUID4String(IID_IHTMLDocument2,"{332C4425-26CB-11D0-B483-00C04FD90119}")
  182.         DllCall("oleacc\ObjectFromLresult", "Uint",lResult, "Uint",GUID, "int",0, "UintP",pdoc)
  183.         return COM_QueryService(pdoc,IID,IID), COM_Release(pdoc)
  184.     }
  185. }
  186.  
  187.  
  188. IELoad(Pwb)  
  189. {
  190. Loop
  191.     Sleep, 50
  192. Until   (pwb.readyState=4 && pwb.document.readyState="complete" && !pwb.busy)
  193. }
  194.  
  195. IE_GetTabCount(hWnd)
  196. {
  197. loop {  
  198.     ControlGet, pHwnd, Hwnd,, DirectUIHWND%A_Index%, ahk_id %Hwnd%
  199.     phwnd?cHwnd:=pHwnd
  200.      }  until !phwnd
  201. oAcc := Acc_Get("Object", "4.1", 0, "ahk_id " cHwnd)
  202. for each, oChild in Acc_Children(oAcc)
  203. {
  204.     vTabText := oChild.accName(0)
  205.     if (vTabText == "New tab (Ctrl+T)")
  206.         vCount++
  207. }
  208. oAcc := oChild := ""
  209. return vCount
  210. }
  211.  
  212. ;==================================================
  213.  
  214. IE_GetTabNames(hWnd, vSep="`n")
  215. {
  216. loop {  
  217.     ControlGet, pHwnd, Hwnd,, DirectUIHWND%A_Index%, ahk_id %Hwnd%
  218.     phwnd?cHwnd:=pHwnd
  219.      }  until !phwnd
  220. oAcc := Acc_Get("Object", "4.1", 0, "ahk_id " cHwnd)
  221. vOutput := ""
  222. for each, oChild in Acc_Children(oAcc)
  223. {
  224.     vTabText := oChild.accName(0)
  225.     if !(vTabText == "New tab (Ctrl+T)")
  226.         vOutput .= vTabText vSep
  227. }
  228. ;~ vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
  229. oAcc := oChild := ""
  230. return vOutput
  231. }
  232.  
  233. ;==================================================
  234.  
  235. IE_FocusTabByNum(hWnd, vNum)
  236. {
  237. loop {  
  238.     ControlGet, pHwnd, Hwnd,, DirectUIHWND%A_Index%, ahk_id %Hwnd%
  239.     phwnd?cHwnd:=pHwnd
  240.      }  until !phwnd
  241. oAcc := Acc_Get("Object", "4.1", 0, "ahk_id " cHwnd)
  242. if !Acc_Children(oAcc)[vNum]
  243.     vNum := ""
  244. else
  245.     Acc_Children(oAcc)[vNum].accDoDefaultAction(0)
  246. oAcc := ""
  247. WinActivate, ahk_id %hWnd%
  248. return vNum
  249. }
  250.  
  251. ;==================================================
  252.  
  253. IE_FocusTabByName(hWnd, vTitle, vNum=1)
  254. {
  255. loop {  
  256.     ControlGet, pHwnd, Hwnd,, DirectUIHWND%A_Index%, ahk_id %Hwnd%
  257.     phwnd?cHwnd:=pHwnd
  258.      }  until !phwnd
  259. oAcc := Acc_Get("Object", "4.1", 0, "ahk_id " cHwnd)
  260. vCount := 0, vRet := 0
  261. for each, oChild in Acc_Children(oAcc)
  262. {
  263.     vTabText := oChild.accName(0)
  264.     if (vTabText = vTitle)
  265.         vCount++
  266.     if (vCount = vNum)
  267.     {
  268.         oChild.accDoDefaultAction(0), vRet := A_Index
  269.         break
  270.     }
  271. }
  272. oAcc := oChild := ""
  273. WinActivate, ahk_id %hWnd%
  274. return vRet
  275. }
  276.  
  277. ;==================================================
Advertisement