Click if an image is visible Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
diegg1739
Posts: 110
Joined: 17 Oct 2023, 18:29

Click if an image is visible

24 May 2024, 11:05

Hello, I made the following script that should work as follows: If a small image is visible in the toolbar, in the Paint.net window, then by typing Alt Gr + O, AHK clicks on the toolbar and displays a menu. If the image is not visible, then typing Alt Gr + O should do nothing.

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance
Persistent
#HotIf WinActive('ahk_exe paintdotnet.exe')
<^>!o::	{
	found:= ImageSearch(&OutputVarX, &OutputVarY, 70, 70, 100, 100, "F:\Documents\AutoHotkey\Imágenes ImageSearch AHK\Herramienta Formas Paint.net.png")
	if (found)
	{
	click 170, 90
	mouseMove 220,400
	}
	}
#HotIf
The problem is that the script does not work. It's wrong? Thanks for any help you can give me to make it work!
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Click if an image is visible

24 May 2024, 11:10

Hello,

"Does not work" does not explain what your script does when you run it. Is your hotkey ever triggered? If so, how do you know? What does the script actually do?

What value does the search return?

Are your coordinates relative to the active window's client area? If not, adjust, or use CoordMode to change the point of reference.

Testing an ImageSearch does not require 13 lines. You can do it in three lines. This will speed your coding and debugging.

viewtopic.php?f=76&t=128216&p=566260#p566260

#HotIf is not necessary to determine whether your script works. Debugging is easier without it.

Code: Select all

#Requires AutoHotkey v2.0
image := 'test.png'
x1 := 70
y1 := 70
x2 := 100
y2 := 100
found := ImageSearch(&x, &y, x1, y1, x2, y2, image)
MouseMove x1, y1
MsgBox 'Title:`n' WinGetTitle('A') '`n`nfound = ' found, 'Result', 'Iconi'
diegg1739
Posts: 110
Joined: 17 Oct 2023, 18:29

Re: Click if an image is visible

24 May 2024, 12:36

Thanks for your response, Mikeyww. Regarding my script, these are the answers to your questions:
1) When I run the script, it does nothing. Pressing AltGr + or nothing happens, whether the icon is visible or not visible.
The result I expect is that, when the icon is visible, when I press AltGr + o, the script will click on the toolbar to display a menu.
2) The search does not return any results.
3) The coordinates are relative to the client area of the active window.

Regarding your script, a window appears that says: Result:
Title: Autohotkey - Tablacus Explorer
found = 0.

So I added the image address in your script

Code: Select all

#Requires AutoHotkey v2.0
WinActive('ahk_exe paintdotnet.exe')
image := 'F:\Documents\AutoHotkey\Imágenes ImageSearch AHK\Herramienta Formas Paint.net.png'
x1 := 70
y1 := 70
x2 := 100
y2 := 100
found := ImageSearch(&x, &y, x1, y1, x2, y2, image)
MouseMove x1, y1
MsgBox 'Title:`n' WinGetTitle('A') '`n`nfound = ' found, 'Result', 'Iconi'
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Click if an image is visible

24 May 2024, 13:42

So you added the image. What happened next?
diegg1739
Posts: 110
Joined: 17 Oct 2023, 18:29

Re: Click if an image is visible

24 May 2024, 14:09

When you run the script, the mouse pointer is displayed in the correct position, although this occurs over the active File Explorer window. Additionally, a small window is displayed that says: Result: Title: Downloads - Tablacus Explorer, Found = 0.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Click if an image is visible  Topic is solved

24 May 2024, 15:20

I would start by capturing a simple static image from your screen as a PNG file, and then using that in your testing to see if ImageSearch works. If it fails, then it typically means that the image is not visible or that the coordinates are wrong. If using default coordinates, then the point of reference will be the client area of whichever window happens to be the active window at the time.
diegg1739
Posts: 110
Joined: 17 Oct 2023, 18:29

Re: Click if an image is visible

25 May 2024, 08:46

Indeed, mikeyww. There was an error in the coordinates and, for that reason, ImageSearch was not finding the icon.
This is the script that works as I wanted:

Code: Select all

#HotIf WinActive('ahk_exe paintdotnet.exe')
image := 'F:\Documents\AutoHotkey\Imagenes-ImageSearch-AHK\Herramienta-Formas-Paint.net.png'
x1 := 60
y1 := 60
x2 := 110
y2 := 110
<^>!o::	{
found := ImageSearch(&x, &y, x1, y1, x2, y2, image)
	if (found)
	{
	click 170, 90
	mouseMove 220,400
	}
	}
#HotIf
Thank you very much for your help! :thumbup:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: wineguy and 59 guests