Watch a folder to optimize pictures


The Image converter application is great. However, we can work faster converting pictures without having to drag and drop them into the application icon.

Instead, we'll setup a local img folder with a watching script. As soon as we add new files into this folder, they will get processed by the web optimization process (resize and jpg compression).

Creating the plist file

The plist file is the one telling macOS: "Watch this folder, and whenever something get changed, execute this command".

We create the following file:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

We create the img and watch_img folders

cd ~/Documents/__Boulot/__yoricm/__dl
mkdir watch_img
mkdir img

Bash script

We create the following script:



for f in ../img/*
  [ -f "$f" ] || break
  /usr/local/bin/convert -resize 980x980 -quality 60 "$f" "${newf}"
  mv "${newf}" "../site/img/${newf##*/}"

Loading the plist file

We load the plist file so it's ready to work:

launchctl load ~/Library/LaunchAgents/com.yoricm.watchimg.plist`