Using ImageOptim with guard-shell

For the uninitiated, ImageOptim is a great Mac app that uses several well-known image optimization tools to compress images and help keep file sizes down and Guard is "a command line tool to easily handle events on file system modifications." If you've never tried Guard, I'd encourage you to take some time and check it out. With over 150 plugins now available, there's surely some way that you can use it to optimize your workflow.

I like to pair the guard-shell gem with ImageOptim to help me keep my project's image assets compressed and ready for production. Make sure you have both guard and guard-shell in your Gemfile and set up a watcher in your Guardfile for new or edited images:

gem 'guard'
gem 'guard-shell'
guard 'shell' do
  watch %r{^public/images/.} do |file|
    n file[0], 'Image changed'
    `open #{file[0]} -a ImageOptim`
  end
end

An interesting side effect to note is that guard will catch the file change when ImageOptim is done compressing and re-open it in ImageOptim and continue this loop untill there's nothing else ImageOptim can compress out of the image.