Skip to content

Instantly share code, notes, and snippets.

@tjluoma
Created June 11, 2014 05:50
Show Gist options
  • Select an option

  • Save tjluoma/d8a84e0d6bd4f740f8df to your computer and use it in GitHub Desktop.

Select an option

Save tjluoma/d8a84e0d6bd4f740f8df to your computer and use it in GitHub Desktop.
#!/bin/zsh -f
# Purpose:
#
# From: Tj Luo.ma
# Mail: luomat at gmail dot com
# Web: http://RhymesWithDiploma.com
# Date: 2014-06-07
NAME="$0:t:r"
if ((! $+commands[po.sh] ))
then
# note: if po.sh is a function or alias, it will come back not found
echo "$NAME: po.sh is required but not found in $PATH" | tee -a "$HOME/Dropbox/$NAME.log"
fi
if ((! $+commands[jhead] ))
then
po.sh "No jhead installed on `hostname`. Use 'brew install jhead'"
fi
# Again this is the same directory as before
SOURCE="$HOME/Dropbox/Apps/Attachments/[email protected]"
# Change this to wherever your website folder is
DIR="/Library/Server/Web/Data/Sites/your.domain.tld"
# an exit strategy in case things go wrong
function die
{
po.sh "$NAME [fatal]: $@"
exit 1
}
# first we want to make sure .DS_Store files are gone
find "$SOURCE" -name .DS_Store -delete
# now we want to look at all the rest of the files
find "$SOURCE" -type f -print |\
while read line
do
# get the extension of the file. Lowercase.
EXT="$line:e:l"
# get the `:h`ead aka parent directory
# Why? Here’s the genius part: we can’t set the name
# of the image on our iOS device, but we _CAN_ set
# something else: the _Subject line_ of the email that
# we send to SendToDropbox. Remember we set that as
# number ‘3’ in the image above? Well, now we’re
# going to take that and use it as the new filename/title
# for your image(s).
TITLE="$line:h"
# lowercase the title/filename. Replace anything that
# isn’t a letter or digit or hyphen and replace it
# with a hyphen. So your Subject line: ”My Great Picture”
# will become “my-great-picture”
TITLE=`echo "$TITLE:t:l" | tr -sc '[0-9a-z]-' '-' | sed 's#-$##g'`
# now we save the filename as a variable
SHORT="$TITLE.$EXT"
# What do we do if the file already exists?
# Add a number to it, and check to see if it
# still exists. If it does, increment the number.
# Repeat until it no longer exists.
# This also lets us send one email with multiple
# pictures attached and get several URLs back for
# each one such as:
# my-great-pictures.jpg
# my-great-pictures-1.jpg
# my-great-pictures-2.jpg
while [[ -e "$DIR/$SHORT" ]]
do
((COUNT++))
SHORT="$SHORT:t:r:l-$COUNT.$EXT"
done
# Now we move the file to the new directory and new name
mv -vn "$line" "$DIR/$SHORT" ||\
die "Failed to rename $line to $DIR/$SHORT"
# make sure the rotation is the way we want it
[[ "$EXT" == "jpg" ]] && \
jhead -autorot "$DIR/$SHORT"
# now you send the URL to yourself
po.sh "http://your.domain.tld/$SHORT"
done
# Once we're done, let's delete all the empty directories
find "$SOURCE" -type d -exec rmdir {} \;
exit
#
#EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment