GIMP Chat
http://gimpchat.com/

Script-fu skip run-mode parameter (solved)
http://gimpchat.com/viewtopic.php?f=9&t=20193
Page 1 of 1

Author:  preklov [ Mon Nov 14, 2022 8:33 am ]
Post subject:  Script-fu skip run-mode parameter (solved)

Hi there. I'm new to this forum.

I wrote some script-fus and a python-fu, some of which don't need a dialogue and no run-mode parameter either. So i didn't include it in the registration's parameter list. But when I call these functions I am forced to put a RUN-NONINTERACTIVE or RUN-INTERACTIVE at the beginning of the argument list. To keep my code better readable I would be glad to know how to register the scripts without run-mode.

Gimp 2.10.32 on macOS Ventura 13.0.1.

Thanks in advance.
Volker

Author:  Pocholo [ Mon Nov 14, 2022 4:49 pm ]
Post subject:  Re: Script-fu skip run-mode parameter

Can you post the code? It would be easier to see what your referring.

Author:  Rod [ Tue Nov 15, 2022 3:57 am ]
Post subject:  Re: Script-fu skip run-mode parameter

Does the param require an int?
Try 1 or 0

Author:  preklov [ Tue Nov 15, 2022 4:27 am ]
Post subject:  Re: Script-fu skip run-mode parameter

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

Author:  ofnuts [ Tue Nov 15, 2022 5:32 am ]
Post subject:  Re: Script-fu skip run-mode parameter

The question is why you are making separate script-fu scripts to call your python code when you could have everything in one single python file? Everything in script-fu also exists in python-fu.

PS: and IMHO the "readability" is Scheme's main problem, whether Gimp is implied or not.

Author:  preklov [ Tue Nov 15, 2022 7:58 am ]
Post subject:  Re: Script-fu skip run-mode parameter

Many years ago I wrote a few script-fu (Tiny-fu in those days) macros to perform some batch tasks. I improved and extended them 7 years ago. For my needs now I could reuse the most part of them but for the few tasks that are not possible in Scheme (see my code).

I agree with you that Scheme is somehow deprecated, not only because of its readability problem (that you can reduce by a consequent indent structure to keep track of the parantheses). Some day I'll go about transferring the whole stuff to Python, I hope. I like to develop new code, but to refight old battles is not so desirable.

I don't call for a new feature, I only wanted to know if there is a way to keep Gimp from inserting a parameter that is not defined. If I get told that there is no way then I'll content myself with it.

Greetings, Volker

Author:  preklov [ Wed Nov 16, 2022 1:22 pm ]
Post subject:  Re: Script-fu skip run-mode parameter (solved)

I found the solution.

For Python-fu there is a register parameter run_mode_param (listed at the very end) that defaults to True.
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,
  run_mode_param = False,)

Setting it to False did the trick.

Thank you for your attention, Volker

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/