Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 18 down vote accepted

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.

share|improve this answer
    
I'd just like to note that I get different results when running the second command. First one works as I'd expect. –  TM. Jan 11 '11 at 2:27
    
I use Arch but it might help someone here in the future. The 1st one didn't work for me, 2nd one didn't provide correct dimensions maybe due to the WM. But the combination of the two worked. xwininfo -id $(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)" | grep -Eo "0x[0-9a-f]{2,}") –  Sev Jan 19 '13 at 16:10
add comment

With "xdtotool" you can easily interact with windows using keyboard or mouse events.

Example of sending a ctrl-c 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.

share|improve this answer
    
Interesting, but I just get Error: Invalid key sequence 'ctrl-c'. –  l0b0 May 22 '13 at 14:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.