是否可以获取有关运行 bash 脚本的终端窗口/选项卡的状态信息?

发布于 2024-12-11 03:39:42 字数 229 浏览 0 评论 0原文

如果已经有人问过这个问题,请提前道歉 - 搜索这个问题真的很难,因为 bash 和终端同时出现在很多地方。

基本上我想要做的是让 bash 脚本知道它运行的终端当前是否处于活动状态(选定),以便我可以使用通知发送来通知用户只有在以下情况下某些操作已完成它当前不是活动窗口。我猜想可以做一些事情,至少可以通过以某种方式访问​​ gnome 来检测活动窗口是否是终端窗口,但我不知道如何做,我想这可能是一个黑客,但任何帮助都是赞赏。

Apologies in advance if this has already been asked - it's really hard to search for this as bash and terminal show up together in so many places.

Basically what I'd like to be able to do is have a bash script be aware of whether or not the terminal it's running in is currently active (selected) so that I could use notify-send to inform the user something has completed only if it's not currently the active window. I'm guessing it would be possible to do something that can at least detect whether the active window is a terminal window by accessing gnome somehow, but I have no idea how and I imagine it would likely be a hack, but any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2024-12-18 03:39:42

这个问题主要是关于 xserver 和窗口管理,而不是 bash 本身。为此,您需要两件事:

a) 找到您的窗口:

gt; xprop -root _NET_CLIENT_LIST_STACKING

它给出如下输出:

_NET_CLIENT_LIST_STACKING(WINDOW): window id # 0xe0001e, 0x3a0006d, 0x1a00004

这是窗口堆栈,因此最后一个窗口位于顶部。您需要提取窗口 ID 列表 (0xe0001e 0x3a0006d 0x1a00004)。

接下来,您必须迭代该 id 列表。您可以通过检查此属性来找到您的窗户:

 xprop -id <win id> WM_NAME

b) 如何升起窗户的示例如下:
X11:通过命令行提升现有窗口?

Well this question is rather about xserver and window management then bash itself. You need 2 things for this:

a) to find your window:

gt; xprop -root _NET_CLIENT_LIST_STACKING

It gives an output like:

_NET_CLIENT_LIST_STACKING(WINDOW): window id # 0xe0001e, 0x3a0006d, 0x1a00004

This is window stack so the last window is on top. You need to extract list of window ids (0xe0001e 0x3a0006d 0x1a00004).

Next you have to iterate over that list of ids. You will find your window by examining this property:

 xprop -id <win id> WM_NAME

b) Example how to raise your window is here:
X11: raise an existing window via command line?

夜清冷一曲。 2024-12-18 03:39:42

经过更多挖掘后,我找到了一个可行的解决方案,因此我将其作为答案发布,但它不太便携,因此我仍然希望有任何更好的解决方案。

我发现,当使用 Gnome 时,您可以从 gnome 终端访问 $WINDOWID 变量(不幸的是,如果您已经 ssh 到另一个盒子,那么这将不起作用,因此并不理想)。这可以与 xprop 结合起来,如下所示:

activeWindow=$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')
thisWindow=$(printf '0x%x\n' $WINDOWID)

然后可以比较其是否相等。然而,获取当前窗口 id 的更合适的方法会更好。

编辑:看到 Zaytzev 的答案后,我想这可以合并,这样我就可以设置终端标题并检查 xprop -id $activeWindow WM_NAME 是否等于我的窗口标题,而不是使用 $WINDOWID 。

After some more digging I've found a workable solution so I'm going to post it as an answer, but it's not very portable so I'd still appreciate any better solutions.

What I've found is that when using Gnome you have access to a $WINDOWID variable from the gnome-terminal (this doesn't work if you've ssh'ed to another box unfortunately and so isn't ideal). This can be combined with xprop like this:

activeWindow=$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')
thisWindow=$(printf '0x%x\n' $WINDOWID)

Which can then be compared for equality. A more suitable way of getting the current window id would be good however.

Edit: Having seen Zaytzev's answer I guess this could be combined so instead of using $WINDOWID I could set the terminal title and check whether xprop -id $activeWindow WM_NAME is equal to my window title.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文