Hazel as a Watch Script

Saturday, March 15, 2014

This site is so fast and smooth because it is statically built with Jekyll and a few days ago I released TakeIt, another kind of static site generator. Running a build script after you make a change to a site is slightly inefficient for most of my needs, so I decided to see if I could get a decent watch script built with Hazel. With some help from my resident Hazel expert, C. Nordengren, I came up with a decent solution.

For Jekyll I set the folder to watch to _posts/ and for TakeIt I set it to watch the images/ folder. It keeps track of changes in modification, if the filename has changed, and if the file is new.

The downside is that when you first set this up for an existing project, I recommend letting the rule run once without setting the Run shell script portion. It will rebuild once for every item it matches, which can be processor intensive. I do not add a lot of changes at once for my stuff, so this works for me. This also assumes you are not using the file’s Spotlight comments for anything, which is a pretty safe assumption.

The Hazel Rule

If any of the following conditions are met:

tell application "Finder"
	set theFileDetails to (get properties of theFile)
	if comment of theFileDetails is name of theFileDetails then
		return false
		return true
	end if
end tell

Do the following to the matched file or folder:

# Your build code goes here
# Jekyll Example:
cd /Users/jake/Dropbox/Blog && jekyll build
# TakeIt Example:
cd /Users/jake/Dropbox/TakeIt && rake