Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

4 Answers 4

up vote 6 down vote accepted

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.)

share|improve this answer
add comment

Simpler (IMO) than OP's solution (i.e. without ps, grep and awk), to get the process name :

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

Or if you want an end of line :

echo $(cat /proc/$(xdotool getwindowpid $(xdotool getwindowfocus))/comm)
share|improve this answer
    
Yes, cat /proc/ is nice (and arguably faster, since it doesn't involve grepping the whole ps output). I added it to solution in question body, so future users will be able to find it. –  Rogach Jul 6 '13 at 3:34
add comment

Try the xwininfo command, http://www.xfree86.org/4.2.0/xwininfo.1.html, it definitely returns the window title and as far as process goes, well ...

X has assigned it an ID and become the parent PID of the window and would also conceal it by default, so, assuming that Gnome has NET_WM_PID supported, as this patch from 2001 indicates it has, http://mail.gnome.org/archives/gtk-devel-list/2001-October/msg00238.html, then we can review this post, http://www.mail-archive.com/devel@xfree86.org/msg05809.html , where the author writes a short C program to convert Window ID into PID, voila.

share|improve this answer
    
But it seems that xwininfo requires me to manually select the needed window. I hoped for a way to get the focused window from bash. –  Rogach May 17 '12 at 19:30
    
Something as in, `xwininfo -root -children | grep -oEi 'Window id: (?[0-9a-zA-Z]+) | grep -oEi '(?0-9a-zA-Z)' -- edit: my finger slipped, my regex for window id was bad. check this other site for varying reference: davygoat.com/software/rizzle/How_it_works.html –  rhoyerboat May 17 '12 at 20:19
    
This command only gives the "root" window id, and not the active one :( –  Rogach May 18 '12 at 3:39
add comment

It is as simple as this:

xdotool getactivewindow getwindowname

Good luck hope it works for you!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.