I am exploring the possibility of using Hazel to
- upload locally changed files
- automatically upload images to my server in order to include them on the blog with a link.
I have easily found blog posts on the web indicating how to do this using Hazel and a Bash script that opens an FTP connection (my free host only accepts FTP, which is not very secure or nice, and I will have to change my hosting solution soon, I think). Unfortunately, these scripts were not correctly working because of a small but deadly error (at least given the rules used by Hazel and Bash currently under OSX ML).
I am sharing below the commented script I am using now to update my web site’s folder tree.
Hazel has a rule defined for my web site’s root folder.
The first rule tells Hazel to go into subfolders:
- It’s condition part tells “Kind” “is” “Folder”;
- and its action tells “Run rules on folder contents”.
The real job is done by the second rule, which is called in all subfolders on the tree. It’s condition part tells:
- “Subfolder Depth” “is less than” “3” (which is enough in my case; in lower levels, I have files that never change);
- “Date Last Modified” “is less than” “Date Last Matched”
And its action part runs the shell script given below (check the comments in the script).
I will write another set of rules for a folder where I will locally put the image files that I use for this blog and Hazel will copy them to a dedicated public folder on my web server and copy the full URL of the image file to a log file (the last part of the script). It would be enough to paste the last line of this file in an image tag in Markdown.
#!/bin/bash
# A rule to check the changed files in the subfolders of
# /Users/you/Documents/tests/
# and upload them in the corresponding subfolders of
# /public_html/test
# on the server
# get the full path and name of the file
ff=$1
# get the file name only
filename1="$(postname $1)"
# and folder path
foldername="$(dirname $1)"
# filename without the extension
filename="${filename1%%.*}"
# and the path as relative to the root of the folder supervised by Hazel
# this will be used during the FTP operations
relfoldername="${ff#/Users/you/Documents/tests/}"
relfoldername="${relfoldername%%/$filename1}"
# Growl message to check the paths
/usr/local/bin/growlnotify -t "Document" -m "full name: $ff - foldername: $foldername - filename: $filename1 - relfoldername: $relfoldername"
#Going to the folder to which the changed file belongs
cd "$foldername"
# checking that we are where we should be
#/usr/local/bin/growlnotify -s -m "Current folder: $PWD"
# The file can now be put on the server
# FTP upload
# FTP login details
# Do not use quote marks in the variables that will be used by the FTP!!
HOST=ftp.yourserver.com
USER=your-user-name
PASS=your-password
#FTP login and upload
/usr/bin/ftp -in $HOST << EOF
user $USER $PASS
put $filename1 /public_html/test/$relfoldername/$filename1
EOF
/usr/local/bin/growlnotify -s -t "FTP" -m "File $filename transferred!"
# Copy the address of the image to a log file
echo “http://your-web-server/$relfoldername/$filename1” >> ~/Documents/Hazel-Uploads.txt
exit 0
Note: The problem with the scripts I found on the web was the quotation marks used for the FTP server, username, and password. Once these marks were deleted, the script started to work as it was supposed to.