bash 从bash进程中获取X窗口ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20580897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get X window id from process in bash
提问by grinchfox Colinsgrove
Is there any bash tool/warper that could tell which x window(s) has been created by specific process ?
是否有任何 bash 工具/warper 可以告诉特定进程创建了哪个 x 窗口?
回答by Andrey Sidorov
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 freedesktopfor reference.
如前所述,您可以使用命令行工具,例如wmctrl
或xprop
。行为良好的客户端应该将_NET_WM_PID
属性设置为创建主窗口的进程的 pid(所有流行的工具包都会为您执行此操作)。请注意,某些客户端没有设置它或者可能在另一台物理机器上(您可以使用WM_CLIENT_MACHINE
属性) - 因此使用此信息作为提示,不要依赖它的存在或准确。请参阅freedesktop 上的 emwh 规范以供参考。
回答by Adrian Bartyczak
Hereare several X11 window management solutions (including one to this problem).
这里有几个 X11 窗口管理解决方案(包括一个解决这个问题的)。
To get the ID of a window by its process ID, use wmctrl in the following way:
要通过进程 ID 获取窗口的 ID,请按以下方式使用 wmctrl:
#!/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 "" ]]; 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