This example is a layer 1 script to make a special “magic” directory in which image files will be converted automatically therein.
The full script:
This creates a local table of all supported file formats. The file formats are used as keys.
Configures actions to be instant and there is unlimits the amount the conversion to be done at once. Well not unlimited but set the limit pretty high.
This script uses the layer 1 inlet interface altough it greps only single events and not lists. It does this instead of layer 2 as it needs to do common operations for all kind of events.
Ignores directories. As using layer 1 it has to explicitly discard events it does not spawn actions for.
Uses Lua string patterns to extract the file extension from the rest - here called base.
Looks the extension up in the formats table. This can be done, since formats are keys in that table. If not an image format it bails out.
Following actions will done on “Create” and “Modify” events.
This script builds a bash command using a string.
It iterates for all image formats and excludes the one which is the source image.
This is a little trick. It creates Exclusions for the converted images. As this images are not placed in a target directory but right next to the source image in the source directory they would otherwise trigger Create actions as well.
And for every image to be converted adds the calls to the arguments. It uses " || /bin/true " to let the shell continue if one conversion fails. In that it chains the conversion with ‘&&’ they will be called sequentially.
And eventually it spawns the shell doing the conversions and is finished.
For deletions it does technically something similar, but it deletes all other file formats of the image.
and not to forget to nicely discard all other events.
collect is called when the conversions finished. It will remove the temporary excludes again.
And finally use the configuration to watch “magicdir”.