Faster Image Search in Active Window - A wrapper function for FindText

Post your working scripts, libraries and tools
Ben
Posts: 17
Joined: 03 Oct 2016, 07:26

Faster Image Search in Active Window - A wrapper function for FindText

25 Aug 2019, 23:25

feiyue wrote:
14 May 2016, 12:10
FindText is a function similar to the AHK built-in command "ImageSearch",
but it can convert images into strings, written into the script without
saving as an external image, so it is easier to use than "ImageSearch".
https://www.autohotkey.com/boards/viewtopic.php?t=17834
I am really impressed with FindText and prefer it over ImageSearch because of it's flexibility and I no longer have to include image files with my scripts. If you don't know about this function, read the link above or the rest of this won't make sense to you.

tl;dr;

I have 3 monitors an have noticed that FindText was taking a few seconds to search my screen for images. In most cases I only need to search the active window so I created a wrapper function to do this for me. It will perform much faster now that it's range has been limited.
After adding my functions to your script, you should be able to immediately replace your calls to FindText with my new functions, FindTextAW (AW = Active Window) and FindTextAWClick.

Code: Select all

FindTextAW(text,err1:=0,err0:=0)
; FindText only within the active window.  Much faster if you have a large
; desktop or are searching for multiple images.
; Input parameters x,y,w,h are not needed in this version and err1 and err0
; are optional parameters in case they are needed.
; This outputs the same array of x,y,w,h,Comment that the main function returns.
; Ben Sacherich - 7/17/2019
{
    t1:=A_TickCount

    WinGetPos, X, Y, W, H, A  ; "A" to get the active window's position.
	
	;SoundBeep 423, 100	; Uncomment for debugging.
	
    ; Get the coordinates for the center of the window and pass that to the regular FindText function.
    ; This is needed for FindText versions prior to 6.0
    ;W:=W//2
    ;X+=W
    ;H:=H//2
    ;Y+=H
    ;MsgBox, Searching at %X%`, %Y%, %W%`, %H%

    ;if (ok:=FindText(X, Y, W, H, 0, 0, Text,1,0)) ; Take new Screenshot, Find First
    if (ok:=FindText(X, Y, W, H, 0, 0, Text,1,1)) ; Take new Screenshot, Find All
    {
      CoordMode, Mouse
      X:=ok.1.1, Y:=ok.1.2, W:=ok.1.3, H:=ok.1.4, Comment:=ok.1.5, X+=W//2, Y+=H//2
    }
    t1:=A_TickCount-t1

    ; Show a MouseTip on the first two matches. Uncomment for debugging.
    ;for i,v in ok
    ;  if i<=2
    ;    {
    ;    MouseTip(v.1+v.3//2, v.2+v.4//2)
    ;    Comment.=v.5 ", "
    ;    }

    ;if ok	; Uncomment for debugging.
    ;    MsgBox, 4096, % "FindTextAW", % "Time:`t" (t1) " ms`n`n"
    ;        . "Pos:`t" X ", " Y "`n`n"
    ;        . "Found:`t" ok.MaxIndex() "`n`n"
    ;        . "Result:`t" (ok ? "Success!`n`n" Comment : "Failed!"),5
    return, ok.MaxIndex() ? ok:0
}
I made the next function to use in cases where I wanted it to find the image and immediately click on it.

Code: Select all

FindTextAWClick(text,err1:=0,err0:=0)
; FindText in the active window and click on it.
; This relies on FindTextAW().
; Ben Sacherich - 7/17/2019 - This saves me writing code that I was repeating a lot.
{
    if (ok:=FindTextAW(Text))
    {
        CoordMode, Mouse
        X:=ok.1.1, Y:=ok.1.2, W:=ok.1.3, H:=ok.1.4, Comment:=ok.1.5, X+=W//2, Y+=H//2
        Click, %X%, %Y%
        ;MouseMove, X, Y	; Uncomment if you want the mouse to move to the click site.

        SoundBeep 2000, 150	; Let the user know something happened.

        return, 1
    }
}
Example usage below. Note that there are times I need to search for multiple images, or return multiple. These examples do not show that.

Code: Select all

^r::    ; CTRL+r
; If the Microsoft Access Query Designer is open and in Design mode...(rest of algorithm removed)

; Looking for the Design Triangle in the bottom right corner of the window.  It is hard to find this and not get a false positive.
Text:="|<Access Design Mode>[email protected]$55.zzzzzzzzzU0003zzzzk0001zzzzs0000zzzzw0000TzjzS0000DznzL00007zuz7cV003zxj7m6001zy/7u4U00zzT7wWE00TzjLy0U00DzkbzF2007zzzzU0003zy00k2001zzPPM0000zzjzg0000Tzk060000Dzzzz00007zzzzk"
if (ok:=FindTextAW(Text))
{
    SoundBeep, 1000, 150

    ; Access Design Mode detected
    CoordMode, Mouse
    X:=ok.1.1, Y:=ok.1.2, W:=ok.1.3, H:=ok.1.4, Comment:=ok.1.5, X+=W//2, Y+=H//2
    MsgBox, 64, Design Mode detected: %X%`, %Y%`, %Comment%, 4
	
	; Do more stuff after we know Design Mode is active.  No need to click on the icon.
}


+#q::   ; WIN+SHIFT+Q
IfWinExist ahk_class LyncConversationWindowClass  ; Lync 2016 ; meeting window being shared.   It may say "(5 Participants)" in the title.
{
	IfWinNotActive ahk_class LyncConversationWindowClass
	{
		SoundBeep 423, 150
		WinActivate  ; Activates the window found by #IfWin.

		; If the "Hide Stage" button is present, click it. It may be left over from a previous meeting.
		Text:="|<Hide Stage>*156$58.zzzzzzzzzyyrvzsTzzzvvzjzjjzzzjjyzyyzzzyyr3XtkVkss3NBbnjqHNjhikTqsPg6yqvTz/hirvvPAzQiqnDjhUsS68MC7zzzzzzzyzzzzzzzzznzzzzzzzzsTzzzzzzzzzzs"
		FindTextAWClick(Text)
		Return
	}
}
Special thanks to @feiyue for sharing his FindText function with us. :clap:
ricardo231
Posts: 22
Joined: 24 Aug 2019, 16:14

Re: Faster Image Search in Active Window - A wrapper function for FindText

04 Sep 2019, 02:26

Could the tolerance percentage be changed?
Ben
Posts: 17
Joined: 03 Oct 2016, 07:26

Re: Faster Image Search in Active Window - A wrapper function for FindText

15 Sep 2019, 20:26

ricardo231 wrote:
04 Sep 2019, 02:26
Could the tolerance percentage be changed?
Can you elaborate on your request? I didn't mention tolerance or percentage in my post so I'm not sure what you're asking for.
ricardo231
Posts: 22
Joined: 24 Aug 2019, 16:14

Re: Faster Image Search in Active Window - A wrapper function for FindText

24 Sep 2019, 15:27

About this

Code: Select all

FindText(X,Y,W,H,err0,err1,Text)
err1 is the character "0" fault-tolerant in percentage (0-1) Default 0.
err0 is the character "_" fault-tolerant in percentage.(0-1) Default 0.
Ben
Posts: 17
Joined: 03 Oct 2016, 07:26

Re: Faster Image Search in Active Window - A wrapper function for FindText

25 Sep 2019, 15:18

I'm still not sure what you are asking. My wrapper function accepts the err1 and err0 parameters and just passes them on to the the official FindText() function. If you want to pass them in a different order they you would need to adjust the code. There are some parameters that I'm not accepting and passing but you are welcome to add them yourself.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: c7aesa7r, mac2347 and 16 guests