Skip to content

Instantly share code, notes, and snippets.

@ppmathis
Last active August 4, 2018 18:51
Show Gist options
  • Save ppmathis/7184319 to your computer and use it in GitHub Desktop.
Save ppmathis/7184319 to your computer and use it in GitHub Desktop.
Linux screenshot script, can be used together with a custom shortcut. Based on notify-send, shutter, scp and xclip.
#!/bin/sh
# Options
RANDOM_CHARS=$(< /dev/urandom tr -dc a-z0-9 | head -c10)
FILE_DESTINATION='/home/pmathis/Pictures/Screenshots'
FILE_FORMAT=$RANDOM_CHARS'_%Y-%m-%d_%H.%M.%S.png'
SSH_ALIAS='trinity'
SSH_FOLDER='/home/pmathis/public/screens.snapserv.net'
HTTP_URL='https://screens.snapserv.net'
SUCCESS_ICON='/usr/share/icons/gnome/32x32/status/stock_dialog-info.png'
FAILURE_ICON='/usr/share/icons/gnome/32x32/status/stock_dialog-error.png'
# Check for required applications
command -v notify-send >/dev/null 2>&1 || { echo >&2 "I require 'notify-send' but it's not installed! Aborting."; exit 1; }
command -v shutter >/dev/null 2>&1 || { echo >&2 "I require 'shutter' but it's not installed! Aborting."; exit 1; }
command -v scp >/dev/null 2>&1 || { echo >&2 "I require 'scp' but it's not installed! Aborting."; exit 1; }
command -v xclip >/dev/null 2>&1 || { echo >&2 "I require 'xclip' but it's not installed! Aborting."; exit 1; }
# Check if icons exist
if [ ! -f "$SUCCESS_ICON" ]; then echo >&2 'Missing success icon: '"$SUCCESS_ICON"; exit 2; fi
if [ ! -f "$FAILURE_ICON" ]; then echo >&2 'Missing failure icon: '"$FAILURE_ICON"; exit 2; fi
# It's screenshot time!
shutter --select --exit_after_capture --output="$FILE_DESTINATION"'/'"$FILE_FORMAT" 1>/dev/null 2>&1
LATEST_FILE=$(ls -t "$FILE_DESTINATION" | head -1)
echo "Output directory: $FILE_DESTINATION"
echo "Screenshot: $LATEST_FILE"
# Upload screenshot via SFTP
scp "$FILE_DESTINATION"'/'"$LATEST_FILE" "$SSH_ALIAS"':'"$SSH_FOLDER"'/'"$LATEST_FILE"
if [ $? -ne 0 ]; then
notify-send -u low -i "$FAILURE_ICON" 'Screenshot failed!' 'Could not upload screenshot to '"$SSH_ALIAS"'.'
exit
fi
# Copy link to clipboard and show some fancy notification
echo "$HTTP_URL"'/'"$LATEST_FILE" | xclip -i -selection c
notify-send -u low -i "$SUCCESS_ICON" 'Screenshot uploaded!' 'The link was copied to your clipboard.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment