Back to Home

Automate mouse clicks on Linux: xdotool

xdotool · xclip · automation · Steam · Linux

Automate mouse clicks on Linux: xdotool

  • Tutorial
This very short note on the example of activation of keys on Steam describes the process of automating operations performed using the mouse and keyboard.

Let's say you bought several sets of Humble Bundle games. Now you have, for example, 5 keys for activation on Steam. Or maybe 15 or even 25. You really do not want to activate them manually, because it is too dreary: in the Steam client you need to hover over the Games menu each time, click, then hover over the menu item "Activate a Product on Steam ... ”, click again, then press Enter, then Enter again, and only then finally enter the key (and then you need to wait, press Enter again, then Escape). And then repeat the same for each subsequent key. As Leonid Kaganov wrote, was it worth it to go down from a palm tree and pick up a stone ax for the sake of such "progress"?

In general, you decided to automate this process - especially since the task is, in fact, very simple. To solve it, we need the console utilities xdotool and xclip - make sure that they are installed on your system.

First, you collect keys from a web page, then write them to a text file, one per line. Naturally, not by hand. For example, from the Humble Budle page, you can collect them by doing something like this through the JS console:

$("div.keyfield:visible").each(function() {console.log($(this).text())});

It turns out a text file with approximately the following contents:

9MZ43-42XXZ-0B9X3
I4YYK-CRGVN-VHXCR
NQJ6E-GJWNG-GZWVX
YCKI8-I0B9T-85CM4
KBFHW-5LE39-WHFMW
WFLWX-PPRBT-ZCGAN
ER26C-XFT5C-2NDGG
J876-XPFC-H0SF-KGMO
37YZQ-93TCM-V9MBY
2GFNA-XHBME-3MB70

When saving a text file, make sure that there is a line break at the end (some text editors do not automatically add it). Otherwise, when the script is executed, the last key will not be activated.

Next, you need to open Steam and determine the coordinates of those points where you will need to automatically move the cursor. There are two such points: firstly, the point for the first click (menu "Games"), and secondly, the point for the second click (menu item "Activate a Product on Steam ..."). Here's the mood - you can get the coordinates using the getmouselocation command, or you can pick them up.

In the first case, you can either run xdotool through watch (to automatically over-execute the command at a certain time interval), or add sleep.

That is, either runwatch xdotool getmouselocationand move the cursor to the desired location on the screen, while looking at the console and memorizing the values ​​of the necessary points, or start xdotool sleep 5 getmouselocationand get the coordinates of the point where the cursor appears 5 seconds after the command starts (accordingly, you do not need to look at the console or remember the coordinates - it will be enough just have time to place the cursor in the right place, and only then switch to the console).

In the second case, you write, say, xdotool mousemove 52 38execute, look where the cursor is, and then change the numbers until you find the right ones. This option is more fun, so I think many will prefer to do just that.

Now that we have the necessary coordinates, let's act iteratively: first of all, we will try to automatically go through until the key is entered.

For this, we will use the mousemove, click, sleep, and key commands. A list of commands, by the way, can be found in the xdotool documentation .

Let's try:

xdotool mousemove 210 105 click 1 mousemove 210 160 sleep 0.1 click 1 sleep 0.1 key Return sleep 0.1 key Return

If at the point where the cursor moves first, not Steam, but some other window (for example, if Steam is hidden behind the terminal window), then you need to add the windowactivate / windowfocus commands so that the Steam window appears first, and already then the following commands were executed.

For example:

steam_window=48234551 ; xdotool windowactivate $steam_window windowfocus $steam_window sleep 0.5 mousemove 210 105 click 1 mousemove 210 160 sleep 0.1 click 1 sleep 0.1 key Return sleep 0.1 key Return

To get the identifier of the active window, you can use the getactivewindow command (if the Steam window is active a second after the commandxdotool sleep 1 getactivewindow,then its identifier will be displayed in the console). In addition, for the getmouselocation command (see above), the identifier of the window above which the cursor is located is also indicated.

Now let's try to write a small script that reads the file with the keys and activates them one by one.

The script can be called, for example, activate_steam_keys.sh .

#!/bin/bash
commands=(
    "sleep 1"
    "mousemove 210 105"
    "click 1"
    "mousemove 210 160"
    "sleep 0.1"
    "click 1"
    "sleep 0.1"
    "key Return"
    "sleep 0.1"
    "key Return"
    "sleep 0.1"
    "key ctrl+v"
    "sleep 0.5"
    "key Return"
    "sleep 10"
    "key Return"
    "sleep 0.5"
    "key Escape"
)
while read key
do
    echo -n $key | xclip -selection c
    xdotool ${commands[*]}
done < $1

We allow execution for the file and run the script. At the same time, we pass the argument to the script - the name of the file with the keys.

chmod +x activate_steam_keys.sh
./activate_steam_keys.sh steam_keys.txt

And then just look at the screen and enjoy the process. Still, it was a good idea to take that stone ax.

Happy programming!

Read Next