Skip to content

Instantly share code, notes, and snippets.

@ukn
Last active October 15, 2021 20:30
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 ukn/329c2f4542e043ffab2f5ceef0442ee8 to your computer and use it in GitHub Desktop.
Save ukn/329c2f4542e043ffab2f5ceef0442ee8 to your computer and use it in GitHub Desktop.
Resize active window to 1/3 of the screen and position it in the middle, right or left part of the display
#!/usr/bin/env bash
#
# Resize active window to 1/3 of the screen and position it in the middle, right or left part of the display
# Tested with XFCE (xserver) and ultra wide monitor (3440x1440)
#
# based on: https://askubuntu.com/questions/104155/center-a-window-via-command-line#answer-571711
# Can this be calculated automagically?
taskBarHeight=63
# Get display WIDTH and HEIGHT
eval $(xdotool getdisplaygeometry --shell)
# Possible window X positions
middleX=$((WIDTH/3))
leftX=0
rightX=$((WIDTH/3 * 2))
# Window width to be 1/3 of the screen
sizeX=$((WIDTH/3 - 1))
# Get active window position X
eval $(xdotool getactivewindow getwindowgeometry --shell | egrep 'X=')
# Had to add 6 pixels to calculations
padding=6
if (( "$X" == $((middleX + padding)) )); then
newPos=$rightX
elif (( "$X" == $((rightX + padding)) )); then
newPos=$leftX
else
newPos=$middleX
fi
# DEBUG
#echo $X,$leftX,$middleX,$rightX
xdotool getactivewindow windowsize "$sizeX" "$((HEIGHT - taskBarHeight))"
xdotool getactivewindow windowmove "$newPos" "0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment