Gnome – How to Retrieve the Active Window Process/Title

gnomeguiwindow

I need a solution for getting the current active (focused) window information on a Gnome 2 desktop. I'm mostly interested in the process running that window and window title.

Is it possible?

SOLUTION:

Getting window title:

xwininfo -root -children | grep $(printf '%x\n' $(xdotool getwindowfocus)) | grep -oEi '"[^"]+"' | head -1

Getting process name:

ps -e | grep $(xdotool getwindowpid $(xdotool getwindowfocus)) | grep -v grep | awk '{print $4}'

or:

cat /proc/$(xdotool getwindowpid $(xdotool getwindowfocus))/comm

Best Answer

You can use xdotool, a versatile X window automation tool.

focused_window_id=$(xdotool getwindowfocus)
active_window_id=$(xdotool getactivewindow)
active_window_pid=$(xdotool getwindowpid "$active_window_id")

(I don't know what the difference between focused and active is.)

(I thought wmctrl could do this, but apparently not.)

Related Question