【问题标题】:How to parse out base file name using Script-Fu如何使用 Script-Fu 解析出基本文件名
【发布时间】:2010-11-26 01:21:42
【问题描述】:

使用从 gimp.org 下载的用于 MAC OS X(X11 下)的 Gimp 2.6.6。

我正在尝试使用 Script-Fu 自动化一个无聊的手动过程。我需要解析图像文件名以使用原始文件名的后缀将各个图层保存为新文件。

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

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

然后我尝试使用this information 使用正则表达式解析出基本文件名,但(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))
    ) 

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

【问题讨论】:

    标签: scheme gimp guile script-fu


    【解决方案1】:

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

    > (filename-basename "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)
      )
     )
    )
    

    【讨论】:

    • 如果缺少扩展名 (morph-filename "a.b." "next") 返回 ()。
    【解决方案2】:

    上下文

    GIMP 2.6.6 Windows Vista SP2

    目标

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

    症状

    错误:评估:未绑定的变量: 重新匹配第n次

    可能的建议

    GIMP 菜单“过滤器” > “Script-Fu” > “控制台

    在输入框中,粘贴以下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"
    

    【讨论】:

    • 谢谢! strbreakup 正是我想要的。
    【解决方案3】:

    我的版本将文件名 (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"
    

    【讨论】:

      【解决方案4】:

      非常感谢 philcolbourn 指出了一种“简单”的方法。 不幸的是,不推荐使用 butlast 函数: http://www.gimp.org/docs/script-fu-update.html#deprecated

      这是 philcolbourn 的版本,建议替换:

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-09-30
            • 1970-01-01
            • 2010-11-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多