dotfiles/.local/bin/move-window-to-empty-workspace

49 lines
1.2 KiB
Shell
Executable file

#!/bin/sh
# Move the active window to the first empty workspace and follow it
#
# Note: This assumes that only the main monitor has workspaces enabled
set -eu
active=$(xprop -root _NET_ACTIVE_WINDOW | awk '{print $NF}')
desktop=$(xprop -id "$active" _NET_WM_DESKTOP 2>/dev/null | awk '{print $NF}')
# 4294967295 (i.e., 0xFFFFFFFF) means "on all workspaces",
# which is what `mutter` reports for windows on secondary monitors
# when "workspaces-only-on-primary" is true
if [ "$desktop" = "4294967295" ] || [ -z "$desktop" ]; then
command -v notify-send >/dev/null \
&& notify-send 'Window Management' 'Active window is not on a workspace on the main screen'
exit 1
fi
occupied_workspaces=$(wmctrl -l) || exit 1 # fail safe
occupied_workspaces=$(echo "$occupied_workspaces" | awk '{print $2}' | sort -u)
total=$(wmctrl -d | wc -l)
# 0-based index for the first workspace
i=0
while [ "$i" -lt "$total" ]; do
if ! echo "$occupied_workspaces" | grep -qx "$i"; then
wmctrl -r :ACTIVE: -t "$i"
wmctrl -s "$i"
exit 0
fi
i=$((i + 1))
done
command -v notify-send >/dev/null \
&& notify-send 'Window Management' 'No empty workspace available'
exit 1