Automating Ubuntu desktop with xdotool

Beginning from Ubuntu Intrepid Ibex, xdotool is added into the repositories. Xdotool is an application that allows you to control various X11 elements from the command line. In other words, you could change a window’s size, send keystrokes to an application, move and click the mouse, etc.

So what?

I could imagine that being able to do these things from the command line is an useful thing to have when running automated tests. And, I supposed xdotool is originally written with this in mind. However, the tool could also be useful for normal users to automate their tasks. It’s akin to having a macro facility for the desktop.

Companion tool

Xdotool has limited facility to query for information about opened windows. For that, you use another tool: xwininfo.

Installation

To install xdotool (for Intrepid only):

sudo apt-get install xdotool

To find out the command line options for xdotool:

man xdotool

Xwininfo is available by default, so there is no additional action required to install it.

Set a window to a specific size

I used xdotool to set a window to a specific size, before taking screenshots. Previously, I did this mostly by trial and error. I.e. do a capture; check it’s size; resize manually; repeat. As you can imagine this was time consuming and not a whole lot of fun. Using xdotool removes the trial and error, and provided direct and immediate result.

First, read the window ID.

xwininfo

The mouse cursor will turn into a plus. Left click on the window in question. Xwininfo will gives the relevant information. E.g. this is what I get for the calculator (gcalctool).

$ xwininfo

xwininfo: Please select the window about which you
          would like information by clicking the
          mouse in that window.

xwininfo: Window id: 0x4a00003 "Calculator"

  Absolute upper-left X:  717
  Absolute upper-left Y:  194
  Relative upper-left X:  717
  Relative upper-left Y:  194
  Width: 260
  Height: 309
  Depth: 24
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +717+194  -47+194  -47-97  +717-97
  -geometry 260x309-47-97

What we are interested in is the “Window ID: 0×4a00003″. To change the window size, I use the command in this format:

xdotool windowsize <window ID> <width> <height>

E.g. change to 500×400:

xdotool windowsize 0x4a00003 500 400

Automating windows arrangement on desktop

Previously, I posted about this here.

Now, instead of using the Compiz “Place Windows” plugin, I can use xdotool as another way to accomplish the same task. Though I have to admit, it’s a bit more convoluted.

#!/bin/bash

gnome-terminal --geometry=80x15 &
sleep 2
wid=`xdotool getwindowfocus`
wid=`xwininfo -children -id $wid | grep Parent\ window\ id | cut -c21-30`
xdotool windowmove $wid 0 0

gnome-terminal --geometry=80x20 &
sleep 2
wid=`xdotool getwindowfocus`
wid=`xwininfo -children -id $wid | grep Parent\ window\ id | cut -c21-30`
xdotool windowmove $wid 0 300

gnome-terminal --geometry=58x35 &
sleep 2
wid=`xdotool getwindowfocus`
wid=`xwininfo -children -id $wid | grep Parent\ window\ id | cut -c21-30`
xdotool windowmove $wid 600 0



Post a Comment

Your email is never published nor shared. Required fields are marked *