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 は 16 進数の表示に若干異なる形式を使用していることが判明しました)。

編集 3: 特定の競合を回避するために、アプリ名は wmctrl -lx の 3 番目の列から取得する必要があります。

答え2

を使って非常に堅牢なアプリスイッチャーを作りましたwmctrlUbuntu フォーラムの投稿と私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 の回答)。
  • アプリのウィンドウがない場合は、ウィンドウを開きます (その場合に実行するコマンドを指定するオプションの 2 番目の引数が追加されました)。

スクリプトは次のとおりです。

#!/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」です。

関連情報