views:

365

answers:

2

Using Gimp 2.6.6 for MAC OS X (under X11) as downloaded from gimp.org.

I'm trying to automate a boring manual process with Script-Fu. I needed to parse the image file name to save off various layers as new files using a suffix on the original file name.

My original attempts went like this but failed because (string-search ...) doesn't seem to be available under 2.6 (a change to the scripting engine?).

(set! basefilename (substring filename 0 (string-search "." filename)))

Then I tried to use this information to parse out the base file name using regex but (re-match-nth ...) is not recognized either.

(if (re-match "^(.*)[.]([^.]+)$" filename buffer)
 (set! basefilename (re-match-nth orig-name buffer 1))
 )

And while pulling the value out of the vector ran without error, the resulting value is not considered a string when it is passed into (string-append ...).

(if (re-match "^(.*)[.]([^.]+)$" filename buffer)
 (set! basefilename (vector-ref buffer 1))
 )

So I guess my question is, how would I parse out the base file name?

+2  A: 

Context

GIMP 2.6.6 Windows Vista SP2

Goal

Extract the basename of the original filename without its extension.

Symptom

Error: eval: unbound variable: re-match-nth

Possible suggestion

GIMP menu "Filters" > "Script-Fu" > "Console"

In the input box, paste the following Script-Fu definition of function then hit the ENTER key:

(define (filename-basename orig-name)
    (car (strbreakup orig-name "."))
    ; Nimmzo 09/09/30: the string split function strbreakup is defined 
    ; in the compatibility file from SIOD to TinyScheme:
    ; C:\Program Files\GIMP\share\gimp\2.0\scripts\script-fu-compat.init
) ; end  filename-basename

To test the function, enter:

(filename-basename "screen.xcf")

The Script-Fu Console answers:

"screen"
Nimmzo
Thanks! strbreakup was exactly what I was looking for.
ongle
+2  A: 

Not really a correct solution:

> (filename-basename "this.is.a.long.filename.jpg")

"this"

A better implementation:

(define (morph-filename orig-name new-extension)
 (let* ((buffer (vector "" "" "")))
  (if (re-match "^(.*)[.]([^.]+)$" orig-name buffer)
   (string-append (substring orig-name 0 (car (vector-ref buffer 2))) new-extension)
  )
 )
)
Fox2
+1 Fair point and thanks for the improved code
ongle