📜 ⬆️ ⬇️

Automate mouse clicks on Linux: xdotool

This very short note on the example of key activation in 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 to activate on Steam. Or maybe 15 or even 25. You really don’t want to activate them manually, because it’s too dreary: in the Steam client, you need to move the cursor to the Games menu every time, click, then move the cursor to the menu item “Activate a Product on Steam” ... ”, click again, then press Enter, then again Enter, 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 at all for such a “progress” to descend from a palm tree and pick up a stone ax?

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

To begin with, you collect keys from a web page, to then write them into a text file, one per line. Naturally, not by hand. For example, from the Humble Budle page you can collect them by running something like this through the JS console:
')
$("div.keyfield:visible").each(function() {console.log($(this).text())}); 

It turns out a text file with something like this:

 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 add it automatically). Otherwise, when executing the script, the last key will not be activated.

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

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

That is, either start watch xdotool getmouselocation and move the cursor to the desired location on the screen, looking at the console and remembering the values ​​of the necessary points, or launch xdotool sleep 5 getmouselocation and get the coordinates of the point where the cursor is 5 seconds after the command is launched (respectively, you do not need to look into the console or remember the coordinates - it will be enough just to 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 38 , execute, look where the cursor was, and then change the numbers until you find the right ones. This option is more fun, so I think many would prefer to do just that.

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

To do this, we will use the mousemove, click, sleep, and key commands. The list of commands, by the way, can be found in the documentation for xdotool .

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 windowactivate / windowfocus commands so that the Steam window is displayed first, and then the following teams.

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 ID of the active window, you can use the getactivewindow command (if the Steam window is active a second after executing the xdotool sleep 1 getactivewindow, its identifier will be displayed in the console). In addition, for the getmouselocation command (see above), the identifier of the window above the cursor is also indicated.

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

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 

Allow the file to execute and run the script. In this case, 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, a good idea was to take that stone ax.

Fun programming!

Source: https://habr.com/ru/post/214779/


All Articles