Metalcoder
Metalcoder

Reputation: 2262

Using unless directive in my Script-Fu prevents it from working

The following script is meant to be invoked via Gimp CLI interface, and it changes the color modes of all PNGs in the current directory to indexed:

(define (script-fu-batch-indexify pattern)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let* ((filename (car filelist))
             (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
             (drawable (car (gimp-image-get-active-layer image))))
        (gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE 256 0 0 "")
        (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
        (gimp-image-delete image))
      (set! filelist (cdr filelist)))))

This script works well, but it requires that each PNG in the currently directory isn't indexed. It will abort immediately when it finds a indexed PNG. My intention is to make it skip indexed PNGs. I believe the best way to do so is like this:

(define (script-fu-batch-indexify pattern num-cols)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let* ((filename (car filelist))
             (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
             (drawable (car (gimp-image-get-active-layer image))))
        (unless (gimp-drawable-is-indexed image) ; Now it does not work anymore...
          (gimp-image-convert-indexed image NO-DITHER MAKE-PALETTE num-cols 0 0 "")
          (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
          (gimp-image-delete image)))
      (set! filelist (cdr filelist)))))

But doing so prevents the script from working! It executes without any error, and without indexing any non-indexed PNGs. I tried using if, with the same results. So, what am I getting wrong here?

(I'm using Gimp 2.8.22 over Linux Mint 19.3.)

Upvotes: 0

Views: 222

Answers (2)

LaciP
LaciP

Reputation: 1

Use print like this:

(display (string-append "\n Indexed:" (number->string (car (gimp-drawable-is-indexed drawable))) "< \n"))
  1. You have to create a string for (display ..)

    Look first in PDB for gimp-drawable-is-indexed, returns int32 =>[(number->string] but all funcions returning lists => [(car (..]

  2. (gimp-drawable-is-indexed .) requires a layer instead of an image.

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236114

The script is not coded in the way Scheme is usually written: we try real hard to avoid set! operations, what you did can be implemented using recursion instead of a while loop - in a functional programming style, that's the recommended way.

Having said that, I don't see any obvious flaw with your code, are you sure that this expression actually is false for any image?

(gimp-drawable-is-indexed image)

Try printing the above result just before the unless expression, and let's see what it shows. Start Gimp from a terminal and look at the console:

(print (gimp-drawable-is-indexed image))
(unless (gimp-drawable-is-indexed image)

Upvotes: 1

Related Questions