#!/bin/bash # 1. 取得目前 focused workspace 的名稱 CURRENT_WS=$(swaymsg -t get_workspaces | jq -r '.[] | select(.focused).name') # 如果沒抓到 workspace,直接退出 if [ -z "$CURRENT_WS" ]; then exit 0 fi # 2. 列出該 workspace 內的所有視窗 (con 與 floating_con) # 直接從 get_tree 找對應名稱的 workspace 節點並遞迴出視窗資訊 entries=$(swaymsg -t get_tree | jq -r --arg WS "$CURRENT_WS" ' .. | select(.type? == "workspace" and .name == $WS) | recurse(.nodes[]?, .floating_nodes[]?) | select(.type == "con" or .type == "floating_con") | select(.name != null) | (.id | tostring) + " [" + (.app_id // .window_properties.class // "Unknown") + "] " + .name') # 如果沒有視窗,直接結束 if [ -z "$entries" ]; then exit 0 fi # 3. 透過 wofi 顯示選單,並取得選擇視窗的 id (第一個欄位) selected=$(echo "$entries" | wofi --width 600 --height 400 --dmenu --cache-file /dev/null | awk '{print $1}') # 如果有選擇,則切換焦點到該視窗 if [ -n "$selected" ]; then swaymsg "[con_id=$selected]" focus fi