/home/amgsk

Hyprlandでアプリをトグルする

· amgsk

シングルインスタンスのアプリをトグルするスクリプト。以下の挙動を実現するもの。

  • アプリが起動して無ければ起動
  • アプリが起動していてフォーカスされていなければフォーカス
  • アプリが起動していてフォーカスされていれば閉じる

~/bin/toggle-app.shに保存し、実行権限を付与する。

~/bin/toggle-app.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# Usage: toggle-app.sh <window-class> [launch-command]
#   window-class:  class name
#   launch-command: command

class="$1"
cmd="${2:-${class,,}}"

if [ -z "$class" ]; then
  echo "Usage: $(basename "$0") <window-class> [launch-command]" >&2
  exit 1
fi

focused=$(hyprctl activewindow | grep -qi "$class" && echo yes)
running=$(hyprctl clients | grep -qi "$class" && echo yes)

if [ "$focused" = "yes" ]; then
  hyprctl dispatch closewindow "class:$class"
elif [ "$running" = "yes" ]; then
  hyprctl dispatch focuswindow "class:$class"
else
  $cmd &
fi

使い方はこう。

~/.config/hypr/hyprland.conf
1
2
bind = Alt, S, exec, ~/bin/toggle-app.sh slack Slack
bind = Alt, T, exec, ~/bin/toggle-app.sh sublime_text subl