14

This command will get the PID of the xterm process launched:

xterm & export APP_PID=$!

How can I get the window ID associated to that process (the xterm window ID)? I mean, the ID that xdotool selectwindow would return after clicking on the xterm window.

3 Answers 3

17

You can get the list of matching windows via

 xdotool search --pid [pid]

also see xdotool help search

10

I will use this simple command to get the window ID in hex format

wmctrl -l | grep -i xterm | awk '{print $1}'

For decimal format, bc command can be used for conversion

echo "ibase=16; `wmctrl -l | grep -i xterm | cut -c 3-11 | tr a-z A-Z`" | bc
2
  • 2
    wmctrl -l doesn't give useful results if your xterm window title has been set, e.g., to a string which does not contain "xterm". Nov 7, 2016 at 0:34
  • 2
    There are options you can add. -p gives PID, -G gives position and dimensions. In my case, wmctrl -l -p | { pidof firefox vlc | read line && grep ${line// /\\|} }, and optionally go straight for xwininfo from there: | while read id junk; do xwininfo -id $id; done. I'm using zsh, I'm not sure if that regex is fully portable.
    – John P
    Jun 2, 2018 at 20:27
8

It's been discussed in the "other" forum:

In the first, @Patrick points out that xwininfo can return information on all windows, and by using xprop for each window, you can check for the _NET_WM_PID property, matching it against your process-id.

4
  • 1
    The most direct way, as given in this answer in your 2nd link, is win=$(sed -zn < /proc/$APP_PID/environ 's/^WINDOWID=\(.*\)/\1/p')
    – meuh
    Nov 4, 2016 at 14:53
  • sure - if you prefer non-portable answers. Nov 4, 2016 at 14:54
  • 1
    It's worth noting WINDOWID is a terminal emulator convention (probably from xterm in X10R4, 1986, yes X10, not a typo) not a general X application one; like _NET_WM_PID is a wm-spec convention (since a 1999 draft) that not all terminals follow. Even some modern terminal emulators don't always set WINDOWID. It is also the case that non-terminal processes can inherit it via a parent shell. Beware poking in environ files ;-) Nov 7, 2018 at 12:26
  • yes, but OP didn't ask that (xterm implements that, too see notes). Nov 7, 2018 at 21:55

You must log in to answer this question.

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