Another year, another version. My growth as a programmer strikes me when I compare previous versions with the latest code. Less code which works better. You may compare the last three versions together on GitHub.
#!/usr/bin/env zsh
#
# Functions
#
#
# Check if Photograph is (Approximately) Square
#
# While it was originally written to detect explicitly square images, it will
# now detect squareness within a range of 20 pixels. Uses the absolute value
# of the size.
#
function is_square {
local WIDTH=$(identify -format '%w' $1)
local HEIGHT=$(identify -format '%h' $1)
local SIZE=`expr $WIDTH - $HEIGHT`
[[ ${SIZE#-} -le ${2:=20} ]]
echo $?
}
#
# Create Directory from Date in Filename
# Images imported from Dropbox are named in format "2015-12-27 22.13.26.jpg"
#
function send_to_dated_directory {
local FILENAME_DATE=${1:0:10}
local DATE_REGEX='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
if [[ $FILENAME_DATE =~ $DATE_REGEX ]]; then
mkdir $FILENAME_DATE > /dev/null 2>&1
mv "$1" $FILENAME_DATE
fi
}
#
# Move File to Appropriate Location
#
# 1. To a dated folder if not an Instagram image.
# 2. To the Instagram folder otherwise.
#
function move_file {
local EXTENSION_REGEX='^jpg$'
if [[ ${1##*.} =~ $EXTENSION_REGEX ]] && [[ $(is_square $1) == 0 ]]; then
mv $1 "$INSTAGRAM_FOLDER"
else
send_to_dated_directory $1
fi
}
#
# Variables
#
UPLOADS_FOLDER="${HOME}/Dropbox/Camera Uploads/"
INSTAGRAM_FOLDER="${HOME}/Dropbox/Photos/Instagram/"
cd "$UPLOADS_FOLDER"
for FILE in *.*; do
if [[ ! -f "$FILE" ]]; then
continue
fi
NICENAME=$((tr '[A-Z]' '[a-z]' | tr ' ' '_') <<< "$FILE")
mv "$FILE" $NICENAME && FILE=$NICENAME
case ${FILE##*.} in
gif|png) rm $FILE;;
*) move_file $FILE;;
esac
done