كيفية تحليل اسم الملف الأساسي باستخدام Script-FU
سؤال
باستخدام gimp 2.6.6 لنظام التشغيل Mac OS X (تحت X11) كما تم تنزيله من gimp.org.
أحاول أتمتة عملية يدوية مملة مع 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
هدف
استخراج الاسم الأساسي لاسم الملف الأصلي دون امتداده.
علامة مرض
خطأ: eval: متغير غير محدود: إعادة المباراة
اقتراح ممكن
قائمة gimp "المرشحات" > "السيناريو-فو" > "وحدة التحكم"
في مربع الإدخال ، قم بصق تعريف البرنامج النصي التالي للوظيفة ثم اضغط على أدخل مفتاح:
(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")
إجابات وحدة التحكم السينمائية فو:
"screen"
نصائح أخرى
ليس حقًا حلًا صحيحًا:
> (اسم الملف 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 على الإشارة إلى طريقة "بسيطة" للقيام بذلك. لسوء الحظ ، يتم إهمال وظيفة Butlast:http://www.gimp.org/docs/script-fu-update.html#deprecated
إليكم نسخة Philcolbourn مع الاستبدال المقترح:
(define (drop-extension filename)
(unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)
كما هو الحال في GIMP 2.8 "gimp-image-get-uri" يجب استخدام الموافقة المسبقة عن علم (بدون اللاحقة ".jpg"):
(يترك* (
(oriname (سيارة (صورة gimp-image-get-uri))))))
(الاسم الأساسي (السيارة (عكس (strbreakup (سيارة (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 "")
)
)