I need to get the current mouse coordinates in bash and xdotool doesn't seem to be working for me. How would I do this?
5 Answers
To avoid all the sed/awk/cut stuff, you can use
xdotool getmouselocation --shell
In particular,
eval $(xdotool getmouselocation --shell)
will put the position into shell variables X
, Y
and SCREEN
. After that,
echo $X $Y
will give a snippet ready for a later xdotool mousemove
or any other use.
My extra for sequential clicking into a few positions is a file positions.txt (given by a few eval/echo runs):
123 13
423 243
232 989
And the code that uses it is:
while read line; do
X=`echo $line| cut -c1-3`;
Y=`echo $line| cut -c4-7`;
xdotool mousemove --sync $(( 0.5 + $X )) $(( 0.5 + $Y ));
xdotool click 1
done < positions.txt
If there is no need to scale pixels (unlike my case), it could be a simple
while read line; do
xdotool mousemove --sync $line;
xdotool click 1
done < positions.txt
-
Now the command also outputs WINDOW=... , which seems to solve stackoverflow.com/questions/34207981/…– NemoAug 18, 2016 at 14:07
Try this out:
# Real time mouse position.
watch -t -n 0.0001 xdotool getmouselocation
This will show your mouse location at "x" and "y" in real time as you move it. You can save your coordinates into a file for later referencing or to use in a script to automate those mouse movements in the following way:
# Save real time mouse coordinates to file.
while true; do xdotool getmouselocation | sed -e 's/ screen:0 window:[^ ]*//g' >> coordinates.txt; done
This^ will record only mouse coordinates into coordinates.txt. You can use each line in a script if you want to repeat the actions taken while recording. A simple ctrl+c
will do for ending the recording session.
This is just a small sample of how awesome and practical xdotool
can be for AFK automation and other things. Even custom bots :D
(Edit)
If you need to strip away the x:
and y:
from the sed
command, you can add the logical OR |
, while using the -E
option for extended regex, operator as follows:
xdotool getmouselocation | sed -E "s/ screen:0 window:[^ ]*|x:|y://g"
And if you want to use redirection and command substitution for a more compact command, you can use the following rather than a pipe:
sed -E 's/ screen:0 window:[^ ]*|x:|y://g' <<< $(xdotool getmouselocation)
As a disclaimer, the sed regex is written for GNU sed and may not work the same across different platforms or sed versions.
-
Thanks. Can you further strip
x:
andy:
away from coordinates.txt? How would you read them back and replay?– TimDec 5, 2018 at 16:21 -
What you meant by xdotool
not working?
What's the output of
xdotool getmouselocation
Anyway, if you can compile a C
program: http://dzen.geekmode.org/dwiki/doku.php?id=misc:xget-mouse-position
Regarding your comment below, you wrote you get:
Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665
I assume (in front of Windows XP) that you get it on two lines like:
Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info.
x:654 y:453 screen:0 window:1665
If that's the case, you should redirect STDERR
like:
xdotool getmouselocation 2>/dev/null
That would skip the warning.
If your only input is the cursos positon line then piping that to sed
will give you the coordinates like this:
xdotool getmouselocation 2>/dev/null | \
sed 's/x:\([0-9]\+\)[ \t]y:\([0-9]\+\)[ \t].*/\1;\2/'
# OUTPUT should by something like: "654;453"
If you want to use the coordinates (with bash
):
export COORDINS=`xdotool getmouselocation 2>/dev/null | sed 's/x:\([0-9]\+\)[ \t]y:\([0-9]\+\)[ \t].*/\1;\2/'`
export XPOS=${COORDINS/;*/}
export YPOS=${COORDINS/*;/}
HTH
-
I get Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665 Dec 12, 2011 at 23:03
-
-
Thanks! Can I set each X and Y to individual variable? Again, how would I sed that? Dec 14, 2011 at 13:05
-
new error
$ xdotool getmouselocation 2>/dev/null
x:720 y:439 screen:0 window:1665
Dec 14, 2011 at 17:17 -
If you're using xterm, you can issue an escape sequence ESC [ ? 9 h
which will make xterm send an escape sequence to the controlling program (i.e., bash) when you click with the mouse. I don't know if other terminal emulators have similar functionality.
Info on mouse tracking in xterm is at http://www.xfree86.org/current/ctlseqs.html#Mouse Tracking
-
2It would be handy to mention that
ESC [ ? 9 l
should get xterm out of this mode, so that your mouse works the way it's supposed to again. :)– ghotiDec 13, 2011 at 15:00
I get Warning: XTEST extension unavailable on '(null)'. Some functionality may be disabled; See 'man xdotool' for more info. x:654 y:453 screen:0 window:1665
So it IS working for you. You just need to parse the ouput of the command. You can use the sed script zsolt posted above, or a variety of other options:
xdotool getmouselocation 2>/dev/null | cut -d\ -f1,2 -
// returns something like "x:2931 y:489"
or
xdotool getmouselocation 2>/dev/null \
| awk 'BEGIN{RS=" ";ORS=RS} {split($0,a,":");} a[1]~/^[xy]$/{print a[2];}'
// returns something like "2931 489 "
or
xdotool getmouselocation 2>/dev/null | sed 's/ sc.*//; s/.://g; s/ /x/'
// returns something like "2931x489"
Plenty of ways to skin this cat.
xdotool getmouselocation
report? Debugging that may be easier than coming up with an alternative.