In the classic world of desktop automation, “macros” allow you to repeat a task easily. In general, xte
is the best way of scripting this up on Linux.
First, you need to install it:
|
|
To use xte
, you need to send it information via a ‘pipe’. The man
page covers the key codes and commands in detail, but I’ll step through some basic examples below.
Examples
I’ve put together a couple of interesting examples to show some uses for this tool.
Example 1: Do a Google search
The example below uses xte
to type “Hello world” into a text box.
Type the command, press enter, then quickly click on the text box:
|
|
Example 2: Open a browser and search Wikipedia
To write a script which combines a few commands for xte
, you could put it in a bash
script. Remember to put a pause between commands so that the windowing system can catch up.
The script below will use Gnome Shells “overview mode” to launch Chromium, then open a new tab, and search Wikipedia for “cars”:
|
|
Example 3: Draw a spiral in GIMP
When you script an operation, you can interact with any program using a set of rules. Below is a PHP script called spiral.php
, which draws and labels a spiral in GIMP, switching the foreground & background colours at each step.
This requires an open GIMP window in the correct part of the screen:
The interaction in this case is quite simple for a computer, but would be tedious to do manually:
- N
- Open the Line tool
- T
- Open the Text tool
- X
- Swap foreground & background colours
- Click & Drag
- Draw a line
|
|
After cropping, the spiral image on its own is:
Example 4: Forward emails
Google’s Gmail web interface has keyboard shortcuts for quick navigation. This example uses:
- f
- Forward an email
- Tab
- Move between fields
- Ctrl+Enter
- Send
- j
- Next email
Using these shortcuts, this script, forward.txt
, will forward an email too bob@email.com
and fred@example.com
, then navigate to the next email:
|
|
To send one email through xte
, you could run this, and then click over to an open email in Gmail:
|
|
To send the next 106 emails (e.g. in a label or search), you could instead type:
|
|
This is not fool-proof, so you would need to adjust the timing if your Internet connection is laggy.
When to use xte
Usually, a task which is supposed to be automated will have an API. For example, GIMP provides a python plugin interface, Gmail can be accessed via IMAP, and Google and Wikipedia searches can be done directly through HTTP. This is always the best way to do things.
However, the automation junkie should have xte
in their toolkit, for situations where proper automation is not practical, such as when:
- you don’t want to learn an API, to only use it for one day.
- you’re doing a repetitive task in a program or website which is feature-poor.
- you need to test some feature repeatedly under different circumstances.
- you find a game which requires you to click fast.