SearchRectangle reset problem.

We have written a script (TreeNodeOpen.script) to look for an image, then create a fairly small SearchRectangle to the left of it to do more in.

Params ImageTime, ImageName, Action 

MoveTo (1,1)
if imagefound (ImageTime, ImageName)
then
	(* Variablelizes image location information from the found image and uses them to create search boundaries to be used later in the script. *)
	set TestLeft = left(ImageRectangle(ImageName))
	set TestTop = top(ImageRectangle(ImageName))
	set TestBottom = bottom(ImageRectangle(ImageName))
	set SearchAreaStart = TestLeft - 32
	set UpperLeft = (SearchAreaStart,TestTop)
	set BottomRight = (TestLeft,TestBottom)
	(* Search for the tree node right arrow. *)
	set the SearchRectangle to (UpperLeft, BottomRight)
	if  imagefound ("TreeNodeUnopened")
	then
		log "Expected image was found, so opening: " & ImageName 
		do Action && foundImageLocation()
		MoveTo (1,1)
	else
		if imagefound ("TreeNodeOpened")
		then
			log "Tree node was found to be open already: " & ImageName 
			Return
		else
			log"Expected Image Not Found: " & "TreeNodeOpened"
			WaitFor "1.0", "TreeNodeOpened"
			Return
		end if
	end if
else 
	log "Expected Image Not Found: " & ImageName
	WaitFor "1.0", ImageName
	return
end if

The script works great if we call it from another script once. For example:

TreeNodeOpen 8, "test_image", click

But, if we call it twice, the second run through fails because the SearchRectangle has not been reset. For example:

TreeNodeOpen 8, "test_image", click
TreeNodeOpen 8, "test_image2", click

I have tried incorporating “Set the SearchRectangle to ()” at various locations in the TreeNodeOpen.script with no luck.

The only way I have been able to get this script to work when called more than once from another script is to do the following:

TreeNodeOpen 8, "test_image", click
Set the SearchRectangle to ()
TreeNodeOpen 8, "test_image2", click

I don’t like my solution. What am I missing about getting this line to reset the SearchRectangle to work within the TreeNodeOpen.script?

So, my recommendation would be that instead of permanently changing the SearchRectangle, you use an Image Override.

So remove

set the SearchRectangle to (UpperLeft, BottomRight)

And change your ImageFound functions to:

if imageFound(ImageName: "TreeNodeUnopened", SearchRectangle:(UpperLeft, BottomRight))
if imageFound(ImageName: "TreeNodeOpened", SearchRectangle:(UpperLeft, BottomRight))

Thanks Jonathan. I made the changes you suggested and am still having the same problem. Any other ideas?

That’s very odd. If you’ve removed the line that actually changes the SearchRectangle then you should have no residual impact. The temporary one on the image search definitely gets cleared.

You might need to send a more complete log and/or screenshot for us to investigate it.

Chalk one up to the silly user. I had the script in two locations and was modifying the one that was not getting executed.

It works great!

I also got rid of a few returns I did not need.

Here is the final version if anyone is interested:

Params ImageTime, ImageName, Action 

MoveTo (1,1) 
if imagefound (ImageTime, ImageName)
then 
	(* Variablelizes image location information from the found image and uses them to create search boundaries to be used later in the script. *) 
	set TestLeft = left(ImageRectangle(ImageName)) 
	set TestTop = top(ImageRectangle(ImageName)) 
	set TestBottom = bottom(ImageRectangle(ImageName)) 
	set SearchAreaStart = TestLeft - 32 
	set UpperLeft = (SearchAreaStart,TestTop) 
	set BottomRight = (TestLeft,TestBottom) 
	(* Search for the tree node right arrow. *) 
	if imageFound(ImageName: "TreeNodeUnopened", SearchRectangle:(UpperLeft, BottomRight))
	then 
		log "Expected image was found, so opening: " & ImageName 
		do Action && foundImageLocation() 
		MoveTo (1,1) 
	else 
		(* Search for the tree node down arrow. *) 
		if imageFound(ImageName: "TreeNodeOpened", SearchRectangle:(UpperLeft, BottomRight)) 
		then 
			log "Tree node was found to be open already: " & ImageName 
		else 
			LogError "Expected Image Not Found: " & "TreeNodeOpened" 
			WaitFor "1.0", "TreeNodeOpened" 
		end if 
	end if 
else 
	LogError "Expected Image Not Found: " & ImageName 
	WaitFor "1.0", ImageName 
end if

One additional suggestion: Every call to the ImageRectangle() function does a full search for the image on the screen. So you might want to just call it once, putting the rectangle into a variable and then use that value.

put ImageRectangle(ImageName) into imageRectangle
set TestLeft = left(imageRectangle)
-- and so forth

That could save a couple of seconds each time this is called.

That is significantly faster Doug. Thanks!