Shell scripting your desktop windows

In Java my limits are often what is offered in every OS - and what is inside the JVM.

One thing, I often needed in the past under Windows is to get the title (or other information) of the active window. I was using Windows API to get the information needed. Under Linux/Ubuntu things are different - and more complicated due to different desktop environments, I thought. And how to access those APIs with Java?

Well, it turned out, that I am completely wrong and once again was trapped in my old habits from Windows.

Linux offers commandline tools - not only to get window information - no, also to manipulate windows, move them to different desktop etc. As I could start an application on a different machine and showing the window on my machine, I can either see the machine name in window lists. A commandline program can be easily used from within Java and output can be retrieved easily. Awesome!

It again turns out, that Microsoft Windows is totally poor in commandline tools. However, I could implement a similar small exe that gets me the active window title through API on windows and does console output that can be retrieved by a Java application or can be used in batch files.

If you are a Linux user, have a look at the following commands:
  • wmctrl
  • xprop
  • xwininfo
All these tools can be used even from within shell scripts - awesome!

With xprop -root you get exhaustive information about windows.
Using xprop -root | grep _NET_ACTIVE_WINDOW\(WINDOW\) you get the active window handle. You can then use the address in wmctrl -l | grep theaddress. The only quirks is that in wmctrl the address might have an additional 0 after 0x so you should search for the part after 0x only in the output of wmctrl.

1 comment:

Martin Wildam said...

The even better way instead of using wmctrl is to use

xprop -id theaddress | grep WM_ICON_NAME\(STRING\)