For Advanced

Lesson 7/10

Chasing Dots

Time for some fun! Let’s play a game — or rather let robot play it. Run this script to see the robot’s flawless victory in mimicking mouse clicks on red dots (be sure to enable the Images Addon before you press F9 key):

♥image = ♥environment⟦USERPROFILE⟧\Desktop\dot.png
file.download https://github.com/G1ANT-Robot/G1ANT.Manual/raw/develop/-assets/dot.png filename ♥image
program chrome arguments https://mouseaccuracy.com/game
window ✱Accuracy✱ style maximize

while ⊂true⊃
  label retry
  image.find image1 ♥image timeout 2 errorjump retry
  mouse.click ♥result mousedelay 2
end

What happened here? First, the robot downloaded a tiny image (3x3px) from our GitHub repository to a user’s Desktop. Then it opened Chrome (or other browser of your choice, if you’d provided its name in the third line) and navigated to the Mouse Accuracy game page. The browser window was maximized and the game started: a user is supposed to click the red dots that appear on the screen.

The whole robot magic happened within the while loop. Normally, this loop is executed as long as the condition given as the while command argument is true. Here, this condition is always ⊂true⊃, so the loop is infinite and to stop it, you have to press Ctrl+F12.

Inside this loop, the image.find and the mouse.click commands work together to find and click the red dots: the image.find command searches for the image downloaded in the second line of the script and when it’s found, the mouse.click command steps in, clicking the pixel on the screen returned by the image.find command in the ♥result variable.

In case the image is not found (an error occurs), the robot jumps back to the retry label, so the image search starts over.

Note: If you are using multiple displays, please specify the screensearcharea argument for the image.find command. For example, if your main monitor is 1920x1080, change the image.find line to:

image.find image1 ♥image screensearcharea 0⫽0⫽1920⫽1080 timeout 2 errorjump retry

The next lesson shows how you can access different Windows UI elements directly.