How would I get the current mouse coordinates in bash?
Asked Answered
S

5

44

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?

Sewing answered 12/12, 2011 at 19:56 Comment(2)
From where? An X server? Curses? ...Enuresis
What error does xdotool getmouselocation report? Debugging that may be easier than coming up with an alternative.Hullabaloo
C
74

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
Carinacarinate answered 17/12, 2012 at 2:16 Comment(1)
Now the command also outputs WINDOW=... , which seems to solve #34208481Ninety
K
34

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.

Kaila answered 15/5, 2016 at 12:8 Comment(2)
Thanks. Can you further strip x: and y: away from coordinates.txt? How would you read them back and replay?Provencher
Yeah sure. I edited the answer to reflect your request.Kaila
P
4

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

Panjandrum answered 12/12, 2011 at 20:43 Comment(14)
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:1665Sewing
How would I awk/grep/sed that?Sewing
Thanks! Can I set each X and Y to individual variable? Again, how would I sed that?Sewing
new error $ xdotool getmouselocation 2>/dev/null x:720 y:439 screen:0 window:1665Sewing
Sorry could you specify your problem properly?Panjandrum
The outputted coordinates do not change based on mouse movementSewing
The coordinates change every time you run the export commands. Not automatically.Panjandrum
No, I'm talking about actually typing the command in terminal. It does not change.Sewing
So you had typed all three 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
O_O All I am saying is I typed xdotool getmouselocation 2>/dev/null in ONE line. I didn't type it line by line. The actual command is not working.Sewing
I just said. The value of X: and Y: do not change at all and are always x:720 y:439 screen:0 window:1665Sewing
let us continue this discussion in chatSewing
Sorry but I'm rarely available for chat. Please do the previously mentioned terminal commands, and paste the output somewhere and link it to here.Panjandrum
The mouse coordinates do not changeSewing
V
3

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

Vardar answered 13/12, 2011 at 0:48 Comment(1)
It 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. :)Hullabaloo
H
1

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.

Hullabaloo answered 13/12, 2011 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.