#!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail die() { echo "$*" >&2 exit 1 } take_screenshot() { t=$(mktemp --suffix=.png) xwd -root -silent | magick xwd:- png:"$t" echo "$t" } locate_text() { predicate= i=1 for word in $1;do predicate+="[span[$i][text()='$word']]" i=$((i+1)) done titles=$(xmllint --nowarning --html --xpath "//span[@class='ocr_line']$predicate/@title" -) if [[ -z "$titles" ]];then die "Text not found" fi if (( "$(wc -l <<< "$titles")" > 1 ));then die "Text appears multiple times: $titles" fi title=$(cut -d\" -f2 <<< "$titles") bbox=$(cut -d\; -f1 <<< "$title") x1=$(cut -d' ' -f2 <<< "$bbox") y1=$(cut -d' ' -f3 <<< "$bbox") x2=$(cut -d' ' -f4 <<< "$bbox") y2=$(cut -d' ' -f5 <<< "$bbox") x=$(( (x1 + x2) / 2 )) y=$(( (y1 + y2) / 2 )) echo "Found text at $x $y" >&2 echo "$x $y" } ss=$(take_screenshot) location=$(tesseract "$ss" - hocr | locate_text "$1") shred -u "$ss" # shellcheck disable=SC2086 xdotool mousemove $location click 1