Get X window id from process in bash

做~自己de王妃 提交于 2019-12-08 01:14:21

问题


Is there any bash tool/warper that could tell which x window(s) has been created by specific process ?


回答1:


wmctrl -lp

gives me the windows and their PIDs. Sample output:

0x04c00022  0 2168   ciro ciro.santilli - Skype™
0x03c00014  0 2154   ciro Krusader
0x03200022  0 2181   ciro Guake!
...



回答2:


As mentioned, you can use command line tools like wmctrl or xprop. Well behaved clients should set _NET_WM_PID property to be pid of the process which created main window (all popular toolkits do this for you). Note that some clients don't set it or may be on another physical machine (you can use WM_CLIENT_MACHINE property) - so use this information as a hint and don't rely on it to be present or accurate. See emwh spec at freedesktop for reference.




回答3:


Here are several X11 window management solutions (including one to this problem).

To get the ID of a window by its process ID, use wmctrl in the following way:

#!/usr/bin/env bash
# getwindidbypid
# 
# Get the ID of a window by PID (if the process has a window).
# 
# Usage:
#   getwindidbypid <PID>
# 

while IFS= read line; do
  if [[ "${line}" =~ (0x)([0-9a-z]+)([ ][- ][0-9]+[ ])([0-9]*) ]]; then
    winId="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
    pid="${BASH_REMATCH[4]}"
    if [[ "${pid}" -eq "${1}" ]]; then
      WIND_IDS+=("${winId}")
    fi
  fi
done < <(wmctrl -lp)

if [ "${#WIND_IDS[@]}" -gt 0 ]; then
  echo "${WIND_IDS[0]}"
fi

Example:

user ~ $  getwindidbypid 37248
0x05a00012


来源:https://stackoverflow.com/questions/20580897/get-x-window-id-from-process-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!