I am trying to code a BASH script that will do the following while a program's window has focus. This would be done under KDE:
While holding left mouse button:
- press alt-4 key
- check for color (white) at pixel position 1; if pixel color exists, press alt-1 key
- check for color (white) at pixel position 2; if pixel color exists, press alt-2 key
- check for color (white) at pixel position 3; if pixel color exists, press alt-3 key
Single press mouse button 4:
- toggle script sequence on/off
- press alt-5
I have the following code but it seems to be quite slow. Any way to optimize?
#!/bin/bash
# mouse id. use xinput --list. verify with xinput --query-state [id]
mouse=12
# set colors with xwd. run the following in terminal to get cursor position:
# while true; do xdotool getmouselocation; sleep 0.2; clear; done
while :; do
state="$(xinput --query-state "$mouse")"
color1="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+195+247" txt:- | grep -om1 '#\w\+')"
color2="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1407+681" txt:- | grep -om1 '#\w\+')"
color3="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1200+256" txt:- | grep -om1 '#\w\+')"
color4="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1095+257" txt:- | grep -om1 '#\w\+')"
color5="$(xwd -root -silent | convert xwd:- -depth 8 -crop "1x1+1195+258" txt:- | grep -om1 '#\w\+')"
# if lmb (mouse 1) pressed
if [[ "$state" == *"button[1]=down"* ]]; then
if [[ "$color1" == "#FFFFFF" ]]; then
xdotool key --clearmodifiers a
elif [[ "$color2" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers b
elif [[ "$color3" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers c
elif [[ "$color4" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers d
elif [[ "$color5" == *"#FFFFFF"* ]]; then
xdotool key --clearmodifiers e
fi
fi
done
Any help would be appreciated
A couple of things spring to mind...
Firstly, it seems you are not really interested in the colours of the pixels if the left mouse button is not pressed, so I would avoid getting all the colours if that is not the case. I mean:
That should allow you to test more frequently.
Secondly, you are calling
xwdwhich starts a process and grabs megabytes of data, then startingconvertwhich is another process that receives megabytes of data and then startinggrep. And you are doing all that 5 times to get just five pixels. So, instead of that, start a singlexwd, a singleconvertand get your 5 pixels in one go.Rather than use
xwd, I am just generating a repeatable, fixed image each time here. Your code does this:which produces this:
I am suggesting this:
which produces the same 5 pixels in a single 5-pixel image in just one process rather than 5
xwdprocesses and 5convertprocesses:You might consider piping the output from the previous line into
awkalong these lines:Then you'll get something along these lines:
Here is my best effort, bearing in mind I don't have X11 available to me, or even a computer for testing:
If it has some bugs, you can debug it by running like this:
or paste it into shellcheck.