1

I am trying to get gimp to use a reasonable default path in a "save as" plugin, and to do that I need to be able to specify the default with the return value of a function (I believe).

Currently, my code is something like:

def do_the_foo(image, __unused_drawable, directory):
    # ... do something

register(
    "python_fu_something",
    "Blah de blah",
    "Blah de blah",
    "Blah de blah",
    "Blah de blah",
    "2013",
    "<Image>/File/Save as blah...",
    "*",
    [
        (PF_DIRNAME, "directory", "Directory to save files to", "/")
    ],
    [],
    do_the_foo
)

Naturally this means that the dialog pops up with "/" as the default directory. That's not ideal. I'd like it to start with the path to the currently loaded image if known and then fall back to "/" if the currently loaded image has no path (not saved, whatever).

But to get there I need to know how to replace "/" with a function. I've tried doing just that (create a function, reference it in the PF_DIRNAME line) but no joy (and no error message, either).

3 Answers3

0

i didn't test it, but maybe...

import os
[...]

(PF_DIRNAME, "directory", "Directory to save files to", os.curdir)

or something like that:

(PF_DIRNAME, "directory", "Directory to save files to", os.path.realpath(os.curdir))        
Alberto
  • 718
  • 4
  • 20
0

Be sure to pass the result of the function by calling() it, and not just pass the function itself (which is what happens when the parenthesis are omitted). E.g., replacing line 14 in your example:

( PF_DIRNAME, "directory", "Directory to save files to", get_directory() )

Will evaluate the function get_directory, which is expected to return a string.

Robert K. Bell
  • 9,350
  • 2
  • 35
  • 50
  • P.S.: GIMP won't tell you if the script fails syntax checking, it just won't load it. Do a quick syntax check with `python -m py_compile python_fu_something.py` – Robert K. Bell Nov 10 '13 at 07:39
0
(PF_DIRNAME, "path", "Save", os.getcwd())
Matthew Morrone
  • 220
  • 2
  • 12
  • 1
    It's better to [add more context/explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) around code (as opposed to just having a code-only answer) as that makes the answer more useful. – EJoshuaS - Stand with Ukraine Dec 16 '16 at 17:13