Skip to content

Instantly share code, notes, and snippets.

@thiagopilz
Forked from ernstki/scale.sh
Created October 28, 2022 02:19
Show Gist options
  • Save thiagopilz/7929f95195cb053b49f640bce4210f07 to your computer and use it in GitHub Desktop.
Save thiagopilz/7929f95195cb053b49f640bce4210f07 to your computer and use it in GitHub Desktop.
Detect the last screenshot on the desktop according to a pattern; scale with ImageMagick
#!/usr/bin/env bash
##
## scale the last screenshot on the desktop with ImageMagick
##
## Author: Kevin Ernst (ernstki -at- mail.uc.edu)
## Date: 25 October 2020
## Source: https://gist.github.com/ernstki/8776b3325f0a7abcc0d6080b2160a15c
## License: MIT (https://choosealicense.com/licenses/mit)
##
## Installation:
##
## $ GIST=https://gist.githubusercontent.com/ernstki/8776b3325f0a7abcc0d6080b2160a15c/raw/scale.sh
## $ (curl -L $GIST || wget -O - $GIST) > ~/bin/scale
## $ (cd ~/bin; chmod a+x scale; ln -s scale scalelast)
## $ scale # see if it works
##
#set -u # fail on uninitialized variables
# All of the defaults below can be customized w/ environment variables in your
# ~/.bashrc if your setup is different. What's below is default for the English
# language macOS, but "Screen Shot" is "Bildschirmfoto" for the German
# localization, for example.
# on Mac, default is ~/Desktop, but can be changed with a 'defaults write'
DEFAULT_DIR=${SCALE_SCREENSHOT_DIR:-$HOME/Desktop}
# this is the Mac default; might be localized for you
DEFAULT_PREFIX=${SCALE_SCREENSHOT_PREFIX:-Screen Shot}
# default is PNG on Mac; can be customized with a 'defaults write'
DEFAULT_EXT=${SCALE_SCREENSHOT_EXT:-png}
# default to '800x' (max 800 px horiz.) unless otherwise specified
DEFAULT_GEOM=${SCALE_SCREENSHOT_GEOM:-800x}
# scale image $2 using 'convert -geometry $1'
# see https://imagemagick.org/script/command-line-processing.php#geometry
_scale() {
local geom=${1:?Expected geometry (e.g. '800x', '75%') as first parameter}
local file=${2:?Expected image filename as second parameter}
local base=${file%.*} # strip (shortest) extension
local ext=${file##*.} # strip up to and including last '.'
local suff='_scaled' # suffix to add to scaled image
if [[ ! -f $file ]]; then
echo "ERROR: file '$file' doesn't exist." >&2; return 1;
fi
# run in a subshell w/ trace enabled so you see the command that will run
( set -x; convert -geometry "$geom" "$base.$ext" "$base$suff.$ext" )
}
# return filename of last screenshot in ~/Desktop (without a suffix)
_lastshot() {
local where=$DEFAULT_DIR
local prefix=$DEFAULT_PREFIX
local ext=$DEFAULT_EXT
local newest=
for shot in "$where/$prefix"*[0-9][0-9].$ext; do
# '-nt' means "newer than"
[[ -f $shot ]] && [[ $shot -nt $newest ]] && newest=$shot
done
echo "$newest"
}
# scale the last screenshot in ~/Desktop (without a '_scaled' suffix)
_scalelast() {
local lastshot=$(_lastshot)
local defaultgeom=$DEFAULT_GEOM
_scale "${1:-$defaultgeom}" "${lastshot:?No screenshots found.}"
}
# detect if being sourced and 'export'; otherwise execute internal functions
# based on the name of the script
# h/t: https://github.com/bpkg/term/blob/master/term.sh#L286
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
export -f _scale _lastshot _scalelast
else
# look at the basename of the script and run that function; for example, if
# you symlink it as 'lastshot', runs '_lastshot' with given arguments
_${0##*/} "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment