Skip to content

Instantly share code, notes, and snippets.

@mauricioprado00
Created July 19, 2017 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricioprado00/8c538a40f51f4e68e0ccc4eacfcf8ebd to your computer and use it in GitHub Desktop.
Save mauricioprado00/8c538a40f51f4e68e0ccc4eacfcf8ebd to your computer and use it in GitHub Desktop.
click by image
#!/usr/bin/env bash
# requires: imagemagik, xdotool, xwininfo
# usage: click-by-image [image-file-to-search] [window partial title] [section x] [section y] [section width] [section height]
# section parameter is to cut the image of the window and make imagemagik faster
image_file=$1
window_title=$2
section_x=$3
section_y=$4
section_w=$5
section_h=$6
temp_file=/tmp/click-by-image-screenshot.jpg
temp_file_section=/tmp/click-by-image-section.jpg
temp_file_similarity=/tmp/similarity.jpg
if [ -z "$window_title" ]; then
window_id=$(xwininfo -tree -root | grep 'Root window id' | head -n1 | awk '{print $4}')
else
window_id=$(xwininfo -tree -root | grep "$window_title" | head -n1 | awk '{print $1}')
fi
if [ -z "$section_h" ]; then
section_h=$(xwininfo -id $window_id | grep 'Height:' | awk '{print $NF}')
fi
if [ -z "$section_w" ]; then
section_w=$(xwininfo -id $window_id | grep 'Width:' | awk '{print $NF}')
fi
if [ -z "$section_y" ]; then
section_y=0
fi
if [ -z "$section_x" ]; then
section_x=0
fi
window_x=$(xwininfo -id $window_id | grep 'Absolute upper-left X' | awk '{print $NF}')
window_y=$(xwininfo -id $window_id | grep 'Absolute upper-left Y' | awk '{print $NF}')
import -window $window_id $temp_file
convert -crop ${section_w}x${section_h}+${section_x}+${section_y} $temp_file $temp_file_section
position=$(compare -metric RMSE -subimage-search $temp_file_section $image_file $temp_file_similarity 2>&1 | awk '{print $NF}')
rel_x=$(echo $position | awk -F ',' '{print $1}')
rel_y=$(echo $position | awk -F ',' '{print $2}')
abs_x=$(($window_x + $rel_x + $section_x))
if [ $? -ne 0 ]; then
echo "not round" 1>&2
exit 1
fi
abs_y=$(($window_y + $rel_y + $section_y))
xdotool mousemove $abs_x $abs_y click 1 click 1 click 1
sleep 0.3
xdotool click 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment