sebf
sebf

Reputation: 2952

Why is my conditional statement being ignored?

I am trying to write a scheme script for GIMP 2.8, which will resize and rename/copy files. As part of this I would like to change the output file name based on a boolean set in the script.

My conditional however, is being ignored. Here is an excerpt of the script (based on one in this thread):

(define (script-fu-batch-resize globexp globext src) 
 (define (resize-img n f newx suffix srcdir removetailingunderscore) 
  (let* ((fname (car f))

     ;get path, short name + extension (remember to remove source dir to REPLACE with suffix!)
    (fullname (car (last (strbreakup fname "\\"))))
    (filepath (substring fname 0 (- (string-length fname) (string-length (string-append srcdir fullname)))))
    (name0 (car (strbreakup fullname ".")))
    (name1 (substring name0 0 (- (string-length name0) 1)) )

    (when 
    (> 1 0)
    (
        (name0 (name1) )
    )
    )

    (extension (cadr (strbreakup fullname ".")))

     (img   (car (gimp-file-load 1 fname fname)))) 
    (gimp-image-undo-disable img) 
    (let* 
      ( 
         (oldx (car (gimp-image-width  img))) 
         (oldy (car (gimp-image-height img))) 
         (newy (/ oldy (/ oldx newx))) 
       ) 
       (gimp-image-scale img newx newy)         ;changed for Gimp v2.6
     )              
    (gimp-file-save 1 img (car (gimp-image-get-active-drawable img)) (string-append filepath suffix "\\" name1 "." extension) fname) 
        (gimp-image-delete img) 
   ) 
   (if (= n 1) 1 (resize-img (- n 1) (cdr f) newx suffix srcdir removetailingunderscore)) 
  )
 (define sourcepattern (string-append globexp src globext))
 (define files (file-glob sourcepattern 0))                     ;changed for Gimp v2.6 
 (resize-img (car files) (car (cdr files)) 55 "micro" src 1) 

)

I have tried using when and if-else but no matter what no statement inside the conditional block is executed (i.e. if I had the same statement for true and false in the if, neither would be executed). My conditional is being completely ignored but I do not know why. GIMP does not report any syntax errors.

I would suspect I cannot have a conditional inside the define statement, except there is one that works right below it.

Can anyone see what is wrong? Why is my conditional being ignored?

Upvotes: 1

Views: 1081

Answers (1)

Óscar López
Óscar López

Reputation: 236114

There are several problems with the when expression:

  • It uses a couple of unnecessary parenthesis
  • The condition (> 1 0) is always true, therefore it's useless
  • That's not the way to change the value of a variable, use set!
  • Move the whole when expression outside the variable definition part of the let*, and make it the first statement in the body of the let*

What you need to do is something like this:

(define (script-fu-batch-resize globexp globext src) 
  (define (resize-img n f newx suffix srcdir removetailingunderscore) 
    (let* ((fname (car f))
           (fullname (car (last (strbreakup fname "\\"))))
           (filepath (substring fname 0 (- (string-length fname) (string-length (string-append srcdir fullname)))))
           (name0 (car (strbreakup fullname ".")))
           (name1 (substring name0 0 (- (string-length name0) 1)))
           (extension (cadr (strbreakup fullname ".")))           
           (img   (car (gimp-file-load 1 fname fname))))
      (when (> 1 0)
        (set! name0 name1))
      <rest of the body>)))

Upvotes: 5

Related Questions