Como analisar o nome do arquivo base usando o script-fu
Pergunta
Usando o gimp 2.6.6 para o Mac OS X (sob x11), como baixado em gimp.org.
Estou tentando automatizar um processo manual chato com o script-fu. Eu precisava analisar o nome do arquivo de imagem para salvar várias camadas como novos arquivos usando um sufixo no nome do arquivo original.
Minhas tentativas originais foram assim, mas falharam porque (string-search ...)
Não parece estar disponível em 2.6 (uma alteração no mecanismo de script?).
(set! basefilename (substring filename 0 (string-search "." filename)))
Então eu tentei usar Essa informação para analisar o nome do arquivo base usando regex, mas (re-match-nth ...)
também não é reconhecido.
(if (re-match "^(.*)[.]([^.]+)$" filename buffer)
(set! basefilename (re-match-nth orig-name buffer 1))
)
E enquanto retiram o valor do vetor sem erro, o valor resultante não é considerado uma string quando é passado (string-append ...)
.
(if (re-match "^(.*)[.]([^.]+)$" filename buffer)
(set! basefilename (vector-ref buffer 1))
)
Então, acho que minha pergunta é: como eu analisaria o nome do arquivo base?
Solução
Contexto
GIMP 2.6.6 Windows Vista SP2
Meta
Extraia o nome da base do nome do arquivo original sem sua extensão.
Sintoma
Erro: avaliar: variável não ligada: re-match-nth
Sugestão possível
Menu do gimp "Filtros" > "Script-fu" > "Console"
Na caixa de entrada, cole a seguinte definição de função de script-fu e atingir o DIGITAR chave:
(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
Para testar a função, digite:
(filename-basename "screen.xcf")
As respostas do console de script-fu:
"screen"
Outras dicas
Não é realmente uma solução correta:
> (nome do arquivo-basename "this.is.a.long.filename.jpg")
"isto"
Uma melhor implementação:
(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)
)
)
)
Minha versão divide o nome do arquivo (f) em peças delimitadas pelo separador ("." Nesse caso); cair na última parte; e novamente novamente com o separador novamente
(define (pc-drop-extension f)
(unbreakupstr (butlast (strbreakup f ".")) ".") )
assim
(pc-drop-extension "ab.cd.efi") -> "ab.cd"
e
(pc-drop-extension "ab/cd.ef/ghi.jkl.mno") -> "ab/cd.ef/ghi.jkl"
Muito obrigado a Philcolbourn por apontar uma maneira "simples" de fazer isso. Infelizmente, a função Butlast está preterida:http://www.gimp.org/docs/script-fupdate.html#deprecated
Aqui está a versão de Philcolbourn com a substituição sugerida:
(define (drop-extension filename)
(unbreakupstr (reverse (cdr (reverse (strbreakup filename ".")))) ".")
)
Como no gimp 2.8 "gimp-image-get-uri" deve ser usado para obter o nome do arquivo de um arquivo jpg, mas o gimp-image-get-uri entrega o caminho completo, usei essa função para extrair apenas o nome do foto (sem o sufixo ".jpg"):
(deixar* (
(Uriname (Car (Imagem Gimp-Image-Get-Uri))))
(nome de base (carro (reverso (strbreakup (carro (strbreakup uriname ".") "/")))))))))))))))))))))))))))))))))))))))
...
)
...
)
Para aqueles que procuram uma verdadeira funcionalidade de substituto de string, aqui está uma função que escrevi para uso no 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 "")
)
)