Is it possible to retrieve the active window process/title in Gnome?
Solution 1
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.)
Solution 2
It is as simple as this:
xdotool getactivewindow getwindowname
Good luck hope it works for you!
Solution 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)
Solution 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:
- type
xprop
and select the window you want using mouse cursor, then -
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'
Solution 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.
Related videos on Youtube
Rogach
Updated on September 18, 2022Comments
-
Rogach almost 2 years
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 about 12 yearsBut it seems that
xwininfo
requires me to manually select the needed window. I hoped for a way to get the focused window from bash. -
EnasAZ about 12 yearsSomething 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
-
Rogach about 12 yearsThis command only gives the "root" window id, and not the active one :(
-
Rogach almost 11 yearsYes,
cat /proc/
is nice (and arguably faster, since it doesn't involve grepping the wholeps
output). I added it to solution in question body, so future users will be able to find it. -
ATorras over 2 yearsAs of 2022,
xwininfo -root -tree
will work