如何使用Script-Fu解析出基本文件名

11

我正在使用从gimp.org下载的Gimp 2.6.6版本,用于MAC OS X(在X11下运行)。

我试图使用Script-Fu自动化一个繁琐的手动过程。我需要解析图像文件名,并使用原始文件名上的后缀将各个图层保存为新文件。

我的最初尝试是这样的,但失败了,因为(string-search ...)在2.6版本下似乎不可用(是脚本引擎的更改?)。

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

接着我尝试使用这份信息来使用正则表达式解析基础文件名,但是(re-match-nth ...)也无法被识别。

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

当从向量中获取值时没有出现错误,但是将结果值传递到 (string-append ...)函数时,该值并不被视为字符串。

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

所以我想问的是,我该如何解析出基本文件名?

6个回答

8

不是一个正确的解决方案:

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

"this"

更好的实现方式:

(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)
  )
 )
)

如果文件扩展名缺失(morph-filename "a.b." "next")将返回()。 - philcolbourn

7

背景

GIMP 2.6.6 Windows Vista SP2

目标

提取原始文件名的基本名称,不包括扩展名。

症状

错误:eval: 未绑定变量:re-match-nth

可能建议

GIMP 菜单 "Filters" > "Script-Fu" > "Console"

在输入框中,粘贴以下 Script-Fu 函数定义,然后按下ENTER键:

(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

为了测试该功能,请输入:

(filename-basename "screen.xcf")

脚本-Fu控制台回答:
"screen"

谢谢!strbreakup正是我所需要的。 - Donald Byrd

6
我的版本将文件名(f)分割成由分隔符(在这种情况下为“.”)分隔的部分;删除最后一部分;然后再次使用分隔符重新组合它们。
(define (pc-drop-extension f) 
  (unbreakupstr (butlast (strbreakup f ".")) ".")  )

so

(pc-drop-extension "ab.cd.efi") -> "ab.cd"

并且

(pc-drop-extension "ab/cd.ef/ghi.jkl.mno") -> "ab/cd.ef/ghi.jkl"

5
感谢philcolbourn指出了一个“简单”的方法来做到这一点。 不幸的是,butlast函数已被弃用: http://www.gimp.org/docs/script-fu-update.html#deprecated 以下是philcolbourn的版本,使用了建议的替代方法:
(define (drop-extension filename)
  (unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)

1
在Gimp 2.8中,必须使用"gimp-image-get-uri"来获取JPG文件的文件名,但是gimp-image-get-uri会提供完整的路径,因此我使用此函数仅提取图片的名称(不包括后缀".jpg"):
(let* (
(uriname (car (gimp-image-get-uri IMAGE)))
(basename (car (reverse (strbreakup (car (strbreakup uriname ".")) "/"))))
...
)
...
)

0

对于那些寻找真正的字符串替换功能的人,这里是我为Script Fu编写的一个函数

(define (string-replace strIn strReplace strReplaceWith)
    (let*
        (
            (curIndex 0)
            (replaceLen (string-length strReplace))
            (replaceWithLen (string-length strReplaceWith))
            (inLen (string-length strIn))
            (result strIn)
        )
        ;loop through the main string searching for the substring
        (while (<= (+ curIndex replaceLen) inLen)
            ;check to see if the substring is a match
            (if (substring-equal? strReplace result curIndex (+ curIndex replaceLen))
                (begin
                    ;create the result string
                    (set! result (string-append (substring result 0 curIndex) strReplaceWith (substring result (+ curIndex replaceLen) inLen)))
                    ;now set the current index to the end of the replacement. it will get incremented below so take 1 away so we don't miss anything
                    (set! curIndex (-(+ curIndex replaceWithLen) 1))
                    ;set new length for inLen so we can accurately grab what we need
                    (set! inLen (string-length result))
                )
            )
            (set! curIndex (+ curIndex 1))
        )
       (string-append result "")
    )
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接