GNOME Scripts – Find Location of a Window on the Command Line

gnomescripts

I'm trying to write a little script to help manage my windows via keyboard shortcuts.

In order to do what I want, I need to be able to find the current location and size of the "active" (focused) window. By location, I mean the X,Y coordinates of the window on the desktop, and by size I mean the height and width of the window.

Is there a command line utility that can fetch this information?

Best Answer

You can get all the info for the current active window by using this command:

xwininfo -id $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')

or

xwininfo -id $(xdpyinfo | grep focus | grep -E -o 0x[0-9a-f]+)

Just parse the output for the co-ordinates you need. Here are some useful commands when interacting with windows:

  • xwininfo will give you the relative and absolute geometry.
  • xprop will tell you a lot of info, including if the window is active, but not it's geometry
  • xdpyinfo will get you the id of the active window too, but no extra info.
  • wmctrl will get you a list of information about windows, and allow you select the active window for modification, but not information.

For example to change the title of the active window to "New Name":

wmctrl -r :ACTIVE: -N "New Name"

Or to change the position of the active window:

wmctrl -r :ACTIVE: -e 0,20,20,200,400

Consider wmctrl if you need to just change something simple, like the window's geometry. Consider using the other scripts mentioned above only if you want to do other more complex things.