How To Take Screenshots Of VirtualBox VMs

Version 1.0
Author: Falko Timme
Follow me on Twitter

VirtualBox is a great virtualization solution, however if lacks one big feature: the ability to take screenshots of a virtual machine. Of course, you can use the host operating system's print screen feature, but this wil ltake a picture of the whole workspace, including window decorations, etc. which you will have to remove in your iage editing software which is a tedious task. This tutorial describes how you can create a little script that automatically takes a screenshot of a VirtualBox VM when you click on it; it makes use of the VirtualBox command line tool VBoxManage.

This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I will create the script vboxscreenshot.sh and place it on the desktop so that you can easily double-click it to create a screenshot. Screenshots will be placed on the desktop (you can specify another output directory in the script); file names will be like 1.png, 2.png, 3.png, etc., i.e., the script automatically increments the file name so that you know later on in which order the screenshots were taken.

I'm assuming that you run just one VM at a time because the script tries to find out the name of the running VM automatically. Alternatively, you can hardcode the name of the VM of which you want to take screenshots.

 

2 Creating vboxscreenshot.sh

Open gedit...

... and create a new file with the following contents:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

vboxmanage=/usr/bin/VBoxManage

# Specify the output directory
outputdir=~/Desktop

# Find the next free image ID (1.png, 2.png, 3.png, etc.)
cd $outputdir
list=([0-9]*)
last=${list[@]: -1}
nextnum=$((10#${last%%[^0-9]*} + 1))

# Find out the name of the running VM; this assumes that you have just one VM running at a time
for uuid in $($vboxmanage list runningvms --long | grep "UUID:            " | awk 'BEGIN{FS="UUID:            "}{print $2}')
do
  vm=$($vboxmanage showvminfo $uuid      | sed -e'/^USB Device Filters:/,$ d' | grep "Name:"  | awk 'BEGIN{FS="Name:            "}{print $2}')
done

# Alternatively, you can specify the name of the VM here of which you want to take a screenshot
#vm="Ubuntu 12.04 Desktop"

$vboxmanage controlvm "$vm" screenshotpng $outputdir/$nextnum.png
exit 0

You can adjust the output directory in the variable outputdir if you don't want to place screenshots on the desktop. If you want to specify the name of the virtual machine, uncomment the #vm="Ubuntu 12.04 Desktop" line and replace Ubuntu 12.04 Desktop with the name of the virtual machine; otherwise the script tries to determine the name of the running virtual machine by itself (it is important in this case that you run just one virtual machine at a time).

Save the file as vboxscreenshot.sh on the desktop:

Next right-click the file and select Properties:

Go to the Permissions tab and check the Allow executing file as program checkbox:

That's it! Now start a VirtualBox VM. If you want to take a screenshot of the VM, double-click the vboxscreenshot.sh icon on the desktop. Click Run when you are asked Do you want to run "vboxscreenshot.sh", or display its contents?:

Afterwards you will find the screenshot on your desktop (named 1.png in this example):

 

3 Taking Screenshots Without Additional Dialogue

It can be quite annoying to see that Do you want to run "vboxscreenshot.sh", or display its contents? dialogue each time you want to take a screenshot. To get rid of that dialogue, open Nautilus...

... and go to Edit > Preferences:

Go to the Behavior tab and select Run executable text files when they are opened in the Executable Text Files section:

From now on screenshots are created automatically without any dialogue when you double-click the vboxscreenshot.sh icon.

 

Share this page:

2 Comment(s)