A Scala shell script to move your mouse cursor

I’m currently trying to automate a GUI task, and as a part of that, one thing I need to do is move the mouse cursor.

In short, the solution I came up with was to write a Scala shell script that uses the Java Robot class to move the mouse. Here’s the source code for my script, which I named MoveMouse.sh:

#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#

import java.awt.Robot

if (args.length != 2) {
    Console.err.println("Usage: MoveMouse x y")
    System.exit(1)
}

val x = args(0).toInt
val y = args(1).toInt

val robot = new Robot
robot.mouseMove(x, y)

You run the script like this:

$ MoveMouse.sh 5 10

That moves your mouse cursor to a position where the x-coordinate is 5 and the y-coordinate is 10. This is in the upper-left corner of your display. (I could be nicer and move the mouse with a little animation, but I won’t be watching the screen as the automation task runs, so with this script the mouse just jumps to the location you specify.)

I almost always work on Mac OS X systems, but I couldn’t find a way to do this with AppleScript, so I wrote this little shell script to solve the problem.