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?
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
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.
x:
and y:
away from coordinates.txt? How would you read them back and replay? –
Provencher 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
$ xdotool getmouselocation 2>/dev/null
x:720 y:439 screen:0 window:1665
–
Sewing export ...
commands, then did something like: echo $XPOS $YPOS
then moved your cursor to a different position then reissued those export ...
commands and the echo ...
afterwards, and it did not changed? IMO that can't happen. Could you please paste your terminal lines to some pastebin service like gist.github.com and provide a link to see it? –
Panjandrum xdotool getmouselocation 2>/dev/null
in ONE line. I didn't type it line by line. The actual command is not working. –
Sewing x:720 y:439 screen:0 window:1665
–
Sewing 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
ESC [ ? 9 l
should get xterm out of this mode, so that your mouse works the way it's supposed to again. :) –
Hullabaloo 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.
© 2022 - 2024 — McMap. All rights reserved.
xdotool getmouselocation
report? Debugging that may be easier than coming up with an alternative. – Hullabaloo