FindText - Capture screen image into text and then find it

Post your working scripts, libraries and tools for AHK v1.1 and older
PipelinerUSA
Posts: 2
Joined: 27 Dec 2019, 10:55

Re: FindText - Capture screen image into text and then find it

27 Dec 2019, 11:01

Hello and happy holidays. Admittedly I am not an AHK veteran but I cannot seen to get this to work after reading all of the tutorials and every page of this thread. The popup window that activates the script and asks the user to select the text/image to search for works fine, but after "test" runs, it does not move my cursor to the image it found. The youtube video that is linked from this thread shows that the mouse should automatically move to the found image/text, but my mouse stays stationary and there is a popup that says the test was a success. I have also tried copying the resultant text and saving it as my own script on my desktop and running it, and once again it will successfully find the image but nothing else happens. What am I doing wrong? Thanks in advance
thebbandit
Posts: 45
Joined: 02 Jul 2019, 11:34

Re: FindText - Capture screen image into text and then find it

27 Dec 2019, 14:57

PipelinerUSA wrote:
27 Dec 2019, 11:01
Hello and happy holidays. Admittedly I am not an AHK veteran but I cannot seen to get this to work after reading all of the tutorials and every page of this thread. The popup window that activates the script and asks the user to select the text/image to search for works fine, but after "test" runs, it does not move my cursor to the image it found. The youtube video that is linked from this thread shows that the mouse should automatically move to the found image/text, but my mouse stays stationary and there is a popup that says the test was a success. I have also tried copying the resultant text and saving it as my own script on my desktop and running it, and once again it will successfully find the image but nothing else happens. What am I doing wrong? Thanks in advance
There is one change you need to make in order for it to move your mouse, and that is to "uncomment" the line that does that.

Comments work in AHK by essentially telling the compiler that they should skip everything in a line that is after the ; character

Code: Select all

  ; Click, %X%, %Y%
so you can see that this line is commented out when you first make a test script. If you want it to move the mouse and click on the found string then uncomment this. Or replace that line with this one:

Code: Select all

  MouseMove, X, Y
but it will always show the red and blue boxes when the string is found because of the lines after the messagebox that pops up.

Code: Select all

for i,v in ok
  if (i<=5)
    MouseTip(ok[i].x, ok[i].y)
that is what is putting the box around those found items.

if you do not get a confirmation that your string is found, you need to adjust the search parameters of your captured image.
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

01 Jan 2020, 08:09

Hello, feiyue
Hello, everyone
I am a new ahk coder, only learn very very little. Thank you ,feiyue. I make a script use your gift code. Your gift code run very well when itself.But copy in my script when find pic it run not a massage about find.I copy it right.Why ?Why ?Why ? If the coordinate reason ? I use CoordMode, Mouse , Client . My QQ:10903008 I need help.
itadakimas
Posts: 3
Joined: 01 Jan 2020, 16:33

Re: FindText - Capture screen image into text and then find it

01 Jan 2020, 16:45

Hello,

I been using the code and incorporating it in other scripts. I understand it for the most part. I' am playing around with a macro for a game, learning this before using it for work. I am using control click and the window bind feature to run it in the background in order to still be able to perform other task on my pc. The issue I am having is the coordinates are based upon the screen rather than the specified window id.

My question is can I specify the findtext() to capture the x%X% y%Y% from the X:=ok.1.x, Y:=ok.1.y, be based on the bdm_id coordinates rather than the screens coordinates?

Code: Select all

revive()
{
	Text:="|<Revive>*126$49.VzzvzzzzUTzxzzzynjzzzzzz9r2NPADV039BhYbym3UoqK3wNArsP3TsAaNgRlawqHa7CtsS34"
	if (ok:=FindText(576-150000, 452-150000, 576+150000, 452+150000, 0, 0, Text))
	{
		X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
		ControlClick, x%X% y%Y%, ahk_id %bdm_id%,, L, 1, NA
		for i,v in ok
			if (i<=2)
				MouseTip(ok[i].x, ok[i].y)
		sleep 10000	;10 second delay to account for loading screen
		return_farming()
	}
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: FindText - Capture screen image into text and then find it

01 Jan 2020, 20:26

It's easy to change the screen coordinates to the window coordinates
by subtracting the upper left corner coordinates of the window. :D

Code: Select all

X:=ok.1.x, Y:=ok.1.y
WinGetPos, winx, winy,,, ahk_id %bdm_id%
X:=X-winx, Y:=Y-winy
ControlClick, x%X% y%Y%, ahk_id %bdm_id%,, L, 1, NA
If you want to convert screen coordinates to Client coordinates
relative to the target window, you can use ScreenToClient() function: :beer:

Code: Select all


F1::
CoordMode, Mouse, Screen
MouseGetPos, x1, y1
CoordMode, Mouse, Client
MouseGetPos, x2, y2
ScreenToClient(x1, y1, WinExist("A"), x3, y3)
ClientToScreen(x3, y3, WinExist("A"), x4, y4)
MsgBox, 4096,, Screen Pos:`t%x1%  %y1%`nClient Pos:`t%x2%  %y2%`n
  . `nClient Pos2:`t%x3%  %y3%`nScreen Pos2:`t%x4%  %y4%
return

ScreenToClient(x1, y1, id, ByRef x2, ByRef y2)
{
  VarSetCapacity(pt,8)
  NumPut(x1, pt,0,"int"), NumPut(y1, pt,4,"int")
  DllCall("ScreenToClient", "ptr",id, "ptr",&pt)
  x2:=NumGet(pt,0,"int"), y2:=NumGet(pt,4,"int")
}

ClientToScreen(x1, y1, id, ByRef x2, ByRef y2)
{
  VarSetCapacity(pt,8)
  NumPut(x1, pt,0,"int"), NumPut(y1, pt,4,"int")
  DllCall("ClientToScreen", "ptr",id, "ptr",&pt)
  x2:=NumGet(pt,0,"int"), y2:=NumGet(pt,4,"int")
}

itadakimas
Posts: 3
Joined: 01 Jan 2020, 16:33

Re: FindText - Capture screen image into text and then find it

01 Jan 2020, 21:58

Thanks @feiyue! That did the trick, appreciate the quick response. Still new to AHK and didn't think fo the two approaches you mentioned!

Your script has been a lifesaver, way more accurate than the image search feature.
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 07:52

Can you tell me what is wrong?The game is a window not a full screen.

Code: Select all

WinWait, game,,10
if ErrorLevel ;
{
   MsgBox, WinWait timed out.
   return
} 
else ;
Sleep 10000
#Include <FindText>
t1:=A_TickCount

Text:="|<>[email protected]$57.0000000220A000M00E017w7z00PDURw0zk01vC6lU16y03PUuT0Chk0xsB6A1Zi0DC38hUCbU3P0lZc1aQ03U1yh09nU06C0pc1wy0

TzsAh0wyQ7g0310C7Xk180APU0UD0Ms1aA04006301UU0U03UM0000400014"

if (ok:=FindText(538-150000, 666-150000, 538+150000, 666+150000, 0, 0, Text))
{
  CoordMode, Mouse
  X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
  Click, %X%, %Y%
}
for i,v in ok
  if (i<=2)
    MouseTip(ok[i].x, ok[i].y)
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 11:40

When used in the game, the text color does not change much, but the background image often changes,
So you can set the text tolerance to 0.1, and the background tolerance to 1 (make background transparent),
Then try to get the minimum search range. The following range is the whole range of the window:

Code: Select all

#Include <FindText>

WinWait, game,, 10  ; 逗号错误
if ErrorLevel ;
{
   MsgBox, WinWait timed out.
   return
} 
else ;
   game_id:=WinExist()

Sleep 10000

t1:=A_TickCount

Text:="|<>[email protected]$57.0000000220A000M00E017w7z00PDURw0zk01vC6lU16y03PUuT0Chk0xsB6A1Zi0DC38hUCbU3P0lZc1aQ03U1yh09nU06C0pc1wy0TzsAh0wyQ7g0310C7Xk180APU0UD0Ms1aA04006301UU0U03UM0000400014"

WinGetPos, wx, wy, ww, wh, ahk_id %game_id%
if (ok:=FindText(wx, wy, wx+ww, wy+wh, 0.1, 1, Text))
{
  CoordMode, Mouse
  X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
  Click, %X%, %Y%
}
for i,v in ok
  if (i<=2)
    MouseTip(ok[i].x, ok[i].y)

Last edited by feiyue on 02 Jan 2020, 20:04, edited 2 times in total.
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 11:55

谢谢了,飞跃!I am happy for meet you in the deep night! Let me try ! See you tomorow .
PipelinerUSA
Posts: 2
Joined: 27 Dec 2019, 10:55

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 15:44

thebbandit wrote:
27 Dec 2019, 14:57
PipelinerUSA wrote:
27 Dec 2019, 11:01
Hello and happy holidays. Admittedly I am not an AHK veteran but I cannot seen to get this to work after reading all of the tutorials and every page of this thread. The popup window that activates the script and asks the user to select the text/image to search for works fine, but after "test" runs, it does not move my cursor to the image it found. The youtube video that is linked from this thread shows that the mouse should automatically move to the found image/text, but my mouse stays stationary and there is a popup that says the test was a success. I have also tried copying the resultant text and saving it as my own script on my desktop and running it, and once again it will successfully find the image but nothing else happens. What am I doing wrong? Thanks in advance
There is one change you need to make in order for it to move your mouse, and that is to "uncomment" the line that does that.

Comments work in AHK by essentially telling the compiler that they should skip everything in a line that is after the ; character

Code: Select all

  ; Click, %X%, %Y%
so you can see that this line is commented out when you first make a test script. If you want it to move the mouse and click on the found string then uncomment this. Or replace that line with this one:

Code: Select all

  MouseMove, X, Y
but it will always show the red and blue boxes when the string is found because of the lines after the messagebox that pops up.

Code: Select all

for i,v in ok
  if (i<=5)
    MouseTip(ok[i].x, ok[i].y)
that is what is putting the box around those found items.

if you do not get a confirmation that your string is found, you need to adjust the search parameters of your captured image.
That was simple! Thanks!
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 23:11

feiyue wrote:
02 Jan 2020, 11:40
When used in the game, the text color does not change much, but the background image often changes,
So you can set the text tolerance to 0.1, and the background tolerance to 1 (make background transparent),
Then try to get the minimum search range. The following range is the whole range of the window:

Code: Select all

#Include <FindText>

WinWait, game,, 10  ; 逗号错误
if ErrorLevel ;
{
   MsgBox, WinWait timed out.
   return
} 
else ;
   game_id:=WinExist()

Sleep 10000

t1:=A_TickCount

Text:="|<>[email protected]$57.0000000220A000M00E017w7z00PDURw0zk01vC6lU16y03PUuT0Chk0xsB6A1Zi0DC38hUCbU3P0lZc1aQ03U1yh09nU06C0pc1wy0TzsAh0wyQ7g0310C7Xk180APU0UD0Ms1aA04006301UU0U03UM0000400014"

WinGetPos, wx, wy, ww, wh, ahk_id %game_id%
if (ok:=FindText(wx, wy, wx+ww, wy+wh, 0.1, 1, Text))
{
  CoordMode, Mouse
  X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
  Click, %X%, %Y%
}
for i,v in ok
  if (i<=2)
    MouseTip(ok[i].x, ok[i].y)

真是逗号的问题,太感谢你了,飞跃!
如果我想判断找这个图,如果找到了,点击这个图,如果没找到再循环找10次,如果还没找到,继续执行下面的代码。麻烦你帮我写一下好吗?谢谢
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

02 Jan 2020, 23:19

@feiyue
就是逗号错了,谢谢了!Thank you !
Last edited by xoabc on 08 Jan 2020, 05:51, edited 1 time in total.
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: FindText - Capture screen image into text and then find it

03 Jan 2020, 10:41

@xoabc You are welcome. :D
In the main English forum, please use English for communication, this is respect for most people.
You can go to the Chinese sub forum for help, it can be communicated in Chinese.

Your requirements are too simple, I'm not interested in writing scripts for you.
You can learn about the commands from help files, such as Loop and Break.
As long as you study for a while, you don't need to ask these simple questions.
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

03 Jan 2020, 20:56

I'll be careful, thank you for reminding me. I didn't use English because I know my English is poor. I think it's time to learn AHK.Thank you for your help and advice. feiyue
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

08 Jan 2020, 05:49

@feiyue
Can you help me?
I have a problem.when I like this, I was been warn.
"Warning in #include file "C:\Program Files\FindText.ahk":This variable has not been assigned a value.
Specifically:oldzw (a static variable)
Specifically:hBW (a static variable)
Specifically:ppvBits (a local variable)
Specifically:id(a static variable)"
I don't understand it.What should I do?

Code: Select all

#Include <FindText>
login(num1,num2)

login(zh1,zh2)
{
run, game
WinWait, game,,10
if ErrorLevel ;
{
   MsgBox, WinWait timed out.
   return
} 
else
{
t1:=A_TickCount

Text:="|<>[email protected]$57.0000000220A000M00E017w7z00PDURw0zk01vC6lU16y03PUuT0Chk0xsB6A1Zi0DC38hUCbU3P0lZc1aQ03U1yh09nU06C0pc1wy0TzsAh0wyQ7g0310C7Xk180APU0UD0Ms1aA04006301UU0U03UM0000400014"

if (ok:=FindText(538-1500, 666-1500, 538+1500, 666+1500, 0, 0, Text))
{
  CoordMode, Mouse
  X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
  Click, %X%, %Y%
  Sleep 300
  Click, 626,84 ;
}
for i,v in ok
  if (i<=2)
    MouseTip(ok[i].x, ok[i].y)
}
thebbandit
Posts: 45
Joined: 02 Jul 2019, 11:34

Re: FindText - Capture screen image into text and then find it

10 Jan 2020, 18:48

@xoabc
This in including a library file, and you may not have followed the instruction to place the file into a library folder. Make the file and place it into Documents\Autohotkey\Lib\FindText.ahk
xoabc
Posts: 9
Joined: 01 Jan 2020, 07:51

Re: FindText - Capture screen image into text and then find it

12 Jan 2020, 20:24

@thebbanditThanks for your help.

It took me some time to find the answer to the question. This is more difficult for beginners. Forgive me for not copying the code completely. Missed the "#warn" on the head. Just delete it. The program runs very well.
ahk nooblie
Posts: 6
Joined: 19 Jan 2020, 00:49

Re: FindText - Capture screen image into text and then find it

19 Jan 2020, 03:50

Hello Feiyue, amazing script, the uses are tremendous.

I have a question about your script, how would you go about adding a random coordinate to the image the script finds? Like clicking in different parts of the image every time it finds it? Sorry if my question is too basic.

If anyone could pitch in, I'd welcome the input.

Code: Select all

#MaxThreadsPerHotKey 3
#NoEnv
SetDefaultMouseSpeed, 0


F2::
Toggle := !Toggle
Loop
{

	Text1:="|<>*160$58.D6k20A0001WP080k000ABU0U30000U6lmMDVltm0P9f0n8a8c1hUs34WMWU6q3UAHxXz3PM90l86869gWa34WMWD6lm8AFtVu"
	if (ok:=FindText(818,625,973,730, 0, 0, Text1))
	CoordMode, Mouse
	X1:=ok.1.x, Y1:=ok.1.y, Comment:=ok.1.id
	Click, %X1%, %Y1%

	Text2:="|<>*182$59.Tzzzzzzzzx0000000006000000000A000000000M000000000k3y0000001U6A00400k30AA00801U60MMswtszUA0lX9AWNa0M1y4P10mA0k3ATnmDYM1U6AE1Yn8k30A8nl9aFU60MMwwRwVUA000000000M000000000k000000001U0000000030000000005zzzzzzzzzo"

	if (ok:=FindText(810,548,958,607, 0, 0, Text2))
	CoordMode, Mouse
	X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	Click, %X%, %Y%
	
	If (!Toggle)
		Break
}
Return
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: FindText - Capture screen image into text and then find it

19 Jan 2020, 05:40

@ahk nooblie I think you can do this:

Code: Select all

#MaxThreadsPerHotKey 3
#NoEnv
SetDefaultMouseSpeed, 0


F2::
Toggle := !Toggle
Loop
{

	Text1:="|<>*160$58.D6k20A0001WP080k000ABU0U30000U6lmMDVltm0P9f0n8a8c1hUs34WMWU6q3UAHxXz3PM90l86869gWa34WMWD6lm8AFtVu"
	if (ok:=FindText(818,625,973,730, 0, 0, Text1))
	CoordMode, Mouse
	X:=ok.1.1, Y:=ok.1.2, w:=ok.1.3, h:=ok.1.4
	Random, i, 0, w
	Random, j, 0, h
	Click, % X+i, % Y+j

	Text2:="|<>*182$59.Tzzzzzzzzx0000000006000000000A000000000M000000000k3y0000001U6A00400k30AA00801U60MMswtszUA0lX9AWNa0M1y4P10mA0k3ATnmDYM1U6AE1Yn8k30A8nl9aFU60MMwwRwVUA000000000M000000000k000000001U0000000030000000005zzzzzzzzzo"

	if (ok:=FindText(810,548,958,607, 0, 0, Text2))
	CoordMode, Mouse
	X:=ok.1.1, Y:=ok.1.2, w:=ok.1.3, h:=ok.1.4
	Random, i, 0, w
	Random, j, 0, h
	Click, % X+i, % Y+j
	
	If (!Toggle)
		Break
}
Return
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: FindText - Capture screen image into text and then find it

19 Jan 2020, 05:49

Updated to 7.4 version - 2020/01/20
.... 1. Add: the GetTextFromScreen() function and ft_Gui("Capture_NoGui") is added.
....... So you can quickly generate the final code without GUI interface. :dance:

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 127 guests