Okay, I'll admit it; I'm addicted to automation. A Roomba vacuums for me, my main router checks its DSL connection and automatically resets my DSL modem if it's down, my porch light is motionsensitive, and my bin directories are full of home-grown scripts I use to automate mundane computer tasks. There is something so satisfying when you can reduce a long series of steps down to a single script and just run that.
When most people think of automation with scripts, they think about the command line. After all, most scripts are concerned with standard command-line fare, such as pipes, simple logic, redirection and parsing text output. These days, much of the work on the desktop is done without a terminal, so it would be nice if you could automate some of those more mundane graphical tasks too. A tool called wmctrl can do exactly that. wmctrl provides a corrunand¬line interface to standard window management tasks, so you can resize and move windows, change desktops, toggle a sticky and rolled-up status on a window and much more, all from a shell script.
wmctrl is a common package in most modern distributions, so you should be able to install it with your distribution's package manager. Otherwise, you can obtain the source from wmctrl's main website (www.sweb. czltripielutilslwmctrl) and build it.
One of the great things about wmctrl is that it isn't window-manager-specific. It changes your windows via Extended Window Manager Hints (EWMH), and because most popular window managers these days (such as GNOME's Metacity, KDE's KWin, Compiz Fusion and Fluxbox) support EWMH, not only is wmctrl most likely to work with your window manager, but even if you decide to change to a different window manager, your wmctrl scripts probably will work just the same.
Quake terminal One of the best ways to illustrate the power of wmctrl is to create a script that turns a regular terminal into a Quake terminal. For those of you who haven't played any games from the Quake series, when you press the' key in Quake, a terminal pops down from the top of the screen so you can type commands. This type of terminal is very handy on a cluttered desktop, but you could even use this to create a type of 'boss button' to make a window disappear quickly.
In this example, I create a terminal that I've titled 'Quake Term', but you can change this script to work with the title of any window on your desktop. If you are unsure how wmctrl will view your window's title, run wmctrl with the -I option to show information about all the windows on your desktop:
The very last field in this output is the title of a particular window, and this is the information wmctrl OpenGuru,can use to identify windows for which you want to script actions. To create a basic Quake Term, you just need a single wmctrl command:
The option tells wmctTI the window title on which to act, and the -b option tells wmctrl either to add, remove or toggle up to two different window properties (in this case, the shaded state of my window). Th wmctrl man page lists all the available properties you can tweak with this and any other options.
Note that wmctrl scripts work best if windows have unique titles. If you have multiple windows open with the same title, you might not shade the right one. Each terminal sets its title differently, but on a G OME terminal, for instance, you can change the title within your profile settings (right-click on the terminal and select Edit Current Profile).
I use a modified version of the above command that not only shades the window, but also moves it to the back -below any of the other windows. The script also keeps track of the toggled state with a temporary [lie so that I can be sure the shaded and stacked states stay in sync: I simply bind Super+ ' to run the above script, and then I can toggle my terminal up and down with a quick key sequence.
Quake terminals are handy, but you can do much more powerful things with wmctrl. One of the most handy scripts I've created with wmctrl solves a problem I've had when I chat in IRC and browse the Web at the same time-it's
a pain to resize both windows so you can see both, just to resize them back when you are done chatting or browsing. wmctrllets you resize and move windows, provided you know how to describe the new window location and geometry. With this in mind, I've created a script that toggles between two states: normal mode and chat mode. In chat mode, my IRC window shrinks and moves so that it sits in a narrow strip at the top of the screen, and my Web browser resizes to be shorter so I can see both windows at the same time. Then, I can run the script again, and the windows move back to their normal locations.
To create the script, first arrange your two windows (in my example, one with 'lrssi Term' in the title and one with 'Firefox' in the title) how you normally want them, and then run a special wmctrl command to list all the windows on your desktop along with their geometry and size information:
your respective wmctrl commands, you can throw them in a script very similar to the one 1 used earlier, for the Quake terminal:
In this output, the -G option adds four extra columns in the midcUe. These columns represent the x-offset, y-offset, width and height, respectively. So, in the case of Firefox, the x-offset is 6, the y-offset is 96, the width is 1040,
and the height is 708. Jot down these values For the two windows you want to script, and then resize and move them to reflect your 'chat mode'. Next, run the command again and jot down the new values.
wmctrl provides the -e argument that allows you to modify the position and size of a window. The argument actually takes five integer values in a row-g,x,y,w,h-where g is the gravity of the window (usually put 0 here), x and yare the x and y coordinates for the top-left corner of the window, and wand h are the width and height, respectively. So, if I had moved my Firefox terminal and wanted to move it back to the above coordinates, I would run the following:
If you look carefully, you might notice I changed the coordinate to 0 instead of 96. I've found that in some window managers, the geometry the window manager reports to wmctrl is different from reality. Basically, with a little trial and error, you can tweak the coordinates so that everything lines up just right. Once you are satisfied with noticed that with the current window manager (Compiz), when I ran this command, some bug-either in wmctrl or, more likely, in the window manager-caused Firefox to move from my second desktop to my current desktop.
If this happens to you, there's a simple fix. Simply add the following line above the if statement in the script: wmctrl has commands both for shifting to different desktops and to different viewports. Because Compiz often uses multile viewports instead of desktops, the above command moves me to the second viewport (my desktops are 1280x768, so 1281,0 corresponds to the top corner of my second viewport).
wmctrl has a lot of power. I recommend looking at its man page and reading about the large number of available options. The real power in wmctrl, however, lies in your ability to imagine new and interesting ways to script vvindow manager actions. mrnf\.
By: Kyle Rankin is a senior systems administrator in the San Francisco Bay Area and the author oj a number oj books, including Knoppix Hacks and Ubuntu Hacks jor O'Reilly Media. He is currently the president oj the North Bay Linux Users' Group.



Reply With Quote
Bookmarks