Automation With xdotool

MzQ1NjExN2

Well-Known Member
Joined
Jul 10, 2024
Messages
491
Reaction score
424
Credits
3,825
I recently started using xdotool command for automating things. Its very easy to create scripts and have it do stuff for you. I've created some draft scripts for work where I have to fill out repetitive documentation in a browser. It fills out forms that take up to 10-20 minutes in less than five seconds. So far with draft scripts I should be able to shave off 20 minutes from my work day. Hopefully this will be up to 2 hours/day (maybe more) shaved off once I've created all the scripts I can possibly create with this command.

I told my coworker about this who is using a Windows laptop provided by the company and he is interested in me setting up a Linux laptop for him just for this.

"On the other hand, learning the Linux command line is extremely rewarding. If you think
you're a “power user” now, just wait. You don't know what real power is — yet. And, un-
like many other computer skills, knowledge of the command line is long lasting. The
skills learned today will still be useful 10 years from now. The command line has sur-
vived the test of time."

Quote from "The Linux Command Line" by William Shotts:
 


I started dabbling with xdotool a year or so ago but haven't fully explored it yet. Right now my main use for it is to keep the screen alive on an old netbook. About half the time, when the screen blanks, the graphics (X) goes south when you try to wake it up. The machine doesn't crash, but you can't recover it unless you have another machine handy from which to log in via ssh - which I usually don't have. (No, the magic keystrokes to get to the console don't seem to work) A little scripting with printidle and xdotool to jiggle the mouse before screen blanking would take place and all's well.

As far as automating tasks is concerned, that's not a big use case for me, but xdotool hasn't let me down yet. There may be ways to use cursor positioning relative to a specific window but I haven't got there yet (and likely won't) but when I've used it with, for instance, a browser, I've made sure to keep the browser window in the same position and the same size all the time and just use screen-based coordinates to position the mouse cursor where needed - so if I inadvertently move or resize the browser or if another window overlays it, it can cause problems.

I did notice that in a click-heavy scenario, mouse handling seems to bog down over time.
 
Its an awesome program. Its taking my computing to another level. It looks like you can program it do anything that you would use your mouse and keyboard for.

I'm just thinking how many thousands of hours I would have saved if I knew about this earlier.

 
Here is an easy script that would be useful for anyone.

Create file:
Code:
sudo nano update.sh

Copy and paste the following (Return is used for Enter key):
Code:
#!/bin/bash

xdotool key ctrl+T
sleep 1
xdotool type "sudo apt-get update && sudo apt-get upgrade"
xdotool key Return

Make file executable:
Code:
chmod a+x /home/user/update.sh

Assign the file to a keyboard shortcut and your good to go. A bash script would be faster when using terminal commands but this is just FYI to show how xdotool works.
 
Last edited:
As far as automating tasks is concerned, that's not a big use case for me, but xdotool hasn't let me down yet. There may be ways to use cursor positioning relative to a specific window but I haven't got there yet (and likely won't) but when I've used it with, for instance, a browser, I've made sure to keep the browser window in the same position and the same size all the time and just use screen-based coordinates to position the mouse cursor where needed - so if I inadvertently move or resize the browser or if another window overlays it, it can cause problems.
For the forms I was filling out in a browser I came across this issue as well. I was using the mouse function of xdotool initially and had to have the window in a specific position so the script would start in the right place on the browser. I found using the xdotool key Tab command to be better. You simply have the browser open and then you use Tab until you get where you want so then you don't need the mouse and the success rate is 100% provided your in the right starting point as far as Tab is concerned.
 


Follow Linux.org

Members online


Top