#!/bin/sh # Cycle focus through visible X windows (next or previous). # Usage: window_cycle.sh next | prev # POSIX-compliant; works without a window manager. # Get direction argument case "$1" in next) DIR="next" ;; prev) DIR="prev" ;; *) echo "Usage: $0 next | prev" >&2 exit 1 ;; esac # Get all visible window IDs (space-separated) WINDOWS=$(xdotool search --onlyvisible -class . | tr '\n' ' ') # Get the currently focused window ID CURRENT=$(xdotool getwindowfocus) NEXT="" PREV="" LAST="" FOUND=0 for ID in $WINDOWS; do if [ "$DIR" = "next" ]; then # Find next window if [ "$FOUND" -eq 1 ]; then NEXT=$ID break fi if [ "$ID" = "$CURRENT" ]; then FOUND=1 fi else # Find previous window if [ "$ID" = "$CURRENT" ]; then if [ -z "$PREV" ]; then PREV=$LAST fi break fi PREV=$ID LAST=$ID fi done # Wrap around if needed if [ "$DIR" = "next" ]; then if [ -z "$NEXT" ]; then for ID in $WINDOWS; do NEXT=$ID break done fi TARGET=$NEXT else if [ -z "$PREV" ]; then for ID in $WINDOWS; do PREV=$ID done fi TARGET=$PREV fi # Focus the chosen window xdotool windowfocus "$TARGET"