Activate particular X11 windows via a hotkey

Oct 26, '05 06:10:00AM

Contributed by: Pausanias

One of the issues with using X11 under OS X is that the default window manager, quartz-wm, does not allow you to manipulate the properties of the various X windows very much. The biggest problem is that it is not possible to use a hotkey to bring to front and activate the exact X11 window that you want -- you have to resort to several mouse clicks on the Dock or on the Window menu. Most users are fine with having to use the mouse, but many people aren't -- we want to be able to assign a hotkey to everything. If you are happy using the mouse to switch X11 windows, read no further.

For the rest of you, here's a better description of the problem. Imagine that you have dozens of X11 windows open, and are working in the Finder. Now you want to use a hotkey to switch to a specific window, say emacs, while leaving all the other X11 windows alone. You'll quickly find that this is difficult to do.

The closest solution? Assign a hotkey to X11 using Peter Maurer's Butler, bringing up all the X11 windows at the same time, and forcing you to spend time wading through the mess so you can locate the one emacs window you really want. Even within X11, you first have to lookup which hotkey emacs is assigned to under the Window menu. What's worse, the hotkey assigned to a particular window under that menu changes as you open and close windows, so that it's impossible to keep a specific hotkey consistently assigned to a program. The Applications menu is useless for this purpose, because it always runs a new version of the program, instead of raising a previously active window. A drastic solution would be to switch window managers (to fvwm or something else), but then you would lose the pretty and fast quartz-wm graphics.

To the rescue comes a little program called xwit, plus Butler. The rest of this hint assumes that you aren't afraid of xterms and that you know how to assign hotkeys using Butler. Read on for the solution...

First, open the Terminal. You have to download, compile, and install xwit from the source, and you can do so through the following commands:

$ wget
$ tar xvzf xwit_3.4.orig.tar.gz
$ cd xwit-3.4.orig
$ xmkmf
$ make
$ sudo mv xwit /usr/local/bin
You will have to type your passwrod. Users of tcsh (you know who you are) will also have to add rehash after they are done with the above. Now launch X11, and as an example, pop up an xeyes window:
xeyes &
Now minimize the xeyes window to the Dock by clicking on the yellow "minus" button on the title bar. Then, in an xterm type this:
/usr/local/bin/xwit -current -names xeyes
You will see that it will come back up to the front. If you had other X11 windows minimized, they would not come up -- only the xeyes window would have come up. This works with hidden and buried windows as well. For advanced usage, type xwit --help. Below we will use the window ID rather than -names, so that we know we are raising the right application, regardless of the window title.

Now we need to assign a hotkey to xwit. In Butler, create an AppleScript Smart Item. In the AppleScript source code, type the following:
do shell script "id=`/usr/X11R6/bin/xwininfo -root -children  
-tree | grep xeyes | /usr/bin/awk '{ print $1; exit; }'`;  
/usr/bin/open -a X11; /usr/local/bin/xwit -current -id  $id; 
/usr/local/bin/xwit -warp 1 1 -id $id"
Type in the above without any line breaks (put spaces where you see line breaks). Now assign a hotkey to that item. Now, no matter where you are on your Mac, you can press that hot key, and xeyes will come up to the front and be activated, without disturbing your other X11 windows. To activate a different X11 application, just change xeyes above to the application's name.

A technical note: I was not able to get the run shell script option working in latest version of Butler to run under Panther. Hence I resorted to the AppleScript solution. Also, Peter's Maurer's Witch does not seem to recognize individual X11 windows, so that's out as a possible solution.

Comments (12)

Mac OS X Hints