Sign in to follow this  
Followers 0
appstache

Basic ImageSearch Loop

6 posts in this topic

#1 ·  Posted (edited)

Hey all, I'm trying to write what I thought to be a simple script for my company. We use the app "Fuze" for multiple TV HUDs across multiple offices. We needed a quick way to restart and rejoin our meeting.

The script opens the app, and then SHOULD wait for a certain image to show up, and then click it. The problem is that some of our HUDs are slower than others so I can't just sleep for a certain amount of time. I want the script to check if the image is on the screen and if it isn't sleep for 1 second and then check again.

I threw this together pretty quickly based off of another imagesearch project we have, it's probably something basic as I'm pretty fresh at this myself so please keep that in mind.

Local $hWnd = WinWait("[CLASS:Fuze]", "", 4)

;Running Fuze Meeting
Run(@LocalAppDataDir & "\Fuzebox\Fuze\Fuze.exe")

#include <ImageSearch.au3>
#include <GDIPlus.au3>

$fileA = @ScriptDir & "\join.png"
$fileB = @ScriptDir & "\meeting.png"

_GDIPlus_Startup()

$hImageA =_GDIPlus_ImageLoadFromFile($fileA) ;this is the "Join Meeting" Button
$hBitmapA = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageA)

$hImageB =_GDIPlus_ImageLoadFromFile($fileB) ;this is for the main logo on the first UI screen
$hBitmapB = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageB)

$x = 0
$y = 0

Local $hWnd = WinWait("[CLASS:Fuze]", "", 4)
WinActivate($hWnd)

While 1
$result = _ImageSearch($hBitmapB, 1, $x, $y, 20)
If $result = 1 Then
    MouseMove($x, $y)
    MouseClick("Left")
    Send("BLAHBLAHBLAHREDACTED")
    Send("{TAB}")
    Send("{ENTER}")
    ExitLoop
 EndIf
 If $result = 0 Then
    sleep(1000)
 EndIf
 WEnd

While 2
$result2 = _ImageSearch($hBitmapA, 1, $x, $y, 20)
If $result2 = 1 Then
    MouseMove($x, $y)
    MouseClick("Left")
    ExitLoop
 EndIf
 If $result2 = 0 Then
    Sleep(1000)
 EndIf
 WEnd

_GDIPlus_ImageDispose($hImageA)
_GDIPlus_ImageDispose($hImageB)
_GDIPlus_Shutdown()

again, the program should:

1. Open

2. Wait for the first image to show up

3. Once it shows up it should click the image, enter the text, hit enter.

4. Wait for another image to show up

5. Click the image once it appears

As it is right now, it clicks the first image without issue, but It's not looping and sleeping waiting for the images to appear (specifically the second image)

Any help would be greatly appreciated. Thanks so much!

Edited by appstache

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Just a suggestion: Have you tried using the autoit window info tool on your window?  If at all possible, do control based loop rather than image based loop.

You can also use my sig to grab all the controls on the window...pass in the handle to it.

If you output that, when your image is present, maybe we can workaround the image issue.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Just a suggestion: Have you tried using the autoit window info tool on your window?  If at all possible, do control based loops rather than image based loops.

That's how I initially hoped to do this, which would have been easy for me, but unfortunately the window has no controls so that's out of the question.

To make it more fun pixelsearch doesn't work because multiple buttons have the same color. So Imagesearch it is.

Also of note: If I put a sleep(10000) after the first while loop I see it finishes both while loops. If it doesn't sleep then it just perpetually loops on the second while loop.

Share this post


Link to post
Share on other sites

At risk of bumping my own thread, I will say I also tried it in a do loop with the same issue. I believe I've narrowed it down to the WinActive command causing the hangup though I'm not certain. I AM certain it gets stuck in the loop sleeping and can't see the image even when it's on screen. I verified this by having a MsgBox pop up and disappear while sleeping.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I have the same profile as you do : I am new and made a script based on ImgSearch.

Mine is an auto save which waits the OK window to open to click okay and proceed with other processes (sending mails, doing virtual printing etc) so we can see how alike it is.

I have written my code this way :

Func sauvegarde8 ()
   $Search = _ImageSearchArea('screen2.png', 1, 1680, 218, 1760, 280, $x8, $y8, 75)
   If $Search = 1 Then
      sleep(200)
sauvegarde7 ()
Else
   sleep(500)
   sauvegarde8 ()
   EndIf
EndFunc


Func sauvegarde7 ()
   $Search = _ImageSearchArea('screen1.png', 1, 1680, 218, 1760, 280, $x8, $y8, 75)
   If $Search = 1 Then
      sleep(200)
sauvegarde8 ()
Else
   sleep(500)
   sauvegarde7 ()
   EndIf
EndFunc

Sauvegarde7 ()

This is very simplified code because I have more than 10 functions actually.

This is not well coded (although it works 100%) because it leads to overflow after hours of usage because of the function recursion (I found a fix thanks to this great community though).

You have to use bigger sleep times than the ones I showed if you use this method because the longer the sleep time the more time untill It overflows (note than I have overflow fix if you are intereted).

 

As for going with your current code which I believe is smarter :

1. After it doesn't detect image even though it's on screen, you didn't have an error msg ? when you check bottom right windows process you still see your script running ?

2 . What happens if you set a tolerance in the second Click to 230 instead of 20 (it should click the wind) ?

3 . Why don't you delete your mousemove and simply use 1 mouseclick with coordinates ?

4 . You say if you use 10 000 sleep time it ends the script.. But does it do the clicking (of first img) before ending ? 

5 . Why don't you just look for the join.png and meeting.png (instead of rewrite them in bitmap)

My script worked after the overflow fix and I laid out on it because I have load of work atm, but this is what I think would be better to use : 

Do...untill

In your case :

Local $result = 0
Do
$result = _ImageSearch($hBitmapB, 1, $x, $y, 20)
sleep(1000)
Until $result = 1

Or even : 

While $result = 0
$result = _ImageSearch($hBitmapB, 1, $x, $y, 20)
sleep(1000)
Wend
Edited by Ulysse31

Share this post


Link to post
Share on other sites

I'd be interested in that overflow fix if you could direct me to it, thanks!

Share this post


Link to post
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
Sign in to follow this  
Followers 0