Skip to main content

Parameterize Search

find, findAll and waitFor accept OptionalSearchParameters to fine-tune the search.

This allows to e.g. limit the search space to a certain portion of your screen:

const { screen, Region, OptionalSearchParameters, imageResource } = require("@nut-tree/nut-js");
const { AbortController } = require("node-abort-controller");
(async () => {
// Configure the postion and size of the area you wish Nut to search
const searchRegion = new Region(10, 10, 500, 500);
// Configure the confidence you wish Nut to have before finding a match
const confidence = 0.88;
// Configure whether Nut should try to match across multiple scales of Image
const searchMultipleScales = false;
// Configure an Abort controller so that you can cancel the find operation at any time
const controller = new AbortController();
const { signal } = controller;
// Feed your parameters into the OptionalSearchParameters constructor to make sure they fit the spec
const fullSearchOptionsConfiguration = new OptionalSearchParameters(searchRegion, confidence, searchMultipleScales, signal);
// .find() will return the Region where it found a match based on your search parameters and provided Image data
const matchRegion = await screen.find(imageResource("image.png"), fullSearchOptionsConfiguration);
const cancelFindTimeout = setTimeout(() => {
}, 5000);