Skip to content

Instantly share code, notes, and snippets.

@Darkhogg
Last active October 3, 2015 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darkhogg/2427824 to your computer and use it in GitHub Desktop.
Save Darkhogg/2427824 to your computer and use it in GitHub Desktop.
A small script that uses `scrot` to periodically save screenshots from the desktop.
./capture [-h] [-p period] [-d directory]
* Use **-h** to print the above usage line.
* Use **-p period** to specify the period of the screenshots, in seconds. The *period* argument must be a positive integer. Defaults to *15* seconds.
* Use **-d directory** to specify the directory where the screenshots will be saved. The specified directory must exist and be writable. Defaults to the current working directory (which may be not writable, which won't cause an immediate error, but will make scrot fail).
In order to this script to work, you need to have the `scrot` command installed on your PATH.
The script may be paused by sending an interrupt signal (`SIGINT`), typically by pressing the `CTRL-C` key combination. When the script is paused, you may resume it by pressing `ENTER` or end it with another `CTRL-C`.
The saved images will be named using the current time and date, ensuring they are sorted in chronological order.
#!/bin/bash
# Declarations
declare -i PERIOD=15
declare SAVEDIR="."
declare FNAME
declare -i PAUSED=0
# Print a small help
function PrintHelp {
echo "Usage: $0 [-h] [-p period] [-d directory]"
}
# Log an error
function LogError () {
echo -e "\\033[G\\033[K\\033[0;1m[\\033[31mERR\\033[0;1m]\\033[m $1" >&2
}
# Log an error
function LogWarn () {
echo -e "\\033[G\\033[K\\033[0;1m[\\033[33mWRN\\033[0;1m]\\033[m $1" >&2
}
# Log information
function LogInfo () {
echo -e "\\033[G\\033[K\\033[0;1m[\\033[36mINF\\033[0;1m]\\033[m $1"
}
# Log information
function LogInfoN () {
echo -ne "\\033[G\\033[K\\033[0;1m[\\033[36mINF\\033[0;1m]\\033[m $1"
}
# Handle the interruption
function InterruptHandle () {
echo -ne "\\010\\010\\033[K"
if [ "$PAUSED" -eq 0 ]; then
PAUSED=1
else
LogInfo "Exiting..."
exit 0
fi
}
# Parse command line arguments
while getopts ":p:d:h" Opt; do
case "$Opt" in
"?")
LogError "Invalid option -$OPTARG"
PrintHelp
exit 1
;;
":")
LogError "Missing required argument for -$OPTARG option"
PrintHelp
exit 1
;;
"h")
PrintHelp
exit 0
;;
"p")
PERIOD="$OPTARG"
if [ $PERIOD -le 0 ]; then
LogError "Cannot use a period of $PERIOD, defaulting to 15"
exit 2
fi
;;
"d")
SAVEDIR="$OPTARG"
if [ ! -d "$SAVEDIR" ] || [ ! -w "$SAVEDIR" ]; then
LogError "'$SAVEDIR' is not a directory or is not writable"
exit 3
fi
;;
esac
done
# Test if scrot is available
if ! command -v scrot >/dev/null 2>&1; then
LogError "Required command \\033[0;1mscrot\\033[m could not be found"
exit 4
fi
# Setup the Ctrl-C trap
trap "InterruptHandle" SIGINT
# Start
LogInfo "Capturing to '\\033[0;1m$SAVEDIR\\033[m' every \\033[0;1m$PERIOD\\033[m seconds"
LogInfo "Hit \\033[0;1mCTRL-C\\033[m once to pause, twice to exit"
while [ 1 ]; do
sleep "$PERIOD"
if [ "$PAUSED" -eq 1 ]; then
LogInfo "Capturing paused"
LogInfo "Hit \\033[0;1mENTER\\033[m to resume or \\033[0;1mCTRL-C\\033[m to exit."
read; echo -ne "\\033[T"
PAUSED=0
LogInfo "Capturing resumed"
fi
FNAME="$SAVEDIR/$(date +%Y-%m-%d_%H-%M-%S).png"
LogInfoN "Taking screeshot '\\033[0;1m$FNAME\\033[m'... "
scrot "$FNAME" >/dev/null 2>&1
CODE=$?
if [ $CODE -eq 0 ]; then
echo -e "\\033[0;1;32mOK"
else
echo -e "\\033[0;1;31mFAIL"
LogWarn "Scrot returned error code \\033[0;1m$CODE\\033[m"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment