Skip to content

Instantly share code, notes, and snippets.

@shanebrowncs
Last active January 25, 2021 17:49
Show Gist options
  • Save shanebrowncs/f060d9c91e6bc5fd706fde39deb55c3c to your computer and use it in GitHub Desktop.
Save shanebrowncs/f060d9c91e6bc5fd706fde39deb55c3c to your computer and use it in GitHub Desktop.
Local screenshot tool with region select that saves to a command-line configurable directory
#!/bin/bash
#||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Author: Shane Brown <contact at shanebrown dot ca>
# Description: Local screenshot tool with region select that saves to a commandline configurable directory
# Dependencies: maim, exiftool, xclip
# Revision: 3 (2021-01-25)
#||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SAVEDIR="/home/$USER/.config/lscrot"
function print_help() {
echo "usage: $0 [-h help] [-c current dir] [-d] directory"
}
function changeDirectory() {
TRIMMED_DIR=$(realpath $1)
if [ ! -d "$TRIMMED_DIR" ]; then
echo "Directory \"$1\" does not exist"
return 1
fi
# MODIFY CONFIG LINE
sed -i "/OUTPUT/c\\OUTPUT=$TRIMMED_DIR" "$SAVEDIR/lscrot.conf"
# SOURCE NEW CONFIG
. "$SAVEDIR/lscrot.conf"
if [ "$OUTPUT" = "$TRIMMED_DIR" ]; then
echo "Changed directory"
return 0
else
echo "Failed to read in change, I would check out $SAVEDIR/lscrot.conf before continuing"
return 1
fi
}
function generateDefaultConfig() {
if ! mkdir -p "$SAVEDIR"; then
echo "Could not create config directory, aborting."
exit 1
fi
if echo -e "# BASH FILE, VAR FORMAT: \"KEY=VALUE\" (NO SPACES)\nOUTPUT=\"/home/$USER/Pictures\"" > "$SAVEDIR/lscrot.conf"; then
echo "Config saved."
echo -e "\nImage save directory set to /home/$USER/Pictures.\nChange by running \"lscrot -d \"<DIRECTORY>\"."
else
echo "echo: ERROR \"$?\", failed to save config file, aborting."
exit 1
fi
}
function generateFileName() {
COUNT=1
DATESTR=$(date +%Y-%m-%d)
SAVEFILENAME=$(printf "${DATESTR}_%02d.png" $COUNT)
while [ -f "$OUTPUT/$SAVEFILENAME" ]; do
COUNT=$((COUNT+1))
SAVEFILENAME=$(printf "${DATESTR}_%02d.png" $COUNT)
done
echo "$SAVEFILENAME"
}
# SCRIPT BEGIN
if [ -n "$XDG_CONFIG_HOME" ]; then
SAVEDIR=$XDG_CONFIG_HOME/lscrot
elif [ ! -d "$SAVEDIR" ]; then
echo -n "XDG_CONFIG_HOME is empty, we're going to save our config in '$SAVEDIR', is this okay? [y/n]: "
read -r RESP
case $RESP in
"y" | "Y" | "yes" | "YES")
echo "Continuing.."
;;
*)
echo "Cannot save config, aborting"
exit 1
;;
esac
fi
if [ ! -f "$SAVEDIR/lscrot.conf" ]; then
generateDefaultConfig
fi
CONFIG_FILE="$SAVEDIR/lscrot.conf"
# Source config
. "$CONFIG_FILE"
while getopts "h?cd:" opt; do
case "$opt" in
h|\?)
print_help
exit 1
;;
d)
changeDirectory $OPTARG
exit $?
;;
c)
if [ -n "$OUTPUT" ]; then
echo "OUTPUT DIRECTORY: $OUTPUT"
else
echo "No output directory set"
fi
exit 0
;;
esac
done
OUTFILE=$(generateFileName)
if ! maim -s > "/tmp/$OUTFILE"; then
echo "Maim returned $?, aborting.."
exit 1
fi
if ! exiftool -overwrite_original -all= "/tmp/$OUTFILE" &> /dev/null ; then
rm -f "/tmp/$OUTFILE"
echo "exiftool could not remove exif data, will not continue."
exit 1
fi
if ! mv "/tmp/$OUTFILE" "$OUTPUT"; then
echo "Failed to move resulting file, it's exif data is cleared and exists at \"/tmp/$OUTFILE\""
exit 1
fi
echo "DONE! File \"$OUTFILE\" created at \"$OUTPUT\""
if ! echo "file://$OUTPUT/$OUTFILE" | xclip -selection clipboard; then
echo "Failed to copy to clipboard"
exit 1
fi
echo "Copied location to clipboard"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment