Skip to content

Instantly share code, notes, and snippets.

@davidfoerster
Last active April 21, 2018 22:19
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 davidfoerster/36456d94a87c3d6867e466a219f84844 to your computer and use it in GitHub Desktop.
Save davidfoerster/36456d94a87c3d6867e466a219f84844 to your computer and use it in GitHub Desktop.
Screenshot & Post-process
#!/bin/bash
set -eu -o noclobber
outfile_format='Screenshot %(%F %T)T'
imgext=png
screenshot='gnome-screenshot -f'
postprocessor=gimp
# Discover user picture directory
user_dirs="${XDG_CONFIG_HOME:-"$HOME/.config"}/user-dirs.dirs"
[ ! -e "$user_dirs" ] || source "$user_dirs"
outdir="${XDG_PICTURES_DIR:-"$HOME/Pictures"}"
[ -e "$outdir" ] || mkdir -p -- "$outdir"
# Generate unique destination file name
touch_noclobber() { { : > "$1"; } 2>&-; }
printf -v outfile "%s/$outfile_format" "$outdir" -1
if ! touch_noclobber "$outfile.$imgext"; then
declare -i i=1
while ! touch_noclobber "$outfile.$i.$imgext"; do
i=$(($i + 1))
done
outfile+=".$i"
fi
outfile+=".$imgext"
trap 'rm -f -- "$outfile"' EXIT
# Take and process screenshot
$screenshot "$outfile" "$@"
trap - EXIT
$postprocessor "$outfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment