Is it possible to retrieve the active window process/title in Gnome?

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 '%xn' $(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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It is as simple as this:

xdotool getactivewindow getwindowname

Good luck hope it works for you!

Method 2

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

Method 3

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)

Method 4

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'

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x