How do I find the window dimensions and position accurately including decorations?
The following script will give you the top-left screen co-ords and size of the window (without any decoration). . . . xwininfo -id $(xdotool getactivewindow)
contains enough information for you.
#!/bin/bash
# Get the coordinates of the active window's
# top-left corner, and the window's size.
# This excludes the window decoration.
unset x y w h
eval $(xwininfo -id $(xdotool getactivewindow) |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
echo -n "$x $y $w $h"
#
Much simpler way to get current window size and position:
xdotool getwindowfocus getwindowgeometry
and to get selected window size and position:
xdotool selectwindow getwindowgeometry
The accepted answer can be extended to get the entire window:
entire=false
x=0
y=0
w=0
h=0
b=0 # b for border
t=0 # t for title (or top)
# ... find out what user wants then
eval $(xwininfo -id $(xdotool getactivewindow) |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" \
-e "s/^ \+Relative upper-left X: \+\([0-9]\+\).*/b=\1/p" \
-e "s/^ \+Relative upper-left Y: \+\([0-9]\+\).*/t=\1/p" )
if [ "$entire" = true ]
then # if user wanted entire window, adjust x,y,w and h
let x=$x-$b
let y=$y-$t
let w=$w+2*$b
let h=$h+$t+$b
fi
echo "$w"x"$h" $x,$y
Although easy, it turns out not to work on Unity in Ubuntu 14.04 because the Relative info is all 0. I asked Get the full window dimensions (including decorations) in Unity and got a good answer. Here is how I used that answer:
aw=$(xdotool getactivewindow)
eval $(xwininfo -id "$aw" |
sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/x=\1/p" \
-e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/y=\1/p" \
-e "s/^ \+Width: \+\([0-9]\+\).*/w=\1/p" \
-e "s/^ \+Height: \+\([0-9]\+\).*/h=\1/p" )
if [ "$entire" = true ]
then
extents=$(xprop _NET_FRAME_EXTENTS -id "$aw" | grep "NET_FRAME_EXTENTS" | cut -d '=' -f 2 | tr -d ' ')
bl=$(echo $extents | cut -d ',' -f 1) # width of left border
br=$(echo $extents | cut -d ',' -f 2) # width of right border
t=$(echo $extents | cut -d ',' -f 3) # height of title bar
bb=$(echo $extents | cut -d ',' -f 4) # height of bottom border
let x=$x-$bl
let y=$y-$t
let w=$w+$bl+$br
let h=$h+$t+$bb
fi
This second method works in both Unity and Xfce, and should work in Gnome too.