I need help working with AHK with chrome

Get help with using AutoHotkey and its commands and hotkeys
crazydeveloper
Posts: 3
Joined: 09 Sep 2019, 14:57

I need help working with AHK with chrome

09 Sep 2019, 15:03

I have one spare server online so I wanted to play with it, but the problem is that I don't understand that much this autohotkey,

I wanted to open some pages in chrome and click and add some text and stuff, but it seems to work in chrome you need to install some other features like selenium or chrome.ahk, so I spended some hours looking into those things, and to be honest I'm really confusing (in installation part and that chrome has to be debug mode for that chrome.ahk), so what do you suggest I should focus working in chrome something that will be easier for my brain.
User avatar
boiler
Posts: 6630
Joined: 21 Dec 2014, 02:44

Re: I need help working with AHK with chrome

09 Sep 2019, 16:46

There aren't always easy ways to do things. If you want to automate Chrome, you have to decide whether you are determined enough to learn one of the approaches you mentioned and go through the tutorials that are available until you learn how to do it. If you think that's not something you want to take on, you might consider paying someone to write some code for you.
Getfree
Posts: 219
Joined: 12 Oct 2014, 18:00

Re: I need help working with AHK with chrome

09 Sep 2019, 17:51

Depending on what you want to do, it might be much easier if you use a combination of AutoHotkey scripts and Chrome extensions.

For example, there's an extension called AutoControl Shortcut Manager which can define global shortcuts (i.e. shortcuts that work even if Chrome is not the active window).
I use that extension to define several global shortcuts for the actions I want to perform (that extension in particular can do a lot of stuff) and then I use an AHK script to send the keystrokes for those shortcuts. That way, I can make chrome do things even if it's in the background.
I can open and close tabs and windows, select them, pin them, navigate to pages, create and open bookmarks, etc.

And all that with almost no coding at all, just a few send commands in an AHK script and that's it.
teadrinker
Posts: 2056
Joined: 29 Mar 2015, 09:41
Contact:

Re: I need help working with AHK with chrome

09 Sep 2019, 18:08

Sometimes a combination of IAccessible and Send command can be useful. :)

Code: Select all

SetBatchLines, -1
Return

$F1::
Run, Chrome.exe "https://www.autohotkey.com/boards/viewtopic.php?f=76&t=67878"
WinWait, ahk_class Chrome_WidgetWin_1
SendMessage, WM_GETOBJECT := 0x3D, 0, 1, Chrome_RenderWidgetHostHWND1  ; enable accessible interface
Sleep, 1000

accChrome := AccObjectFromWindow( WinExist() )                         ; get accessible object from Chrome
if !IsObject(accChrome) {
   MsgBox, Failed to get the accessible object from Chrome
   ExitApp
}

Loop {                                                                 ; search "Post Reply" button
   Sleep, 1000
   elem := SearchElement(accChrome, {Name: "Post Reply", Role: ROLE_SYSTEM_LINK := 0x1E})
} until elem || A_Index = 5
if !IsObject(elem) {
   MsgBox, Failed to find "Post Reply" button
   Return
}
elem.accDoDefaultAction(0)                                             ; push "Post Reply" button

Loop {                                                                 ; wait until the page is loaded and "Select code" element appears
   Sleep, 1000
   elem := SearchElement(accChrome, {Name: "Select code", Value: "Select code"})
} until elem || A_Index = 5
if !IsObject(elem) {
   MsgBox, Failed to find "Select code" element
   Return
}
Send {Tab}{Tab}                                                        ; navigate to the code box
SendInput, {Text}It's my new post!                                     ; send the text
Return

SearchElement(parentElement, params)
{
   found := true
   for k, v in params {
      try (parentElement["acc" . k](0) != v && found := false)
      catch 
         found := false
   } until !found
   if found
      Return parentElement
   
   for k, v in AccChildren(parentElement)
      if obj := SearchElement(v, params)
         Return obj
}

AccObjectFromWindow(hWnd, idObject = 0)
{
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", Str, "oleacc", Ptr)
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", Str, idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, Ptr, &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", Ptr, hWnd, UInt, idObject, Ptr, &IID, PtrP, pAcc) = 0
      Return ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
}

AccChildren(Acc)  {
   Loop 1  {
      if ComObjType(Acc, "Name") != "IAccessible"  {
         error := "Invalid IAccessible Object"
         break
      }
      cChildren := Acc.accChildCount, Children := []
      VarSetCapacity(varChildren, cChildren*(8 + A_PtrSize*2), 0)
      if DllCall("oleacc\AccessibleChildren", Ptr, ComObjValue(Acc), Int, 0, Int, cChildren, Ptr, &varChildren, IntP, cChildren) != 0  {
         error := "AccessibleChildren DllCall Failed"
         break
      }
      Loop % cChildren  {
         i := (A_Index - 1)*(A_PtrSize*2 + 8) + 8
         child := NumGet(varChildren, i)
         Children.Insert( NumGet(varChildren, i - 8) = 9 ? AccQuery(child) : child )
         ( NumGet(varChildren, i - 8 ) = 9 && ObjRelease(child) )
      }
   }
   if error
      ErrorLevel := error
   else
      Return Children.MaxIndex() ? Children : ""
}

AccQuery(Acc)  {
   static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
   try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 04:52

Instead of sending

Code: Select all

Send {Tab}{Tab}                                                        ; navigate to the code box
SendInput, {Text}It's my new post!                                     ; send the text
You can search for element in the post of reply and set value to it.
teadrinker
Posts: 2056
Joined: 29 Mar 2015, 09:41
Contact:

Re: I need help working with AHK with chrome

10 Sep 2019, 11:46

malcev wrote: You can search for element in the post of reply
Did you try it? ;)
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 11:54

Code: Select all

Loop {                                                                 ; search "Post Reply" button
   Sleep, 1000
   elem := SearchElement(accChrome, {Name: "", Role: 42, DefaultAction: "activate"})
} until elem || A_Index = 5
if !IsObject(elem) {
   MsgBox, Failed to find "Post Reply" button
   Return
}
elem.accValue(0) := "test"
teadrinker
Posts: 2056
Joined: 29 Mar 2015, 09:41
Contact:

Re: I need help working with AHK with chrome

10 Sep 2019, 12:17

Does it work for you? For me it doesn't.
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 12:30

For me it works.
Win10. Chrome - Version 76.0.3809.132 (Official Build) (64-bit).
teadrinker
Posts: 2056
Joined: 29 Mar 2015, 09:41
Contact:

Re: I need help working with AHK with chrome

10 Sep 2019, 12:36

For me it finds the code box element (if I set DefaultAction: "активировать"), but doesn't want to set accValue, I tested on Windows 7.
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 12:41

May be before setting accValue You have to set focus to code box element?
For me it works on Windows 7 too.
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 15:55

Strange.
You can test it with AccExplorer:
https://github.com/blackrosezy/gui-inspect-tool/blob/master/AccExplorer32.exe
Select code box element and press Def Action.
After that State should become focusable, focused.
malcev
Posts: 829
Joined: 12 Aug 2014, 12:37

Re: I need help working with AHK with chrome

10 Sep 2019, 17:32

Run AccExplorer, press "select with mouse" button (3-rd from left), wait for new window with this button and drag this button to needed element.
After that press "Do it" in "Def Action".
Image
You can alo check object for methods and properties - press "check mark" button (4-th from right), but it does not check put_accValue method.
To test put_accValue You have to download inspect.exe.
Last versions with tree view here:
https://github.com/guolaok/Python-UIAutomation-for-Windows/tree/master/inspect
Choose needed element and press Action->SetAccValue.
ars4l4n
Posts: 5
Joined: 26 Aug 2020, 07:27

Re: I need help working with AHK with chrome

26 Aug 2020, 07:41

Like for some others, the script doesn't do anything on my pc.
Using: Windows 10 64 bit Version 10.0.18362 Build 18362,
AHK Version 1.1.29.00 from 2018

I'll try a newer version...

updated to the 1.1.33.02 July 2020 version, still no difference (REEEEEEEEEEEEEEEEEE)
teadrinker
Posts: 2056
Joined: 29 Mar 2015, 09:41
Contact:

Re: I need help working with AHK with chrome

26 Aug 2020, 12:07

Try another approach. :)

Code: Select all

$F10::
   Run, Chrome.exe "https://www.autohotkey.com/boards/viewtopic.php?f=76&t=67878"
   WinWait, ahk_class Chrome_WidgetWin_1, Chrome Legacy Window
   RunJsFromChromeAddressBar( BuildJS("a[title=""Post a reply""]", "elem.click()") )
   Sleep, 500
   RunJsFromChromeAddressBar( BuildJS("#message", "elem.value = 'My new post!'"), true )
   Return
   
BuildJS(selector, action) {
   js = 
   (LTrim
      (() => {
         async function waitForElement(selector) {
            while (!(elem = document.querySelector(selector))) {
               await new Promise(result => setTimeout(result, 500));
            }
            return elem;
         }
         waitForElement('%selector%').then(elem => {
            %action%;
         });
      })();
   )
   Return js
}

RunJsFromChromeAddressBar(js, refresh := false, exe := "chrome.exe") {
   static WM_GETOBJECT := 0x3D
        , ROLE_SYSTEM_TEXT := 0x2A
        , STATE_SYSTEM_FOCUSABLE := 0x100000
        , SELFLAG_TAKEFOCUS := 0x1
        , AccAddrBar
   (refresh && AccAddrBar := AccChrome := "")
   if !AccAddrBar {
      window := "ahk_class Chrome_WidgetWin_1 ahk_exe " . exe
      Loop 100 {
         Loop 200 {
            Sleep, 100
            SendMessage, WM_GETOBJECT, 0, 1, Chrome_RenderWidgetHostHWND1, % window
            AccChrome := AccObjectFromWindow( WinExist(window) )
         } until IsObject(AccChrome)
         if !IsObject(AccChrome)
            throw "Failed to get accessible object from chrome window"
         Loop 5 {
            Sleep, 100
            AccAddrBar := SearchElement(AccChrome, {Role: ROLE_SYSTEM_TEXT, State: STATE_SYSTEM_FOCUSABLE})
         } until AccAddrBar
         if !IsObject(AccAddrBar)
            AccAddrBar := AccChrome := ""
      } until IsObject(AccAddrBar)
      if !IsObject(AccAddrBar)
         throw "Failed to get accessible object from address bar"
   }
   AccAddrBar.accValue(0) := "javascript:" . js
   AccAddrBar.accSelect(SELFLAG_TAKEFOCUS, 0)
   ControlSend,, {Enter}, % window, Chrome Legacy Window
}

SearchElement(parentElement, params)
{
   found := true
   for k, v in params {
      try {
         if (k = "ChildCount")
            (parentElement.accChildCount != v && found := false)
         else if (k = "State")
            (!(parentElement.accState(0) & v) && found := false)
         else
            (parentElement["acc" . k](0) != v && found := false)
      }
      catch 
         found := false
   } until !found
   if found
      Return parentElement
   
   for k, v in AccChildren(parentElement)
      if obj := SearchElement(v, params)
         Return obj
}

AccObjectFromWindow(hWnd, idObject = 0) {
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF
   DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
      Return ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
}

AccChildren(Acc) {
   static VT_DISPATCH := 9
   Loop 1  {
      if ComObjType(Acc, "Name") != "IAccessible"  {
         error := "Invalid IAccessible Object"
         break
      }
      try cChildren := Acc.accChildCount
      catch
         Return ""
      Children := []
      VarSetCapacity(varChildren, cChildren*(8 + A_PtrSize*2), 0)
      res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
                                                , "Int", cChildren, "Ptr", &varChildren, "IntP", cChildren)
      if (res != 0) {
         error := "AccessibleChildren DllCall Failed"
         break
      }
      Loop % cChildren  {
         i := (A_Index - 1)*(A_PtrSize*2 + 8)
         child := NumGet(varChildren, i + 8)
         Children.Push( (b := NumGet(varChildren, i) = VT_DISPATCH) ? AccQuery(child) : child )
         ( b && ObjRelease(child) )
      }
   }
   if error
      ErrorLevel := error
   else
      Return Children.MaxIndex() ? Children : ""
}

AccQuery(Acc) {
   static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
   try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}
User avatar
tadamm
Posts: 21
Joined: 27 Sep 2020, 11:41

Re: I need help working with AHK with chrome

07 Nov 2020, 15:58

ahk to chrome using a simple ext mentioned above.

https www.youtube.com/watch?v=gr4z0Xw8W2g&t=149s Broken Link for safety

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], grada, HiSoKa, mikeyww, Weshuggah and 64 guests