Help creating my first function in eggPlant

I have a chunk of code that I re-use over and over again in my scripts and I would like to create a function out of it that takes 3 parameters.

The code below first sets the search rectangle to a certain part of the application (this will be the 1st parameter). It then looks for an image (the 2nd parameter) by scrolling in a certain direction (the 3rd parameter). So I’d like a function called imageSearcher that would be called in the following way to execute the code below imageSearcher sharedObjectsArea selectionMales Down.

The third parameter will be the direction in which the scroll occurs first so I’ll be changing the code to change direction if it has not found the object whilst searching in the initial direction.

Basically I’m new to the whole Handlers, Helpers thing in EggPlant and I was wondering what people’s thoughts were on how to go about this? :?

Set the SearchRectangle to ("sharedObjects", "bottomRightWorkspace") -- 1st parameter
put ImageLocation("scrollBarVertical") into coord
SetOption RemoteWorkInterval, 0.2
Set the ImageSearchTime to 0.25
SetOption ImageSearchCount, 2
repeat until imageFound("selectionMales") -- 2nd parameter
	Drag coord
	add (0, 5) to coord
	Drop coord -- drag 5 pixels in the down direction  -- 3rd parameter
end repeat
SetOption RemoteWorkInterval, 0.7 -- back to default value
Set the ImageSearchTime to 1.8 -- back to default value
SetOption ImageSearchCount, 7 -- back to default value
Set the SearchRectangle to () -- back to default value

This is what I was thinking of for the function code:

to imageSearcher searchArea, searchImage, initialDirection //Starts the handler

	If initialDirection > 0 Then
		secondDirection == -5
	Else
		secondDirection == 5
	End If	

	Set the SearchRectangle to (searchArea, "bottomRightWorkspace") //Defines which search area to use
	put ImageLocation("scrollBarVertical") into coord //Finds the scrollbar

	SetOption RemoteWorkInterval, 0.2 //Makes the dragging quicker
	Set the ImageSearchTime to 0.25 //Makes the dragging quicker

	repeat until imageFound(searchImage) //Defines the image to search for
		Drag coord
		add (0,initialDirection) to coord //Defines which direction to drag
		Drop coord
		If imageFound(scrollEnd) Then  //If scrolled to end without finding image then drag in other direction
			repeat until imageFound(searchImage)
				Drag coord
				add (0,secondDirection)
				Drop coord
			end repeat
		Else
		End If
	end repeat

	SetOption RemoteWorkInterval, 0.7 //Back to default value
	Set the ImageSearchTime to 1.8 //Back to default value
	SetOption ImageSearchCount, 7 //Back to default value
	Set the SearchRectangle to () //Back to default value

end imageSearcher //Ends the handler

I’m not an experienced coder so please be gentle! :oops:

OK, so far I’ve created a Helper Suite with a script called imageSearcher that has the code below. My first attempt was without attempting in eggPlant but since then I’ve edited it. It seems to work but it takes 6 seconds between not finding “scrollEnd” and starting the loop again. Does anyone know why there is such a delay? There is also a 6 second delay before it starts the first loop. Do If statements take a long time to execute?

One other issue is that the “add (0,oppDirection)” doesn’t work. I get an error message saying “Command add was called with invalid parameters”.

to imageSearcher searchArea, searchImage, direction //Starts the handler
	
	Set the SearchRectangle to (imageLocation(searchArea), imageLocation("bottomRightWorkspace")) //Defines which search area to use
	put ImageLocation("scrollBarVertical") into coord //Finds the scrollbar
	
	SetOption RemoteWorkInterval, 0.02 //Makes the dragging quicker
	Set the ImageSearchTime to 0.01 //Makes the dragging quicker
	SetOption ImageSearchCount, 2
	
	If direction > 0 Then
		put -5 into oppDirection
	Else
		put 5 into oppDirection
	End If
	
	repeat until imageFound(searchImage) //Defines the image to search for
		Drag coord
		add (0,direction) to coord //Defines which direction to drag
		Drop coord
		If imageFound(0.1, "scrollEnd") Then
			exit repeat
		Else
		End If
	end repeat
	
	repeat until imageFound(searchImage)
		Drag coord
		add (0,oppDirection)
		Drop coord
	end repeat
	
	SetOption RemoteWorkInterval, 0.7 //Back to default value
	Set the ImageSearchTime to 1.8 //Back to default value
	SetOption ImageSearchCount, 7 //Back to default value
	Set the SearchRectangle to () //Back to default value
	
end imageSearcher //Ends the handler

The problem with “add (0,oppDirection)” is that you’re not adding it to anything. Your command is correct in the first repeat loop: “add (0, direction) to coord”.

You don’t need the “Else” because you don’t have anything in that part of the if statement.

You can also simplify the modification of the run options by storing all of the starting values:

put getOptions() into myOptions

then making your changes to the individual settings, and then restoring all of the default (or starting) values at once:

setOptions myOptions

I’m not sure about the delay between the repeats, but if your image is small, low contrast, and/or just lacking in easily identified elements, it may just take eggPlant a while to search the screen for it. Every time it finds a potential match as it scans the screen, it slows down a little bit to see if it’s found the actual image. So, for example, if you’re looking for a character in a page of text, it will take longer to find that than if you were looking for a yellow square on a screen that was otherwise entirely green.

Thanks for that Matt.

The function has ended up as follows:

to objectSearcher searchArea, searchImage, direction //Starts the handler

	put getOptions() into myOptions  //Stores default options
	
	SetOption RemoteWorkInterval, 0.1 //Makes the dragging quicker
	Set the ImageSearchTime to 0.1 //Makes the dragging quicker
	SetOption ImageSearchCount, 0
	
	Set the SearchRectangle to (imageLocation(searchArea), imageLocation("bottomRightWorkspace")) //Defines which search area to use
	WaitFor 10.0, "scrollBarVertical"
	put ImageLocation("scrollBarVertical") into coord //Finds the scrollbar	
	
	repeat until imageFound(searchImage) //Defines the image to search for
		Drag coord
		add (0,direction) to coord //Defines which direction to drag
		Drop coord
		
		If imageFound(0.1, "scrollEnd") Then
			exit repeat
		End If
		
	end repeat
	
	If direction > 0 Then
		put -5 into oppDirection
	Else
		put 5 into oppDirection
	End If
	
	repeat until imageFound(searchImage)
		Drag coord
		add (0, oppDirection) to coord
		Drop coord
	end repeat
	
	setOptions myOptions //Restores options to defaults
	
end objectSearcher //Ends the handler

If anyone wants to use it then feel free. You’ll need an image of the vertical scroll bar and 2 images of the scrollbar at the top and bottom in a folder called scrollEnd for it to work. You just need to change the final parameter to 5 to scroll down and -5 to scroll up. You can of course adjust those values to speed or slow things down.