Jump to content

Bit of guidance please!


AZPERRY
 Share

Recommended Posts

Hello there AutoIT users.

I have used Auto it but its been a few years and most my time is working on PLC logic controllers.

Can i ask what is wrong with my code here please?

HotKeySet("{ESC}", "Exit")

include <ImageSearch.au3>


Func MyExit()

    MsgBox(0, "Ending")
    Exit

EndFunc   ;==>MyExit

While 1
    _click()
WEnd


Func _click()

    ConsoleWrite("Starting search" & @CRLF)
    $pump = _ImageSearch('pump.png', 1, $x, $y, 100)

    if $pump True then
        ConsoleWrite("found pump!" & @CRLF)
        MouseClick("left", $pump)
        Sleep(100)
        EndIf
EndFunc

 

Link to comment
Share on other sites

Please post what you expect it to do, what it does when you run your script, if you get any error messages ...

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I am not familiar with either the _ImageSearch function or Include (UDF) file.

However, I recommend a Sleep of at least 100 in you While ... Wend loop, after _click()

So I don't know if you should be providing $x and $y coordinates, or they just get values returned to those variables. Or indeed whether 'pump.png' is meant to be a full path, or what the 100 parameter is for, or for that matter the 1. I am presuming $pump is a returned coordinate.

Like water says, what is expected from your code?

When providing code, you should Quote it and select AutoIt code, like so. Makes it easier for us to work with.

HotKeySet("{ESC}", "Exit")

include <ImageSearch.au3>


Func MyExit()

    MsgBox(0, "Ending")
    Exit

EndFunc   ;==>MyExit

While 1
    _click()
WEnd


Func _click()

    ConsoleWrite("Starting search" & @CRLF)
    $pump = _ImageSearch('pump.png', 1, $x, $y, 100)

    if $pump True then
        ConsoleWrite("found pump!" & @CRLF)
        MouseClick("left", $pump)
        Sleep(100)
        EndIf
EndFunc

 

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Is this your whole script?  Where are the values for $x & $y coming from?

Edit:  Sorry, I must have hit enter at the same time as TheSaint, with the same question.

Edited by TheXman
Link to comment
Share on other sites

Link to comment
Share on other sites

Needs to be

#include <ImageSearch.au3>

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Where did you store the file ImageSearch.au3?
Same directory as your script? User defind include directory?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include "Image_Search.au3"

$x1=0
$y1=0

$pump = _ImageSearch("pump.png",1,$x1,$y1,0)?
if $result=1 Then
    MouseMove($x1,$y1,3)
    Mouseclick("left",$x1,$y1,3)
Else
    MsgBox(0,"Not Found","Image not found...")
EndIf

ok im doing something wrong here as i cant even get this to work.........

$pump = _ImageSearch("pump.png",1,$x1,$y1,0)?
$pump = ^ ERROR
>Exit code: 1    Time: 0.104

 

Link to comment
Share on other sites

14 minutes ago, AZPERRY said:

$pump = _ImageSearch("pump.png",1,$x1,$y1,0)?

Remove the ? at the end of the line. Don't know why, but sometimes this happens when you C&P Code from the forum.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

You are sure your function starts with an underscore?
I searched the forum and found this. If this is the UDF you use then the function name does not start with an "_". If you use another image search library, then please open the UDF and check the name of the function!

; PUBLIC FUNCTIONS
func image_search($sign, $width, $height)
    Local $pos[2]
    Local $hp[2]
    $pos[0] = -1
    $pos[1] = -1
    $hp[0] = -1
    $hp[1] = -1
    $color = $sign[0]
    do
        $hp = next_screen_hotpoint($hp[1],$color)
        if $hp[1] <> -1 then
            $aux = $hp[1]
            do
                if verify_sign($sign,$width,$height,$hp[0],$hp[1]) then
                    $pos = getMiddlePoint($hp[0],$hp[1],$hp[0]+$width-1,$hp[1]+$height-1)
                    return $pos
                endif
                $hp = next_row_hotpoint($hp[0],$hp[1],$color)
            until $hp[1] == -1
            $hp[1] = $aux
        endif
    until $hp[1] == -1
    return $pos
endfunc

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

2 hours ago, water said:

You are sure your function starts with an underscore?
I searched the forum and found this. If this is the UDF you use then the function name does not start with an "_". If you use another image search library, then please open the UDF and check the name of the function!

Hi @water

I'm pretty sure @AZPERRY uses  this UDF (not the one you have been linking to).

_ImageSearch is the correct function name, see :

Spoiler
#include-once
; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
; Language:       English
; Description:    Functions that assist with Image Search
;                 Require that the ImageSearchDLL.dll be loadable
;
; ------------------------------------------------------------------------------

;===============================================================================
;
; Description:      Find the position of an image on the desktop
; Syntax:           _ImageSearchArea, _ImageSearch
; Parameter(s):
;                   $findImage - the image to locate on the desktop
;                   $tolerance - 0 for no tolerance (0-255). Needed when colors of
;                                image differ from desktop. e.g GIF
;                   $resultPosition - Set where the returned x,y location of the image is.
;                                     1 for centre of image, 0 for top left of image
;                   $x $y - Return the x and y location of the image
;
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
;
; Note: Use _ImageSearch to search the entire desktop, _ImageSearchArea to specify
;       a desktop region to search
;
;===============================================================================
Func _ImageSearch($findImage,$resultPosition,ByRef $x, ByRef $y,$tolerance, $HBMP=0)
   return _ImageSearchArea($findImage,$resultPosition,0,0,@DesktopWidth,@DesktopHeight,$x,$y,$tolerance,$HBMP)
EndFunc

Func _ImageSearchArea($findImage,$resultPosition,$x1,$y1,$right,$bottom,ByRef $x, ByRef $y, $tolerance,$HBMP=0)
    ;MsgBox(0,"asd","" & $x1 & " " & $y1 & " " & $right & " " & $bottom)
    if $tolerance>0 then $findImage = "*" & $tolerance & " " & $findImage
If IsString($findImage) Then
    $result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"str",$findImage,"ptr",$HBMP)
Else
    $result = DllCall("ImageSearchDLL.dll","str","ImageSearch","int",$x1,"int",$y1,"int",$right,"int",$bottom,"ptr",$findImage,"ptr",$HBMP)
EndIf

    ; If error exit
    if $result[0]="0" then return 0

    ; Otherwise get the x,y location of the match and the size of the image to
    ; compute the centre of search
    $array = StringSplit($result[0],"|")

   $x=Int(Number($array[2]))
   $y=Int(Number($array[3]))
   if $resultPosition=1 then
      $x=$x + Int(Number($array[4])/2)
      $y=$y + Int(Number($array[5])/2)
   endif
   return 1
EndFunc

;===============================================================================
;
; Description:      Wait for a specified number of seconds for an image to appear
;
; Syntax:           _WaitForImageSearch, _WaitForImagesSearch
; Parameter(s):
;                   $waitSecs  - seconds to try and find the image
;                   $findImage - the image to locate on the desktop
;                   $tolerance - 0 for no tolerance (0-255). Needed when colors of
;                                image differ from desktop. e.g GIF
;                   $resultPosition - Set where the returned x,y location of the image is.
;                                     1 for centre of image, 0 for top left of image
;                   $x $y - Return the x and y location of the image
;
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
;
;
;===============================================================================
Func _WaitForImageSearch($findImage,$waitSecs,$resultPosition,ByRef $x, ByRef $y,$tolerance,$HBMP=0)
    $waitSecs = $waitSecs * 1000
    $startTime=TimerInit()
    While TimerDiff($startTime) < $waitSecs
        sleep(100)
        $result=_ImageSearch($findImage,$resultPosition,$x, $y,$tolerance,$HBMP)
        if $result > 0 Then
            return 1
        EndIf
    WEnd
    return 0
EndFunc

;===============================================================================
;
; Description:      Wait for a specified number of seconds for any of a set of
;                   images to appear
;
; Syntax:           _WaitForImagesSearch
; Parameter(s):
;                   $waitSecs  - seconds to try and find the image
;                   $findImage - the ARRAY of images to locate on the desktop
;                              - ARRAY[0] is set to the number of images to loop through
;                                ARRAY[1] is the first image
;                   $tolerance - 0 for no tolerance (0-255). Needed when colors of
;                                image differ from desktop. e.g GIF
;                   $resultPosition - Set where the returned x,y location of the image is.
;                                     1 for centre of image, 0 for top left of image
;                   $x $y - Return the x and y location of the image
;
; Return Value(s):  On Success - Returns the index of the successful find
;                   On Failure - Returns 0
;
;
;===============================================================================
Func _WaitForImagesSearch($findImage,$waitSecs,$resultPosition,ByRef $x, ByRef $y,$tolerance,$HBMP=0)
    $waitSecs = $waitSecs * 1000
    $startTime=TimerInit()
    While TimerDiff($startTime) < $waitSecs
        for $i = 1 to $findImage[0]
            sleep(100)
            $result=_ImageSearch($findImage[$i],$resultPosition,$x, $y,$tolerance,$HBMP)
            if $result > 0 Then
                return $i
            EndIf
        Next
    WEnd
    return 0
EndFunc

 

The UDF requires ImageSearchDLL.dll , which is included in the Zipfiles. In the thread, some users report bugs or have made modifications. Also see https://www.autoitscript.com/forum/topic/189338-imagesearchau3-help/

To be honest, I never had the desire (nor a reason) to work with ImageSearch . Maybe I would give it a try just for fun, but I'm not sure if I can trust the .dll.

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Right guy's i have  progressed.

My issue now is that it doesn't actually click on the image.

The image is just on the desktop.

HotKeySet("{ESC}", "MyExit")

#include "ImageSearch.au3"

Func MyExit()

    MsgBox(0, "Ending")
    Exit

EndFunc   ;==>MyExit

$x1=0
$y1=0

While 1
    _click()
 WEnd


Func _click()
$pump = "‪C:\Users\Perrya\Desktop\Autoit\pump.png"

$pump1 = _ImageSearch($pump,1,$x1,$y1,0,0)
    ConsoleWrite("Starting search" & @CRLF)

   if $pump1 = 1 then
      ConsoleWrite("found pump" & @CRLF)
      MouseClick($pump1)
      Sleep(100)
      EndIf
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...