Wednesday, July 21, 2010

Taking periodic snapshots of your Ubuntu Desktop

I've been working an application (will be posting more about that a bit later), but in order to test it, i needed a tool that would constantly take snapshots of my desktop every few minutes and save those snapshots in my home folder.

I thought long about doing this, without support from external applications and using only those that were native to gnome. Unfortunately, that didn't work out too well, simply because the snapshot tool in gnome captures the image and pops up a save dialog box with a preview of the snapshot taken. I didn't want to sit and record a macro as they don't always work too well.

That's when the guys at google tossed out "imageMagick" a.k.a "import" at me. This is a utility that comes packaged with Ubuntu (i'm using 10.04 LTS). And it works purely off the command line without any confirmations or dialogs. Here's a sample of how its used:

shell$ import -window root some_filename.png

This saves the image "some_filename.png" into the current working directory. Its really compact and works inline at the shell. The next thing I wanted to do was to minimize all the applications (CTRL + ALT + D, show the desktop basically) before the snapshot was taken and restore them after my work was done. This is where I feel the .NET framework's SendKeys("keys") makes so much sense. You can use that to send any key combination to the OS. Unfortunately, none of that was available to me on Ubuntu, so I found another command line utility (a really really neat one at that!) called "wmctrl". It can be used for a variety of tasks, and here's how it can be used for minimizing and maximizing all windows:

shell$ wmctrl -k on #minimize all windows to show desktop

shell$ wmctrl -k off #maximize all windows

Finally, combining the two, I came up with a small python script, that allowed me to take snapshots of my desktop at regular intervals. The interval, in minutes, can also be specified at the shell. Here's the script.


0 comments:

Post a Comment