How to take a screenshot of a whole desktop with app menu selection?

  • Applications > Accessories > Take Screenshot > Grab the whole desktop > Grab after a delay of : 5 seconds (say)

alt text

  • Do your Applications menu selection. Wait.

Actually, it is possible to do it without the delay, but with some hacks instead. I have written a small script which will allow you to do that without the delay. This is a big hack, but it works and is certainly (for me) more preferable than using the delay.

#!/bin/bash
######################################################################################
# Simple script to enable users to make screenshots of tooltips/menus/etc...         # 
# without timers                                                                     #
######################################################################################

######################################################################################
# Configuration Section (defaults)                                                   #
######################################################################################
SCREENSHOT_COMMAND="shutter -s"

# The keys can be found out using xinput test "keyboard name"
MODIFIER_KEY=133 #The <Super> Key (aka. Meta or Windows Key)f
CANCEL_KEY=54 # C
CAPTURE_KEY=27 # R

DAEMON_MODE="false" # change to true if you want to keep the script running after the screenshot was taken
VERBOSE="true" #Change this to any value if you dont want to have notifications

######################################################################################

######################################################################################
# Command parsing                                                                    #
######################################################################################

function usage {
    echo "$0 [-hemrcdn]"
    echo "-h prints this message"
    echo "-e <command> - execute that command instead of shutter"
    echo "-m <int> - The modifier key to use. Use xinput test <keyboar> to find out what is what"
    echo "-r <int> - The key to use for capture."
    echo "-c <int> - The key used for cancelling (only valid in non daemon mode)"
    echo "-d - daemon mode. Will keep on running after a screenshot was taken. to kill the daemon, use \"killall xinput\""
    echo "-n - disables notifications"
    exit;
}

while getopts "he:m:r:c:dn" flag
do
    if [ "$flag" == "h" ]; then
        usage
    fi
    if [ "$flag" == "e" ]; then
        SCREENSHOT_COMMAND=$OPTARG
    fi
    if [ "$flag" == "m" ]; then
        CAPTURE_KEY=$OPTARG
    fi
    if [ "$flag" == "r" ]; then
        SCREENSHOT_COMMAND=$OPTARG
    fi
    if [ "$flag" == "c" ]; then
        CANCEL_KEY=$OPTARG
    fi
    if [ "$flag" == "d" ]; then
        DAEMON_MODE="true"
    fi
    if [ "$flag" == "n" ]; then
        VERBOSE="false"
    fi
done

######################################################################################

KEYBOARDS=`xinput list | grep "slave" | grep "keyboard" | sed "s/[^a-zA-Z]*\(.*\)id=.*/\1/" | sed "s/[\t ]*$//"`

function run {
    MODIFIER_PRESSED="false"
    while read line;
    do
        COMMAND=`echo $line | awk '{print $2;}'`
        KEY=`echo $line | awk '{print $3;}'`
        if [ "$KEY" == "$MODIFIER_KEY" ]; then
            if [ "$COMMAND" == "press" ]; then
                MODIFIER_PRESSED="true"
            else 
                MODIFIER_PRESSED="false"
            fi
        fi 
        if [ "$KEY" == "$CAPTURE_KEY" -a "$MODIFIER_PRESSED" == "true" -a "$COMMAND" == "press" ]; then
            bash -c $SCREENSHOT_COMMAND
            if [ "$VERBOSE" == "true" ]; then
                notify-send "Taking Screenshot"     
            fi
            if [ "$DAEMON_MODE" == "false" ]; then
                quit
            fi
        fi
        if [ "$KEY" == "$CANCEL_KEY" -a "$MODIFIER_PRESSED" == "true" -a "$COMMAND" == "press" -a "$DAEMON_MODE" == "false" ]; then
            if [ "$VERBOSE" == "true" ]; then   
                notify-send "Canceling Screenshot"
            fi
            quit
        fi
    done;
}

function quit {
    killall -9 xinput
    exit
}

if [ "$VERBOSE" == "true" ]; then
    notify-send "Screenshot script waiting. Press Meta + R to capture the screenshot"
fi
IFS=$'\n'
for i in $KEYBOARDS
do
    unbuffer xinput test "$i" | run & 
done

Before you can actually use the script (on ubuntu), you need to make sure you have xinput and unbuffer. To do that simply do:

sudo apt-get install xinput expect-dev

Then you can run the script. Run it first with the -h option to see the possible configuration options. By default, the script will only work once and you have to restart the script after every screenshot (e.g. by a keyboard shortcut). This is because the script might have a performance inpact. If you want to run it as a "daemon" run it with the -d option.

By default it will also use shutter. If you want to use something else, use the -e option, e.g. script.sh -c "ksnapshot"

By default, the capture button will be Meta + R. you can change that with the configuration options.