Image recognition scripts work separately but not together
Posted: 12 May 2024, 21:04
Hi.
I did some extremely long back and forth with ChatGPT, trying to make this image recognition script working. Both of the scripts work fine on their own but when combined into one file, Script 2 doesn't seem to make the mouse cursor go to where the image is on the screen. Instead it goes to the same X and Y location of the Script 1's image location on the screen. I am not sure why it is, as I am still new at this thing and my brain can't function anymore trying to explain this to a robot.
I think it said something about some variables being the same in both scripts, hence making the conflict and not acquiring the right coordinates for Script 2, but ever since I tried to making it fix the scripts by combining them into one, it did nothing but create new errors. So I ran out of sanity and I hope that some human experts can actually help. Please someone help me out. Thanks again.
I did some extremely long back and forth with ChatGPT, trying to make this image recognition script working. Both of the scripts work fine on their own but when combined into one file, Script 2 doesn't seem to make the mouse cursor go to where the image is on the screen. Instead it goes to the same X and Y location of the Script 1's image location on the screen. I am not sure why it is, as I am still new at this thing and my brain can't function anymore trying to explain this to a robot.
I think it said something about some variables being the same in both scripts, hence making the conflict and not acquiring the right coordinates for Script 2, but ever since I tried to making it fix the scripts by combining them into one, it did nothing but create new errors. So I ran out of sanity and I hope that some human experts can actually help. Please someone help me out. Thanks again.
Code: Select all
; Script 1:
CoordMode, Pixel, Screen ; Set coordinate mode to screen pixels
; Define the path to the image you want to search for
ImagePath := "C:\COLOR.png"
; Define the offset for adjusting the mouse cursor position
OffsetX := -35
OffsetY := 15
; Define a hotkey (in this case, Control+I) to trigger the image recognition script
+Æ::
{
; Check if the window with class "ahk_class OWL.Dock" exists
If !WinExist("ahk_class OWL.Dock")
{
; Add a 100ms delay
Sleep, 100
; Press F4 key
SendInput, {F4}
; Add another 100ms delay
Sleep, 100
}
; Search for the image on the screen
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
; If an error occurs during image search
If ErrorLevel
{
MsgBox, Image search failed with ErrorLevel: %ErrorLevel%
}
else
{
; Calculate the center coordinates of the found image with offset
ImageWidth := ErrorLevel ; Width of the found image
ImageHeight := ErrorLevel ; Height of the found image
ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
; Move the mouse cursor to the adjusted center of the found image
MouseMove, ImageCenterX, ImageCenterY
; Add a short delay before clicking
Sleep, 100 ; Delay for 100 milliseconds
; Left click on the image
Click
; Add another 100ms delay
Sleep, 100
; Get the current mouse position (position1)
MouseGetPos, posX1, posY1
; Add another 100ms delay
Sleep, 100
; Calculate the new position (position2)
posX2 := posX1 + 490
posY2 := posY1 + 100
; Move the mouse cursor to the new position (position2)
MouseMove, posX2, posY2
; Left click
Click
}
return
}
Code: Select all
; Script 2
CoordMode, Pixel, Screen ; Set coordinate mode to screen pixels
; Define the path to the image you want to search for
ImagePath := "C:\ROTATE.png"
; Define the offset for adjusting the mouse cursor position
OffsetX := 25
OffsetY := 25
; Define a hotkey (in this case, Control+I) to trigger the image recognition script
F10::
{
; Search for the image on the screen
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
; If an error occurs during image search
If ErrorLevel
{
MsgBox, Image search failed with ErrorLevel: %ErrorLevel%
}
else
{
; Calculate the center coordinates of the found image with offset
ImageWidth := ErrorLevel ; Width of the found image
ImageHeight := ErrorLevel ; Height of the found image
ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
; Move the mouse cursor to the adjusted center of the found image
MouseMove, ImageCenterX, ImageCenterY
; Add a short delay before clicking
Sleep, 15 ; Delay for 15 milliseconds
; Left click on the image
Click, 2
; Debug
; MsgBox, Image clicked at X:%ImageCenterX% Y:%ImageCenterY%
}
}
return