How to find the program name of an X window?

I was just reading »How to revert to GNOME Classic?«. Some answers say that you should press Alt+Win to get the Add to panel dialogue. However my keyboard has no Win. So I was asking myself how to start the Add to panel window. Usually the best way is to enter the program name into a terminal. In this case I can’t figure out the name.

I tested it on another computer and couldn’t find the name in the process list. Also xwininfo or similar programs shoed no name. So what is the best way to figure out the name of the program which opened a (X) window.

Asked By: qbi


Running xprop _NET_WM_PID from a terminal will put X into a special mode where you can click on a window to get that window’s PID in the terminal.

Then run ls -l /proc/<pid>/exe and look at where the link is to to figure out what the program’s file name is.

The following is a little python script take from a program I’m working on:

#! /usr/bin/env python

import os, subprocess

# Function based on code from apport
def get_window_pid():
    xprop = subprocess.Popen(['xprop', '_NET_WM_PID'],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, err) = xprop.communicate()
    if xprop.returncode == 0:
            return int(out.split()[-1])
        except ValueError:
            return -1
        return -1

def get_window_exe():
    pid = get_window_pid()
    if pid == -1:
        return ''
    return os.path.realpath('/proc/' + str(pid) + '/exe')

if __name__=='__main__':
    exe = get_window_exe()
    if exe == '':
        print 'Unable to identify window's executable' 
        print exe

Save this into a file and run by typing python <filename>.py. Click on the window and it will print the program’s name.

Answered By: iBelieve

Based on iBelieve’s answer:

alias fxname='basename $(ll /proc/$(xprop _NET_WM_PID | awk "{print $NF}")/exe | awk "{print $NF}")'

The $NF in the awk commands must be escaped so it’s not interpreted as a shell variable. As soon as you run it a reticle will appear for you to click on the desired window.

Answered By: user1714965

Today this can be done more concisely using xdotool

xdotool getactivewindow getwindowclassname


Answered By: Matt
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.