wmctrl-聚焦应用程序的最近窗口

wmctrl-聚焦应用程序的最近窗口

我创建了一些模仿 Unity 的Mod4+num行为的快捷方式。

wmctrl -xa Sublime || subl

我不喜欢的是,一旦 sublime 运行,它总是聚焦第一个打开的窗口。我想聚焦最后一个“聚焦”窗口。与 Unity 中相同。有标志吗?

答案1

该脚本可以完成您想要的操作:

#!/bin/bash

app=$1
workspace=$(wmctrl -d | grep '\*' | cut -d ' ' -f1)
win_list=$(wmctrl -lx | grep $app | grep " $workspace " | awk '{print $1}')

IDs=$(xprop -root|grep "^_NET_CLIENT_LIST_STACKING" | tr "," " ")
IDs=(${IDs##*#})

for (( idx=${#IDs[@]}-1 ; idx>=0 ; idx-- )) ; do
    for i in $win_list; do
        if [ $((i)) = $((IDs[idx])) ]; then
            wmctrl -ia $i
            exit 0
        fi
    done
done

exit 1

编辑:此脚本始终聚焦于最后聚焦的窗口,而不是按照打开的顺序循环浏览窗口。

编辑2:我修改了脚本(结果发现 wmctrl 和 xprop 使用略有不同的格式来显示十六进制数)。

编辑 3:应用程序名称应取自 wmctrl -lx 的第 3 列,以避免某些冲突。

答案2

我使用 制作了一个非常强大的应用切换器wmctrl。查看我的Ubuntu 论坛帖子和我的askubuntu 答案

这是要启动的脚本:

#!/bin/bash
app_name=$1
workspace_number=`wmctrl -d | grep '\*' | cut -d' ' -f 1`
win_list=`wmctrl -lx | grep $app_name | grep " $workspace_number " | awk '{print $1}'`


active_win_id=`xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}' | awk -F', ' '{print $1}'`
if [ "$active_win_id" == "0" ]; then
    active_win_id=""
fi


# get next window to focus on, removing id active
switch_to=`echo $win_list | sed s/.*$active_win_id// | awk '{print $1}'`
# if the current window is the last in the list ... take the first one
if [ "$switch_to" == "" ];then
    switch_to=`echo $win_list | awk '{print $1}'`
fi


if [[ -n "${switch_to}" ]]
    then
        (wmctrl -ia "$switch_to") &
    else
        if [[ -n "$2" ]]
            then
                ($2) &
        fi
fi


exit 0

答案3

我将前面两个答案结合起来,我觉得这很方便。它能够:

  • 将焦点设置到应用程序最近聚焦的窗口(Raul Laasner 的回答)如果该窗口尚未获得焦点
  • 否则,循环到应用程序的下一个窗口(mreq 的答案);
  • 如果没有应用程序窗口,则打开一个(现在有一个可选的第二个参数,指定在这种情况下要运行的命令)。

脚本如下:

#!/bin/bash
if [ $# -lt 1 ] ; then
  echo "Usage : $0 <window name> [<command to run if there is no window with that name>]"
  exit 1
fi

app_name=$1

workspace_number=`wmctrl -d | grep '\*' | cut -d' ' -f 1`
win_list=`wmctrl -lx | grep $app_name | grep " $workspace_number " | awk '{print $1}'`

# Get the id of the active window (i.e., window which has the focus)
active_win_id=`xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}' | awk -F', ' '{print $1}'`
if [ "$active_win_id" == "0" ]; then
    active_win_id=""
fi

# If the window currently focused matches the first argument, seek the id of the next window in win_list which matches it
if [[ "$win_list" == *"$active_win_id"* ]]; then

    # Get next window to focus on 
    # (sed removes the focused window and the previous windows from the list)
    switch_to=`echo $win_list | sed s/.*$active_win_id// | awk '{print $1}'`

    # If the focused window is the last in the list, take the first one
    if [ "$switch_to" == "" ];then
        switch_to=`echo $win_list | awk '{print $1}'`
    fi

# If the currently focused window does not match the first argument
else

    # Get the list of all the windows which do
    win_list=$(wmctrl -lx | grep $app_name | awk '{print $1}')

    IDs=$(xprop -root|grep "^_NET_CLIENT_LIST_STACKING" | tr "," " ")
    IDs=(${IDs##*#})

   # For each window in focus order
    for (( idx=${#IDs[@]}-1 ; idx>=0 ; idx-- )) ; do
        for i in $win_list; do

           # If the window matches the first argument, focus on it
            if [ $((i)) = $((IDs[idx])) ]; then
                wmctrl -ia $i
                exit 0
            fi
        done
    done
fi

# If a window to focus on has been found, focus on it
if [[ -n "${switch_to}" ]]
then
    (wmctrl -ia "$switch_to") &

# If there is no window which matches the first argument
else

    # If there is a second argument which specifies a command to run, run it
    if [[ -n "$2" ]]
    then
        ($2) &
    fi
fi

exit 0

使用示例:

focus_window.sh vlc vlc
focus_window.sh gnome-control-center gnome-control-center
focus_window.sh gnome-terminal gnome-terminal

答案4

答案迟了,但设法优雅地完成了工作,就我而言,使用 Firefox:

/usr/lib/firefox/firefox && sleep 0.5 && wmctrl -ia $(wmctrl -l Firefox | grep "Mozilla Firefox" | sort -r | head -n 1 | cut -d " " -f 1)

解释:

/usr/lib/firefox/firefox- 打开 Firefox

sleep 0.5- 给窗口打开一些时间

wmctrl -ia- 聚焦于特定窗口 ID

$(wmctrl -l Firefox | grep "Mozilla Firefox" | sort -r | head -n 1 | cut -d " " -f 1)- 获取最后一个窗口 ID,在我的情况下,该窗口在主页上打开,因此其名称为“Mozilla Firefox”。

相关内容