Skip to content

Instantly share code, notes, and snippets.

@asidko
Last active December 30, 2025 07:46
Show Gist options
  • Select an option

  • Save asidko/176c325d5387e4440b132fb0ad71b194 to your computer and use it in GitHub Desktop.

Select an option

Save asidko/176c325d5387e4440b132fb0ad71b194 to your computer and use it in GitHub Desktop.
XFCE window repositioning script - centers and resizes off-screen windows to fit current display. Useful when connecting via RDP from different devices (phone, tablet, different monitors)
#!/bin/bash
# Resize and reposition windows to fit current screen work area
get_work_area() {
wmctrl -d | awk '/\*/ {
split($9, dim, "x")
split($8, offset, ",")
print offset[1], offset[2], dim[1], dim[2]
}'
}
read -r wa_x wa_y wa_w wa_h <<< "$(get_work_area)"
if [[ -z "$wa_w" || -z "$wa_h" ]]; then
notify-send "tyle" "Failed to get work area"
exit 1
fi
current_desktop=$(wmctrl -d | awk '/\*/ {print $1}')
border=2
titlebar=28
padding=$((border * 2))
wmctrl -l | while read -r wid desktop _host title; do
[[ "$desktop" != "$current_desktop" ]] && continue
[[ "$desktop" == "-1" ]] && continue
# skip minimized windows
if xprop -id "$wid" _NET_WM_STATE 2>/dev/null | grep -q "_NET_WM_STATE_HIDDEN"; then
continue
fi
eval "$(xdotool getwindowgeometry --shell "$wid" 2>/dev/null)"
wx=$X wy=$Y ww=$WIDTH wh=$HEIGHT
[[ -z "$ww" || -z "$wh" ]] && continue
new_w=$ww
new_h=$wh
max_w=$((wa_w - padding))
max_h=$((wa_h - padding - titlebar))
# resize if too large
((new_w > max_w)) && new_w=$max_w
((new_h > max_h)) && new_h=$max_h
new_x=$(( wa_x + (wa_w - new_w) / 2 ))
new_y=$(( wa_y + (wa_h - new_h) / 2 ))
wmctrl -i -r "$wid" -e "0,$new_x,$new_y,$new_w,$new_h"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment