GIMP python-fu, How to get the directory of the last saved image

259 Views Asked by At

I'm trying to create some GIMP plug-in and here I have gtk FileDialog:

chooser = gtk.FileChooserDialog(title="Save as...",
                                action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                buttons=(gtk.STOCK_SAVE,
                                         gtk.RESPONSE_OK,
                                         gtk.STOCK_CANCEL,
                                         gtk.RESPONSE_CANCEL))
chooser.set_do_overwrite_confirmation(True)

I want to set current folder of the FileChoserDialog to the last used directory, like that

chooser.set_current_folder(">>>>>HERE I NEED THE DIRECTORY OF THE LAST SAVED IMAGE<<<<<<<<")

How could I do that?

The only thing I came up with is to save filenames to image.filename and then purge it.

chooser.set_current_folder(choose_set_dir())
response = chooser.run()
filename = chooser.get_filename()
chooser.destroy()
if response == gtk.RESPONSE_OK
    <...>
    pdb.file_jpeg_save(image, drawable, filename, filename, 0.9, 0, 0, 0, "", 2, 1, 0, 0)
    clear_filenames()
    image.filename = filename
    image.clean_all()


def choose_set_dir():
    for img in gimp.image_list():
        if img.filename:
            return path.dirname(img.filename)
    
    return path.expanduser("~/Desktop")
    
def clear_filenames():
    for img in gimp.image_list():
        if img.filename:
            img.filename = ""

But it's some sort of hack, and I'd like to get things right way.

2

There are 2 best solutions below

0
On

Maybe not a good idea... The FileChoose doc says:

Note that old versions of the file chooser’s documentation suggested using gtk_file_chooser_set_current_folder() in various situations, with the intention of letting the application suggest a reasonable default folder. This is no longer considered to be a good policy, as now the file chooser is able to make good suggestions on its own. In general, you should only cause the file chooser to show a specific folder when it is appropriate to use gtk_file_chooser_set_filename(), i.e. when you are doing a Save As command and you already have a file saved somewhere.

In addition, the file chooser dialog has a "Recently used" bookmark so users can quickly find any of their recently used directories.

If this isn't enough:

  • You could iterate the image list, and obtain the file names of all the loaded images, check time stamps, and try to deduce the last saved image, but still you would need these image to be currently opened in Gimp.
  • On Linux, you can check the file ~/.local/share/recently-used.xbel which is where (most, including Gimp, but not all) apps share information about recently used files. The last files are the recent ones.

A couple more hints:

  • gimp.user_directory(n) (n in [0..8]) returns the path of some directories: Desktop, Documents, Pictures....
  • adding a named arg run_mode=RUN_INTERACTIVE to the file_jpeg_save makes it elicit the JPEG options dialog, so you don't need to hardcode options, and you can use run_mode=RUN_WITH_LAST_VALS in the following calls to reuse the same settings.
0
On

I found out that there is a module called gimpshelf

Using this module I can save data between runs of the plugin

from gimpfu import *
from gimpshelf import shelf
import gtk
from os import path

def choose_set_dir():
    if shelf.has_key('photo_sign_last_save_dir'):
        return shelf['photo_sign_last_save_dir']
    else:
        return gimp.user_directory(USER_DIRECTORY_DESKTOP)

chooser = gtk.FileChooserDialog(title="Save As...",
                                action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                buttons=(gtk.STOCK_SAVE,
                                         gtk.RESPONSE_OK,
                                         gtk.STOCK_CANCEL,
                                         gtk.RESPONSE_CANCEL))
chooser.set_do_overwrite_confirmation(True)
chooser.set_current_folder(choose_set_dir())
response = chooser.run()
filename = chooser.get_filename()
chooser.destroy()

if response == gtk.RESPONSE_OK:
    <...>
    pdb.file_jpeg_save(image, drawable, filename, filename, 0.9, 0, 0, 0, "", 2, 1, 0, 0)
    shelf['photo_sign_last_save_dir'] = path.dirname(filename)