48

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
Rogach
  • 6,150
  • 11
  • 38
  • 41

5 Answers5

56

It is as simple as this:

xdotool getactivewindow getwindowname

Good luck hope it works for you!

Anthon
  • 78,313
  • 42
  • 165
  • 222
eric
  • 561
  • 1
  • 4
  • 2
27

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

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
9

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 06 '13 at 03:34
8

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'

jjj
  • 211
  • 2
  • 4
6

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/[email protected]/msg05809.html , where the author writes a short C program to convert Window ID into PID, voila.

rhoyerboat
  • 361
  • 2
  • 5
  • 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: http://www.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 03:39
  • As of 2022, `xwininfo -root -tree` will work – ATorras Jan 07 '22 at 23:45