How would I get the current mouse coordinates in bash?

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


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

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


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.