Of course I could substitute the argument with 0 or 1 to shorten the code. But I use to come to work on Gimp macros only in intervals of years, and I fear that next time I try to understand my code I would puzzle about what the heck is that 0 or 1. So I'd prefer a more straightforward solution.
My code for two very simple python-fu macros, one to read a metadata value and the other to create a new directory:
from gimpfu import *
import xml.etree.ElementTree as ET
import os
def get_metadata_value(metadata, metadata_name):
ic_name = metadata_name.lower()
value = ""
root_node = ET.XML(metadata)
for tag in root_node.findall('tag'):
if tag.attrib['name'].lower() == ic_name:
value = tag.text
break
return value
register(
"python-fu-image-get-metadata-value",
"Get the value of an image metadata tag",
"Get the value of an image metadata tag.",
"Volker Lenhardt",
"Volker Lenhardt",
"2022",
"",
None,
[
(PF_STRING, "metadata", "The metadata of an image as an xml string", ""),
(PF_STRING, "metadata-name", "The tag name (case insensitive)", ""),
],
[ (PF_STRING, "value", "Tag value"),],
get_metadata_value,)
def make_dir(path):
if not os.path.exists(path):
os.mkdir(path)
return 1
elif os.path.isdir(path):
return 0
return -1
register(
"python-fu-make-dir",
"Make a new directory",
"Make a new directory, if it does not exist.",
"Volker Lenhardt",
"Volker Lenhardt",
"2022",
"",
None,
[(PF_STRING, "path", "The full path to the new directory", ""),],
[(PF_INT, "state", "The result state of the operation\n-1 = path exists as a normal file\n 0 = path exists as a directory\n 1 = path generated as a new directory"),],
make_dir,)
main()
A clipping from a script-fu shows the use:
...
(let*
(
(title-tag
"xmp.dc.title")
(date-tag
"xmp.exif.datetimeoriginal")
(metadata
(car (gimp-image-get-metadata image)))
(title
(string-trim (car (python-fu-image-get-metadata-value
RUN-NONINTERACTIVE metadata title-tag))))
(date
(car (python-fu-image-get-metadata-value
RUN-NONINTERACTIVE metadata date-tag)))
...