Skip to content

Instantly share code, notes, and snippets.

@svandragt
Last active January 16, 2024 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svandragt/298478faffaf9497443496ab31ac472e to your computer and use it in GitHub Desktop.
Save svandragt/298478faffaf9497443496ab31ac472e to your computer and use it in GitHub Desktop.
Cascade the windows on the current workspace and display
#!/bin/bash
# Variables to set the starting position and increment.
xPos=60
yPos=60
increment=30
# Save the active window id for later.
active_window_id=$(xdotool getactivewindow)
active_window_id_hex=$(printf "0x%08x" "$active_window_id")
active_screen=$(xprop -id "$active_window_id" _NET_WM_DESKTOP | awk '{print $3}')
# Get window IDs of all windows on the same screen as active window.
workspace_windows=$(wmctrl -l | awk -v screen="$active_screen" '$2==screen{print $1}')
# Loop through all visible windows.
for window_id in $workspace_windows; do
# Skip the active window.
if [ "$window_id" == "$active_window_id_hex" ]; then
continue
fi
# Convert id to hexadecimal form, required by xdotool.
window_id_hex=$(printf "0x%08x" "$window_id")
# Get the name of the window.
wname=$(xdotool getwindowname "$window_id_hex")
# Skip certain windows.
if [ "$wname" != "" ] && [ "$wname" != "plank" ] && [ "$wname" != "io.elementary.wingpanel" ] && [[ "$wname" != *"CopyQ"* ]]; then
echo "Cascading $wname to $xPos,$yPos"
# Unmaximize the window so it can be repositioned
wmctrl -i -r "$window_id" -b remove,maximized_vert,maximized_horz
# Use xdotool to move and raise the window.
xdotool windowmove "$window_id" $xPos $yPos
xdotool windowraise "$window_id" && xdotool windowactivate "$window_id"
# Increment position for the next window.
xPos=$((xPos + increment))
yPos=$((yPos + increment))
fi
done
# Move and raise the initially active window.
wname=$(xdotool getwindowname "$active_window_id")
echo "Cascading $wname to $xPos,$yPos"
wmctrl -i -r "$active_window_id" -b remove,maximized_vert,maximized_horz
xdotool windowmove "$active_window_id" $xPos $yPos
xdotool windowraise "$active_window_id" && xdotool windowactivate "$active_window_id"
@svandragt
Copy link
Author

image

Works better with some apps than others

@svandragt
Copy link
Author

svandragt commented Jan 15, 2024

  • does not reposition windows on non primary displays.
  • does not reposition Elementary Files App correctly due to invisible title bar
  • After closing the Files app it's still listed in active windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment