質問

gimp.orgからダウンロードされたMac OS X(X11未満)にGIMP 2.6.6を使用します。

退屈な手動プロセスをScript-FUで自動化しようとしています。元のファイル名の接尾辞を使用して、さまざまなレイヤーを新しいファイルとして保存するために、画像ファイル名を解析する必要がありました。

私の元の試みはこのようになりましたが、失敗しました (string-search ...) 2.6では利用できないようです(スクリプトエンジンの変更?)。

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

それから私は使用しようとしました この情報 Regexを使用してベースファイル名を解析するが (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))
    ) 

それで、私の質問は、どのようにしてベースファイル名を解析するのでしょうか?

役に立ちましたか?

解決

環境

GIMP 2.6.6 Windows Vista SP2

ゴール

拡張機能なしで、元のファイル名のベースネームを抽出します。

症状

エラー:評価:Unbound変数: 再試合nth

考えられる提案

GIMPメニュー」フィルター" > "スクリプトfu" > "コンソール"

入力ボックスで、次のスクリプト-FU機能の定義を貼り付け、次にヒットします 入力 鍵:

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

他のヒント

本当に正しい解決策ではありません:

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

私のバージョンは、ファイル名(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"

これを行うための「シンプルな」方法を指摘してくれたPhilcolbournに感謝します。残念ながら、バトラスト関数は非推奨です。http://www.gimp.org/docs/script-fu-update.html#deprecated

これは、Philcolbournのバージョンが提案されている代替品を備えています。

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

GIMP 2.8のように、jpgファイルのファイル名を取得するためにgimp-image-get-uri "を使用する必要がありますが、Gimp-image-get-uriは完全なパスを提供します。この関数を使用して、 pic(接尾辞なし ".jpg"):

(させて* (
(uriname(car(gimp-image-get-uriイメージ)))
(Basename(car(reverse(strbreakup)(car(strbreakup uriname "。") "/")))))
...
)
...
)

真の文字列複製機能を探している人のために、スクリプト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 "")
    )
)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top