As engineer I thrive to make computing simple and efficient as possible. I came across a rather nasty problem that requires some seriously time consuming and monotonous manual labour. Well that or some automation. Needless to say I chose the latter.
The Problem
Very simple task that had to be repeated thousands of times. This example is not the actual problem/solution that I had, but sufficient for this tutorial. Here’s a flowchart (without the chart) that shows the simplicity of the problem.
1. mouse right-click
2. move mouse x+3, y+3
3. mouse left-click
4. move mouse x-3, y-3 (reset)
5. goto 1.
I had customized the GUI of my program, so that the notes above works.
The GUI approach.
There are plenty of GUI’s available and I tried few, but none was good enough. gnee, available from the Ubuntu Software Centre, gnee is a GUI for xnee. The program started, but gave errors. After fixing the initial errors it gave me fresh ones, then it hung and I stopped messing with it. The program may not be at fault as I do have a slightly… unorthodox rig, with enlightenment’s seven virtual desktops and such. Perhaps it likes pure GNOME better? Next I downloaded the LinuxAutoMouseClick package from that site, read the read me where it states “try before buy … $10 ” *pfff!* nope, not going to pay for a problem this simple..
The actual solution
Then I stumbled upon xdotool, also available from the USC. This nice little tool lets you write the desired actions into terminal. for example, to do “mouse right-click” you’d type:
$ xdotool click 1
That checks #1 off from the problem list! Try it now, put your engineer’s hat on and read the man pages. The commands –sync and mousemove_relative are of interest to me. Sync makes sure that the script doesn’t send actions at the wrong time if the program doesn’t respond immediately and I can see no harm adding if in anyway and the mousemove_relative is self explanatory.
Seeing that there are multiple steps to take to solve the problem, a bash script is required. I saved the following as auto_mouse.sh.
COUNTER=$1
until [ $COUNTER -lt 1 ]; do
xdotool click 3
xdotool mousemove_relative --sync 3 3
sleep 1
xdotool click 1
xdotool mousemove_relative --sync -- -3 -3
let COUNTER-=1
done
That it! The script takes one parameter (times to loop) with no error checking. While the script is running it will do exactly the required events to solve the problem. I had to add sleep, which is a Linux command that makes the system idle for 1 second. It’s a bit of an overkill, but since the resolution is 1 second, there’s nothing I can do. And now I have something to watch as I drink my coffee.
Conclusion
My initial thought were correct. It is possible to write a simple automation script with the proper tool. No GUI’s are necessary, however with my previously acquired skills in designing GUI’s, it would be interesting project to take.