8

I have certain scripts that create files (often from keyboard shortcuts) and I would like to have the files they create appear in the "Recently Used" section of file open dialogs. Is there a way to add to this list from the command-line?

For example, I have a script that runs scrot with certain options to take a screenshot. I would like the resulting screenshot to then appear in the "Recently Used" section of file open dialogs.

Laurence Gonsalves
  • 829
  • 1
  • 9
  • 22
  • I think you need to specify the programs whose file open dialogs you are interested in. Different programs track different recently used files. – edwinksl Oct 01 '16 at 01:57
  • @edwinksl Are you sure about that? If I download an image in Chrome, the downloaded image appears in the Recently Used list of GIMP. It seems unlikely that Chrome would have hard-coded support for GIMP. – Laurence Gonsalves Oct 01 '16 at 02:25
  • Might be possible. I know for a fact there exists amd xml file somewhere in user home folder that contains recently used files, but I don't recall of the top of my head where it is. Look through some of the questions here on AskUbuntu. I know Jacob Vlijm has used it in his scripts. Once i have time, I'll find it and post and answer unless someone does before me – Sergiy Kolodyazhnyy Oct 01 '16 at 02:28
  • @LaurenceGonsalves Take Nautilus as an example. If I merely create a new file somewhere, say, in my home directory, by using `touch`, the Recent tab in Nautilus is not aware of this new file created. If I click on Open File in my Atom editor, this new file also doesn't show up in the Recently Used tab. – edwinksl Oct 01 '16 at 06:47
  • @LaurenceGonsalves I should rephrase what I said in my first comment. I think what you see in the file open dialogs depends on how the new files are created. – edwinksl Oct 01 '16 at 07:04
  • @LaurenceGonsalves As a followup to what Serg said, I think the file in question is `~/.local/share/recently-used.xbel`. – edwinksl Oct 01 '16 at 08:28
  • @edwinksl Yes, I know that not all newly created files are added to this list -- that's why I'm asking how to add certain files to it. `recently-used.xbel` is a good pointer. I searched for that and found that the relevant API is the `GtkRecentManager`. I don't see a command-line wrapper for it, but will see if I can write one in Python. I'll post that here if I am successful (and no one else beats me to it). – Laurence Gonsalves Oct 02 '16 at 03:21

1 Answers1

3

From Python, this can be done with:

from gi.repository import Gio
from gi.repository import GLib
from gi.repository import Gtk

...

GLib.set_application_name(appname) # optional: appname is recorded
uri = Gio.File.new_for_path(filename).get_uri()
Gtk.RecentManager.get_default().add_item(uri)

A more complete script to do this can be found in the recently_used github repo. Usage is pretty straightforward:

$ recently_used.py foo.txt bar.png

This adds the files foo.txt and bar.png to the recently used list used by GTK+ apps.

Laurence Gonsalves
  • 829
  • 1
  • 9
  • 22
  • Good job on solving your own question ! So `Gtk.RecentManager` writes to `~/.local/share/recently-used.xbel` by default ? – Sergiy Kolodyazhnyy Oct 14 '16 at 01:08
  • @Serg I was less concerned about the intermediate file, and more about the end result (ie: does the file I want end up in the recently used section of Nautilus and file dialogs?), but I just checked, and it does look like `recently-used.xbel` gets updated by `Gtk.RecentManager`. Actually, looking in there was interesting because I discovered that the app name is also recorded, so I added a flag to the script to change the app name. – Laurence Gonsalves Oct 14 '16 at 01:28
  • Yeah, it's actually quite useful feature. I recently had to help a user who for some reason had a directory added to the file (which what I think isn't supposed to happen), and we figured out the app was geany for whatever reason, which kind of suggests there's a bug in geany. – Sergiy Kolodyazhnyy Oct 14 '16 at 01:32