From b6611b94ad036b25fad7f5aaa10778d8056aa55d Mon Sep 17 00:00:00 2001 From: saul Date: Sun, 23 Mar 2014 20:36:06 -0400 Subject: [PATCH] Simplify command line processing by adding a 'with-files' macro. --- plug-ins/script-fu/scripts/script-fu-util.scm | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plug-ins/script-fu/scripts/script-fu-util.scm b/plug-ins/script-fu/scripts/script-fu-util.scm index 546b1ed8ef..09af7512e9 100644 --- a/plug-ins/script-fu/scripts/script-fu-util.scm +++ b/plug-ins/script-fu/scripts/script-fu-util.scm @@ -44,3 +44,41 @@ ) ) +; Allow command line usage of GIMP such as: +; +; gimp -i -b '(with-files "*.png" )' +; +; where is the code that handles whatever processing you want to +; perform on the files. There are three variables that are available within +; the : 'basename', 'image', and 'layer'. The 'basename' is the +; name of the file with its extension removed, while the other two +; variables are self-explanatory. You basically write your code as though +; it were processing a single 'image' and the 'with-files' macro applies +; it to all of the files matching the pattern. +; +; For example, to invert the colors of all of the PNG files in the +; start directory: +; +; gimp -i -b '(with-files "*.png" (gimp-invert layer) \ +; (gimp-file-save 1 image layer \ +; (string-append basename ".png") \ +; (string-append basename ".png") ))' + +(define-macro (with-files pattern . body) + (let ((loop (gensym)) + (filenames (gensym)) + (filename (gensym))) + `(begin + (let ,loop ((,filenames (cadr (file-glob ,pattern 1)))) + (unless (null? ,filenames) + (let* ((,filename (car ,filenames)) + (image (catch #f (car (gimp-file-load RUN-NONINTERACTIVE + ,filename + ,filename )))) + (layer (if image (car (gimp-image-get-active-layer image)) #f)) + (basename (unbreakupstr (butlast (strbreakup ,filename ".")) "."))) + (when image + ,@body + (gimp-image-delete image))) + (,loop (cdr ,filenames)))) + (gimp-quit 0))))