Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 60 lines (51 sloc) 1.48 KB
#!/bin/bash
# Copyright: Christoph Dittmann <github@christoph-d.de>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
#
# This script takes a screenshot of a window. It saves the
# screenshots as N.png and N_small.png, where N is the least positive
# integer so that no file will be overwritten. N.png is the original,
# the other file is a smaller version.
#
# The scaled down versions are suitable for Anki cards.
set -e -u
if [[ $# -ne 1 ]]; then
echo "Please click on a window."
WINDOW_ID="$(xwininfo | grep -m 1 'Window id: ' | sed 's/^.*Window id: \([^ ]*\) .*$/\1/')"
echo "Window id: $WINDOW_ID"
else
WINDOW_ID="$1"
if [[ $WINDOW_ID = '--foreground-window' ]]; then
WINDOW_ID=$(xdotool getwindowfocus)
fi
fi
i=1
while [[ -f $i.png || -f ${i}_small.png ]]; do
let ++i
done
import_window() {
if ! import -silent -window "$1" "$2"; then
echo "Import failed. Invalid window ID?"
exit 1
fi
}
too_small() {
[[ $(du -b "$1" | cut -f 1) -lt 4096 ]]
}
tries=6
while :; do
let --tries
import_window "$WINDOW_ID" "$i.png"
if ! too_small "$i.png" || [[ $tries -eq 0 ]]; then
break
fi
echo "Import seems to have failed. Will try again ($tries tries remaining)..."
sleep 0.1
done
if too_small "$i.png"; then
echo "Import does not seem to succeed. Sorry about that."
exit 1
fi
echo "Filename: $i.png ${i}_small.png"
convert $i.png -resize 600x300 -quality 99 ${i}_small.png
exit 0