Ready-to-use Code ExampleRegionNumKVGaming

OCR Score Reader with History

Reads a numeric score from a screen region, keeps a persistent history in the KV store, and notifies on a new record. `Num.parseOcr` tolerates common OCR digit-recognition errors (for example "1,234" or "l234").

Completion, parameter hints and type checking. Installation instructions and downloads are hosted on GitHub.

Copyable Example Code

You can paste this into the code editor; still verify target image, region, color, text, and timing values in your own device flow.

-- OCR score reader
-- Region:readAsString + Num.parseOcr tolerate OCR mistakes

local scoreRegion = Region(800, 100, 280, 80)
local bestKey = "best_score"

Snap.screenRefresh()

-- Read the area and convert it to a number
local rawText = scoreRegion:readAsString()
local score = Num.parseOcr(rawText, 0)

if score <= 0 then
  toast("Could not read the score: '" .. rawText .. "'")
  return
end

-- Compare with the record
local bestScore = KV.getNumber(bestKey, 0)

if score > bestScore then
  KV.set(bestKey, score)
  KV.increment("record_count", 1)
  System.vibrate(300)
  System.noti("New record: " .. score .. " (previous " .. bestScore .. ")")
else
  toast("Score: " .. score .. " (record: " .. bestScore .. ")")
end

-- Keep a short history
local history = KV.get("score_history", "")
local newEntry = score .. ","
if #history > 200 then
  history = history:sub(-100)
end
KV.set("score_history", history .. newEntry)

Implementation and Adaptation Notes

  • Before running, replace sample values such as image names, text, colors, coordinates, and file paths with your own macro values.
  • For examples that use Region or coordinates, retest the target area on different resolutions and DPI values.
  • Tune mScore, timeout, and scan rate in a test macro first, then move the verified values into the production macro.
  • Avoid overly wide Region areas; a tighter area scans faster and reduces false matches.