12

It often happens to me, that an application pops up a question and I wonder to which exact application this window belongs, because if I look in the Launcher there is no application that currently has focus (no white triangle on the right side of the icon when I focus the message window)

Is there a way to find out the belonging of an open window?

5 Answers 5

14

Using xdotool

First make sure xdotool is available on your system:

sudo apt-get install xdotool

The following command will print the process name of the window currently in focus:

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

To give yourself more time to focus the window / click on it you can prepend a small sleep duration:

sleep 5 && cat "/proc/$(xdotool getwindowpid "$(xdotool getwindowfocus)")/comm"

The process name should be displayed after a short amount of time.


Using wininfo

Wininfo is a graphical utility that displays various information on windows and their properties, including the PID (process ID) associated with the window:

image of PID in wininfo

wininfo should be available in the official repositories:

sudo apt-get install wininfo

Having determined the PID of the window you can then look up the process name associated with it. There are various ways to do this, e.g. by looking at /proc:

$ cat /proc/17002/comm
gnome-terminal

This would be the process name associated with the PID 17002.

A more elegant way that allows inspecting the process tree context, as suggested by @Rmano:

$ pstree -a -s -l -p -u 17002
init,1
  └─lightdm,1900
      └─lightdm,3202 --session-child 12 19
          └─lxsession,3307,glutanimate -s LXDE -e LXDE
              └─openbox,3362 --config-file /home/glutanimate/.config/openbox/lxde-rc.xml
                  └─gnome-terminal,17002
                      ├─bash,1841
                      ├─bash,2332
                      ├─bash,2424
                      │   └─pstree,2484 -a -s -l -p -u 17002
                      ├─gnome-pty-helpe,1840
                      ├─{gnome-terminal},1835
                      ├─{gnome-terminal},1836
                      ├─{gnome-terminal},1842
                      └─{gnome-terminal},2269

Of course you can also combine pstree with the xdotool option above (thanks to @rubo77 for pointing this out!):

sleep 2; pstree -spaul $(xdotool getwindowpid "$(xdotool getwindowfocus)")

Sources:

https://unix.stackexchange.com/q/38867/29245

http://www.linuxquestions.org/questions/debian-26/how-to-find-the-process-associated-with-a-top-level-x-window-907125/

https://superuser.com/q/632979/170160

8
  • 1
    +1 for the nice answer. If the PID is of a child of the main application, you can suggest the use of pstree -a -s -l -p -u $PID which is quite nice...
    – Rmano
    Aug 8, 2014 at 9:48
  • @Rmano: Oh, that's neat! I added it to the answer. Aug 8, 2014 at 10:04
  • The echo $() in your commands isn't necessary. Aug 8, 2014 at 15:08
  • @DennisWilliamson Thanks for the pointer. Edited. Aug 8, 2014 at 15:20
  • 1
    xdotool getwindowpid "$(xdotool getwindowfocus)"xdotool getwindowfocus getwindowpid
    – Veedrac
    Aug 14, 2016 at 1:05
3

You can create a keyboard-shortcut with this command:

zenity --info --text $(xprop $win_id WM_CLASS | cut -d" " -f4-)
6
  • 1
    That's a very nice solution, +1. However, one smaller issue: if the application doesn't assign a proper WMCLASS (which is oftentimes the case with cryptic dialogs), xprop won't be of any help. Aug 8, 2014 at 9:02
  • Did you have an example for an application (popup) that didn't work, thank you.
    – TuKsn
    Aug 8, 2014 at 9:06
  • 1
    I don't have anything specific at hand but I do remember having issues with several Java applications and other obscure programs that didn't have WMCLASS set (or set to something generic like java-window). I ran into these kinds of issues a while ago when I was trying to implement application-specific rules in my window manager configuration. Aug 8, 2014 at 9:09
  • 1
    You are right there is a similar problem with apps created with qml/qt -> bugreports.qt-project.org/browse/QTBUG-29396
    – TuKsn
    Aug 8, 2014 at 9:24
  • How do you create a keyboard-shortcut for this? I tried, as shown below, but zenity doesn't show up
    – rubo77
    Apr 15, 2015 at 11:22
2

You can use xprop to get the Process ID of the clicked Window:

xprop $win_id _NET_WM_PID

then analyze it with pstree:

pstree -spaul $(xprop $win_id _NET_WM_PID | cut -d" " -f3-)

This you can redirected to a text-info window with

pstree -spaul $(xprop $win_id _NET_WM_PID | cut -d" " -f3-)|zenity --text-info --title "pstree of clicked Window"

I tried to create a keyboard-shortcut for this last command (if you need it regularely), but for some reason This doesn't work.

0

With xdtool and yad installed you can have a nice GUI App.

#!/bin/bash
while true; do cat "/proc/$(xdotool getwindowpid "$(xdotool 
getwindowfocus)")/comm"; sleep 1; echo -e '\f'; done | yad --no- 
buttons --no-focus --geometry 250x10 --fixed --text-info -- 
title="Active window" --listen
0

If you are using gnome then can use the following solution.

Full Disclosure: The github link bellow is my project.

Please look at https://github.com/blueray453/gnome-utils-by-blueray453/blob/main/appFunctions.js

GetAppFromFocusedWindow() {
    let w = global.get_window_actors().find(w => w.meta_window.has_focus() == true);

    let tracker = Shell.WindowTracker.get_default();
    let app = tracker.get_window_app(w.meta_window);
    return app.get_id();
}

You need gnome extension to access data about windows and shell. You see the global. and Shell.WindowTracker, you can not import these is gjs or python.

Please check Could not find Meta, Shell & St in Python's GTK bindings which gnome extension documentaion has.

If you install this extension then the command you would run is:

dbus-send --print-reply=literal --session --dest=org.gnome.Shell /org/gnome/Shell/Extensions/GnomeUtilsApps org.gnome.Shell.Extensions.GnomeUtilsApps.GetAppFromFocusedWindow

You can bind this command to a key.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .