Skip to content

Instantly share code, notes, and snippets.

@rbeer
Last active January 23, 2022 06:04
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 rbeer/c78ac54a7bdcd6ce2e4d58edde60ec92 to your computer and use it in GitHub Desktop.
Save rbeer/c78ac54a7bdcd6ce2e4d58edde60ec92 to your computer and use it in GitHub Desktop.
Sna[r]pshooter - Take screenshots of specific windows
#!/usr/bin/env bash
C_RED="\e[91m"
C_RESET="\e[0m"
function print_help {
echo "sna[r]pshooter - 1.0.0"
echo "Takes screenshot of a selected window, the whole screen or a selected region."
echo -e "Depends: ImageMagick's 'import', 'xwininfo'\n"
if [ -n "$1" ]; then
print_error "$1"
fi
echo "Usage: ./snap.sh [-g] [-h] [-r] [-t] [-w] [-s seconds] <filename>.png"
echo " -g | Open taken screenshot in GIMP."
echo " -h | You're looking at it!"
echo " -r | Capture the whole screen. This includes all screens in a multi-screen setup."
echo " -t | Add timestamp to filename"
echo " -w | Capture a selected window."
echo " -s seconds | Wait x seconds before capturing."
exit 1
}
function check_dependency {
which $1
if [ $? -eq 1 ]; then
print_help "Missing dependency: $1"
fi
}
function print_error {
echo -e "error: ${C_RED}$1${C_RESET}"
}
MODE=DIRECT
check_dependency "import"
while getopts ":s:rtwghf:" opt; do
case $opt in
\?)
print_help $OPTARG
;;
:)
print_help $OPTARG
;;
h)
print_help
;;
g)
GOGIMP=true
;;
s)
if [[ $OPTARG =~ ^[0-9]*$ ]]; then
SLEEPBEFORE=$OPTARG
else
print_help "--- Invalid option: -s $OPTARG ---"
fi
;;
t)
ADD_DATE=1;
;;
w)
MODE=XWININFO
;;
r)
MODE=ROOT
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" ]; then
print_help "No filename given"
fi
if [ $ADD_DATE ]; then
FILENAME="$(pwd)/$1_$(date +%F_%R).png"
else
FILENAME="$(pwd)/$1.png"
fi
if [ "$MODE" == "XWININFO" ]; then
check_dependency "xwininfo"
echo 'Please select window to capture...'
HID=$(xwininfo | grep -o 'Window id: \([[:digit:]abcdefx]*\)' | cut -f3 -d" ")
echo 'Got:' $HID
fi
if [ "$MODE" == "ROOT" ]; then
HID="root"
fi
if [ $SLEEPBEFORE ]; then
echo "Waiting $SLEEPBEFORE seconds before taking screenshot..."
sleep $SLEEPBEFORE
fi
if [[ "$MODE" == "XWININFO" || "$MODE" == "ROOT" ]]; then
echo 'Taking screenshot...'
import -window $HID $FILENAME
echo 'Saved to' $FILENAME
else
import $FILENAME
fi
if [ $GOGIMP ]; then
gimp $FILENAME & disown
fi
echo 'DONE. Cya.'
@rbeer
Copy link
Author

rbeer commented Dec 16, 2016

Add: -r option to capture whole screen (rev. 3)

@rbeer
Copy link
Author

rbeer commented Apr 21, 2021

Add: dependency checks and colored errors
Change: filename from -f option to last parameter

(rev. 4)

@rbeer
Copy link
Author

rbeer commented Jan 23, 2022

Add: snarpshooter -t flag logic
(rev. 5 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment