How can I find the location (on the desktop) of a window on the command line?
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?
Solution 1:
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.
Solution 2:
With "xdotool" you can easily interact with windows using keyboard or mouse events.
Example of sending a CtrlC sequence to the active window:
xdotool getwindowfocus key ctrl+c # Sends CTRL+C to the current window.
If you really need the window position:
eval $(xdotool getmouselocation --shell); echo $X $Y
For more actions/options check the man page.