Object MethodScanning and Detection

Region:detectUi(options?)

Returns visible text groups or buttons within a region as a list of Results without a search phrase.

Documented for app version 1.0.64Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

This is an object method: call it with a colon on a Region object created with Region(x, y, w, h), or Region() for the whole screen (region:<method>(...)). The global Region table does not have this method. Requires Macro Handler 1.0.62 or later.

When To Use It

In detection flow, refresh the screen with Snap.screenRefresh() first, then narrow the search area and confidence using Region and FindParam.

Parameters and Return

kind: text or button. unit: word, line, paragraph. buttonSource: auto, android, visual. Defaults to text, line and auto. An empty result returns {}.

Options and Limits

Filter by color (#RRGGBB), tolerance (0–255, default 24), minLength/maxLength, minWidth/maxWidth and minHeight/maxHeight. A maximum of 0 is unlimited. Pass a Region or {excludeRegions={r1,r2}} as excludeRegion. selection accepts first, random or all (default). mark=true outlines all eligible results before selection.

Best Combined With

profile defaults to auto. Text profiles are quick_text, balanced_text, precision_text, tiny_text, low_contrast_text, scene_text, rotated_text, styled_text, chinese_text, japanese_text, korean_text, devanagari_text, arabic_text and cyrillic_text. Use script to select the OCR writing system. Drawn button profiles are button_fast, button_balanced and button_precise. To scan inside a match, call hit:getRegion():detectUi(options).

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local area = Region(20, 100, 360, 240)
local heading = Region(20, 100, 360, 32)
local hits = area:detectUi({kind = "text", unit = "word", profile = "scene_text", minLength = 3, excludeRegion = heading, mark = true})
for _, hit in ipairs(hits) do
  print(hit:getText(), hit:getW(), hit:getH())
end

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local area = Region(20, 100, 360, 240)
local heading = Region(20, 100, 360, 32)
local hits = area:detectUi({kind = "text", unit = "word", profile = "scene_text", minLength = 3, excludeRegion = heading, mark = true})
for _, hit in ipairs(hits) do
  print(hit:getText(), hit:getW(), hit:getH())
end

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local area = Region(20, 100, 360, 240)
local heading = Region(20, 100, 360, 32)
local hits = area:detectUi({kind = "text", unit = "word", profile = "scene_text", minLength = 3, excludeRegion = heading, mark = true})
for _, hit in ipairs(hits) do
  print(hit:getText(), hit:getW(), hit:getH())
end
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local area = Region(20, 100, 360, 240)
  local heading = Region(20, 100, 360, 32)
  local hits = area:detectUi({kind = "text", unit = "word", profile = "scene_text", minLength = 3, excludeRegion = heading, mark = true})
  for _, hit in ipairs(hits) do
    print(hit:getText(), hit:getW(), hit:getH())
  end
end)

if not ok then
  print("API step failed: Region:detectUi: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- In detection flow, refresh the screen with Snap.screenRefresh() first, then narrow the search area and confidence using Region and FindParam.
local function run_detectui_step()
  local area = Region(20, 100, 360, 240)
  local heading = Region(20, 100, 360, 32)
  local hits = area:detectUi({kind = "text", unit = "word", profile = "scene_text", minLength = 3, excludeRegion = heading, mark = true})
  for _, hit in ipairs(hits) do
    print(hit:getText(), hit:getW(), hit:getH())
  end
end

local ok, err = pcall(run_detectui_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local area = Region(20, 100, 360, 240)
local buttons = area:detectUi({kind = "button", buttonSource = "android", minWidth = 40, mark = true})
if #buttons == 1 then click(buttons[1]) end