Obtaining a window title given only a process ID (PID)
Tagged:  •    •  

It's not that obvious to obtain the window title when you only have the process ID (PID) of the process showing that window. xprop(1) and xwininfo(1) didn't work for me, because these were interactive commands. Finally I found the wmctrl(1) tool, which should work fine with most major window managers (KWin for example).

The following command obtains the window title of the process with PID 1234 and having a window managed by your window manager:

wmctrl -l -p | grep 1234 | sed -e "s/.*`hostname` //"

-l is for listing all managed windows, -p is for listing the corresponding PID as well. A simple grep and some post processing makes you obtain the window title.