24

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
17

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

24

It is as simple as this:

xdotool getactivewindow getwindowname

Good luck hope it works for you!

  • This works, can anyone explain why it was downvoted? – lfzawacki Feb 12 '15 at 4:02
7

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)
  • 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
4

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.

  • 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
1

I know the question is old, but I feel xprop also should be mentioned here. It's readily available under X. It can be either used in an interactive way:

  1. type xprop and select the window you want using mouse cursor, then
  2. WM_NAME gives you the title of the window, _NET_WM_PID gives the pid

Or you can directly tell xprop which window you need by passing -id or -name option. Using awk you can get the active window id and pass it back to xprop like that (taken from here):

xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}")

Finally, using Your Favourite Tool™ (e.g. grep or sed) you can grep-out the needed values. For example for pid the output of the above command can be piped to sed: sed -nE 's/^_NET_WM_PID.*= ([0-9]+)/\1/p'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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