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

发布于 2024-08-04 10:31:55 字数 887 浏览 4 评论 0原文

使用适用于 MAC OS X(X11 下)的 Gimp 2.6.6(从 gi​​mp.org 下载)。

我正在尝试使用 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))
    ) 

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

两人的回忆 2024-08-11 10:31:55

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

> (文件名-基本名“this.is.a.long.filename.jpg”)

“这个”

更好的实现:

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

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)
  )
 )
)
小姐丶请自重 2024-08-11 10:31:55

上下文

GIMP 2.6.6 Windows Vista SP2

目标

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

症状

错误:eval:未绑定变量:
重新匹配第 n 次

可能的建议

GIMP 菜单“过滤器”> “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")

Script-Fu 控制台回答:

"screen"

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"
煞人兵器 2024-08-11 10:31:55

我的版本将文件名(f)分割成由分隔符(在本例中为“.”)分隔的部分;丢弃最后一部分; 组合起来

(define (pc-drop-extension f) 
  (unbreakupstr (butlast (strbreakup f ".")) ".")  )

再次将它们与分隔符重新

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

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

My version splits the filename (f) into parts delimited by separator ("." in this case); drops last part; and re-combines them with separator again

(define (pc-drop-extension f) 
  (unbreakupstr (butlast (strbreakup f ".")) ".")  )

so

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

and

(pc-drop-extension "ab/cd.ef/ghi.jkl.mno") -> "ab/cd.ef/ghi.jkl"
花伊自在美 2024-08-11 10:31:55

非常感谢 philcolbourn 指出了一种“简单”的方法来做到这一点。
不幸的是,butlast 函数已被弃用:
http://www.gimp.org/docs/script-fu-update .html#deprecated

这是 philcolbourn 的版本以及建议的替换:

(define (drop-extension filename)
  (unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)

Many thanks philcolbourn for pointing out a "simple" way to do this.
Unfortunately, the butlast function is deprecated:
http://www.gimp.org/docs/script-fu-update.html#deprecated

Here is philcolbourn's version with the suggested replacement:

(define (drop-extension filename)
  (unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)
好听的两个字的网名 2024-08-11 10:31:55

正如在 Gimp 2.8 中一样,必须使用“gimp-image-get-uri”来获取 JPG 文件的文件名,但 gimp-image-get-uri 提供完整路径,我使用此函数仅提取 JPG 文件的名称图片(不带后缀“.jpg”):(

让*(
(uriname(汽车(gimp-image-get-uri IMAGE)))
(basename(汽车(反向(strbreakup(汽车(strbreakup uriname“。”))“/”))))
...
)
...

As in Gimp 2.8 "gimp-image-get-uri" has to be used to get the filename of a JPG file, but gimp-image-get-uri delivers the complete path, I used this function to extract just the name of the pic (without the suffix ".jpg"):

(let* (
(uriname (car (gimp-image-get-uri IMAGE)))
(basename (car (reverse (strbreakup (car (strbreakup uriname ".")) "/"))))
...
)
...
)

一抹微笑 2024-08-11 10:31:55

对于那些寻找真正的字符串替换功能的人来说,这是我编写的用于 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 "")
    )
)

For those looking for a true string-replace functionality, here is a function I wrote for use in 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 "")
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文