20

I would like to get the size of the current window from a shell script...

Really not much else to say... I would prefer to use wmctrl.

6 Answers 6

26
wmctrl -lG

Will give you something like:

oli@bert:~$ wmctrl -lG
0x0384c0d5 -1 1590 1030 330  170  bert N/A
0x01200023 -1 0    0    3840 1200 bert x-nautilus-desktop
0x01000003 -1 3840 2352 1920 24   bert Bottom Expanded Edge Panel
0x01000028 -1 0    2352 1920 24   bert Bottom Expanded Edge Panel
0x0500003e  0 676  252  1404 1015 bert Chromium
0x02e00021  0 3860 160  1361 1084 bert Liferea
0x02000047  0 6650 940  506  683  bert Social broadcast messages
0x04600004  0 4546 460  1263 833  bert oli@bert: ~

With this you can grep and cut down on these so you are left with the geometry values (columns 3-6).

To quote the man page so you understand exactly what the columns are:

-l

List the windows being managed by the window manager. One line is output for each window, with the line broken up into space separated columns. The first column always contains the window identity as a hexadecimal integer, and the second column always contains the desktop number (a -1 is used to identify a sticky window). If the -p option is specified the next column will contain the PID for the window as a decimal integer. If the -G option is specified then four integer columns will follow: x-offset, y-offset, width and height. The next column always contains the client machine name. The remainder of the line contains the window title (possibly with multiple spaces in the title).

2
  • I'll be using that!
    – Allan
    Feb 24, 2011 at 14:39
  • 1
    I love the way your computer is called bert :)
    – drnessie
    Sep 19, 2011 at 14:55
7

Use xprop or xwininfo. Both come by default, no install necessary

Usage examples:

Both commands turn cursor into square/cross to allow selecting a particular window.

$ xprop _NET_WM_OPAQUE_REGION                                                          
_NET_WM_OPAQUE_REGION(CARDINAL) = 0, 0, 984, 377


$ xwininfo | awk -F ':' '/Width/ || /Height/{print $2}'                         
 984
 377

Alternatively, one can specify window on command line in XID form

$ xprop _NET_WM_OPAQUE_REGION -id 83886090                                             
_NET_WM_OPAQUE_REGION(CARDINAL) = 0, 0, 984, 377

$ xwininfo -id 83886090 | awk -F ':' '/Width/ || /Height/{print $2}'            
 984
 377

Other posts where these were used

In particular, xwininfo, has been actively used by me for scrips , such as on these AskUbuntu questions:

2
  • He common, add an example ;)
    – A.B.
    May 19, 2015 at 21:11
  • Added them after a year ;) But better later than never , right ? Jul 15, 2016 at 17:25
5

I belive wmctrl does not have any option for finding the active window Id directly.
If someone knows how to do it, I'm interested to know..
That said, here are a couple of scripts which print out the active window's size.

This is: wmctrl + xdotool ...(not using sed).

id=$(xdotool getactivewindow)
wmctrl -lpG | while read -a a; do w=${a[0]}; if (($((16#${w:2}))==id)) ; then echo -n "${a[5]} ${a[6]}"; break; fi; done

This is: xwininfo + xdotool + sed

xwininfo is part of x11-utils

set $(xwininfo -id $(xdotool getactivewindow) \
|sed -n -e "s/^ \+Width: \([0-9]\+\).*/\1/p" \
        -e "s/^ \+Height: \([0-9]\+\).*/\1/p")
echo -n "$1 $2"
0
0

Shell scripts do not know or care about windows. At best they run in a terminal ( which may or may not be displayed in a window ). If you want to get the width and height of the terminal in characters, use the stty utility.

4
  • 2
    Surely a shell script could somehow query X clients for their geometry? Else how would X apps be written?
    – djeikyb
    Feb 24, 2011 at 22:01
  • @djeikyb they are written to be X clients and communicate with the X server. Shell scripts run in a shell, which may or may not be running in an environment that even has an X server, and should not care about X.
    – psusi
    Feb 25, 2011 at 14:19
  • 2
    Understandably, yes. But there are workarounds! Using wmctrl, a package from the repo, you can move, resize, minimize and even change workspace!
    – drnessie
    Feb 26, 2011 at 5:04
  • By @psusi's logic, desktop environments should not exist. After all, they're just a huge collections of scripts that interact with software written for X.
    – andyn
    Oct 15, 2018 at 10:27
0

I found tput cols to work quite nicely!

2
  • 2
    Thank you for your contribution. I suggest to improve your answer: explain what it does, amend the command to get the line count as well. Maybe show how to use the results in a shell script. -- See the top rated answers as examples.
    – guntbert
    Sep 11, 2018 at 17:51
  • 1
    Inferring from OP mentioning wmctrl, we're not talking about console but graphical environment windows.
    – andyn
    Oct 15, 2018 at 10:17
0

Got 3 screens, this output 3 lines with the dimensions:

xrandr | grep '*' | cut -d'*' -f1 | cut -d ' ' -f4
/* OUTPUT *
* 3840x2160
* 1920x1080
* 1920x1080
***********/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .