Ubuntu window screenshot with frame

Ever since advanced desktop effects (previously Beryl or Compiz, now Compiz Fusion) were introduced, there has been an annoying broken-ness of the default Ubuntu screenshot tool. Basically, the screenshot of a window is missing the window’s frame. This problem was reported two years ago here, and remained unfixed to date.

Ok, two years should be more than enough to find a fix for something so rudimentary. But I fully understand the voluntary nature of open source software, and things did fall thru the cracks on occasions. But then again, two years?

The problem

In Compiz Fusion lingo, window’s frame is referred to as “Window Decoration”. You should notice all the screenshots of windows and dialogboxes I made in this blog (to date) were missing their frames. This was not because I deliberately removed them; it’s a result of this bug. For instance, here is a screenshot of the calculator, using the Ubuntu screenshot tool. You can see the screenshot is missing the window’s frame.

title

Note:
When I mentioned the Ubuntu screenshot tool, I meant the one you get by pressing ALT+PrtScr, or by launching the tool from:

Applications > Accessories > Take Screenshot

Workarounds?

I could thinking of three ways to workaround this problem.

  1. Live with it.
    Well, not technically a workaround, but I have been ignoring this problem. It’s not too bad that a screenshot is missing the window’s frame; having the frame looks better, but not vital to the information that needs to be conveyed.
  2. Capture the desktop with the window in question, then manually cropping the image to include the frame.
    This one is doable, but requires too much work.
  3. Temporarily disable Compiz to take the screenshot.
    The screenshot worked without this problem in Metacity. This appeared to be the simplest solution, but still a hassle.

Introducing my script

When I found out about xdotool and xwininfo (see previous post), I realised it’s possible to hack a script that workaround this problem.

For this script to work, you would need to install xdotool, imagemagick and scrot.

sudo apt-get install xdotool imagemagick scrot

Then, copy/paste the script below to a text file, and set the execution bit. That’s all!

#!/bin/bash

# get window id
# "plus" mouse cursor for user to select the window
W_ID=`xwininfo | grep "Window id" | cut -d\  -f4`

# get frame id
F_ID=`xprop -id $W_ID _NET_FRAME_WINDOW | cut -d\  -f5`

# get frame location
F_X=`xwininfo -id $F_ID | grep "Absolute upper-left X" | cut -d\: -f2`
F_Y=`xwininfo -id $F_ID | grep "Absolute upper-left Y" | cut -d\: -f2`

# get frame dimension
F_W=`xwininfo -id $F_ID | grep "Width" | cut -d\: -f2`
F_H=`xwininfo -id $F_ID | grep "Height" | cut -d\: -f2`

# bring window on top
xdotool windowactivate $W_ID

# take screenshot of desktop
# note: import command is "broken", so use scrot instead
scrot -d 1 screenshot.png

# crop desktop image to selected window
convert -crop "$F_W"x"$F_H"+"$F_X"+"$F_Y"! screenshot.png screenshot.png

Results

I added a panel launcher for this script, so now I simply click it when I wanted to take a window screenshot. The mouse cursor will turned to a “plus”. I then click on the window I wanted to capture, and the image file will be saved to “screenshot.png”.

Here is how the calculator actually look like with frame (I used “AgingGorilla” window border).

title

So from now onwards, the screenshots in this blog should look better. :)

Comments 5

  1. Kaho wrote:

    Thanks, this is nice !
    I used Ctrl+PrtScr keybinding to launch the script ^^

    But if I take a new screenshot, it overwrites the previous one… Is there a way to automatically name the files “sreenshot1.png”, “screenshot2.png”… ?
    Also I wanted to know if it was possible to have the screenshots saved on the desktop instead of the home directory ?

    Posted 23 Feb 2009 at 1:40 am
  2. chewearn wrote:

    hi Kaho
    Thanks for reading.

    I guess it should be possible to automatically append a number to the filename, to prevent it from overwriting a previous file. However, it’s troublesome enough that I would not want to implement this. Really, screenshots are not something I do frequently enough to warrant the effort.

    To save the screenshot file to the desktop, simply change the three occurrences of “screenshot.png” to “~/Desktop/screenshot.png”.

    Posted 23 Feb 2009 at 2:00 am
  3. Rod wrote:

    I was caught out by this bug recently. I was replacing the original Russian screenshots in an English help file for an application (Double Commander). I stupidly didn’t notice that the screenshots were missing the titlebars and I had created about 3/4 of them. I had to switch to metacity for the window decoration and recreate them all over again :-(

    This bug still exists in Jaunty (and I wouldn’t be surprised if it’s in Karmic too). Thanks for your fix, it will be nice to take screenshots with my fancy compiz decorations :-)

    Rod.

    Posted 19 Feb 2010 at 6:28 pm
  4. chewearn wrote:

    hi Rod
    Thanks for reading.

    Yeah, at this stage, there is little hope this bug will ever be fixed.

    Compiz Fusion developers are going thru a rewrite to C++ in version 0.9. Meanwhile, Gnome people is busy with Gnome Shell for Gnome 3. Both sides have not had a good history of co-operating.

    Posted 19 Feb 2010 at 9:14 pm
  5. hanaoka wrote:

    I tried command:
    import -frame -screen screenshot.png

    Not everytime, but it takes the screenshot of the window with frame for me.

    Posted 13 May 2010 at 12:48 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *