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?

cigien
  • 57,834
  • 11
  • 73
  • 112
t3hcakeman
  • 2,289
  • 4
  • 25
  • 27

5 Answers5

72

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
Lighty
  • 736
  • 6
  • 3
  • Now the command also outputs WINDOW=... , which seems to solve http://stackoverflow.com/questions/34207981/how-do-you-get-window-id-for-xdotool-automatically – Nemo Aug 18 '16 at 14:07
33

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.

Yokai
  • 1,170
  • 13
  • 17
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

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 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 – t3hcakeman Dec 12 '11 at 23:03
  • How would I awk/grep/sed that? – t3hcakeman Dec 12 '11 at 23:04
  • Thanks! Can I set each X and Y to individual variable? Again, how would I sed that? – t3hcakeman Dec 14 '11 at 13:05
  • new error `$ xdotool getmouselocation 2>/dev/null` `x:720 y:439 screen:0 window:1665` – t3hcakeman Dec 14 '11 at 17:17
  • Sorry could you specify your problem properly? – Zsolt Botykai Dec 14 '11 at 17:24
  • The outputted coordinates do not change based on mouse movement – t3hcakeman Dec 14 '11 at 17:25
  • The coordinates change every time you run the export commands. Not automatically. – Zsolt Botykai Dec 14 '11 at 17:41
  • No, I'm talking about actually typing the command in terminal. It does not change. – t3hcakeman Dec 14 '11 at 17:52
  • 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 https://gist.github.com/ and provide a link to see it? – Zsolt Botykai Dec 14 '11 at 18:33
  • 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. – t3hcakeman Dec 14 '11 at 19:54
  • I just said. The value of X: and Y: do not change at all and are always `x:720 y:439 screen:0 window:1665` – t3hcakeman Dec 15 '11 at 00:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5861/discussion-between-t3hcakeman-and-zsolt-botykai) – t3hcakeman Dec 15 '11 at 01:27
  • 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. – Zsolt Botykai Dec 15 '11 at 08:59
  • The mouse coordinates do not change – t3hcakeman Dec 15 '11 at 13:37
2

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

evil otto
  • 10,348
  • 25
  • 38
  • 2
    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. :) – ghoti Dec 13 '11 at 15:00
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.

ghoti
  • 45,319
  • 8
  • 65
  • 104