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.

nightcod3r
  • 952
  • 2
  • 16
  • 32

3 Answers3

17

You can get the list of matching windows via

 xdotool search --pid [pid]

also see xdotool help search

phil294
  • 875
  • 12
  • 15
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
SHW
  • 14,454
  • 14
  • 63
  • 101
  • 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". – Thomas Dickey Nov 07 '16 at 00: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 02 '18 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.

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
  • 1
    The most direct way, as given in [this answer](http://stackoverflow.com/a/2754512/5008284) in your 2nd link, is `win=$(sed -zn < /proc/$APP_PID/environ 's/^WINDOWID=\(.*\)/\1/p')` – meuh Nov 04 '16 at 14:53
  • sure - if you prefer non-portable answers. – Thomas Dickey Nov 04 '16 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 ;-) – mr.spuratic Nov 07 '18 at 12:26
  • yes, but OP didn't ask that (xterm implements that, too [see notes](https://invisible-island.net/xterm/xterm.log.html#xterm_203)). – Thomas Dickey Nov 07 '18 at 21:55