6

GTK applications mark files as recently used by adding them to the XML in ~/.local/share/recently-used.xbel, but I am frequently working with files from terminal-driven applications like latex, and these are not marked in the GTK list and hence not available from the "Recent" bookmark in GUI file browsers/pickers etc..

Is there a CLI command I can use to explicitly add files to the Recent list, for smoothing operations between the terminal and GUI sides of my Linux usage? Either an official way, or a fast & simple hack with the side-effect of writing to the recently-used.xbel file!

andybuckley
  • 163
  • 4

1 Answers1

8

The following Python script will add all the files given as arguments to the recently-used list, using GIO:

#!/usr/bin/python3

import gi, sys
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GLib

rec_mgr = Gtk.RecentManager.get_default()

for arg in sys.argv[1:]:
    rec_mgr.add_item(Gio.File.new_for_path(arg).get_uri())

GLib.idle_add(Gtk.main_quit)
Gtk.main()

The last two lines are necessary to start the Gtk event loop; if you don’t do that, the changed signal from the manager won’t be handled, and the files won’t be added to the recently-used list.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
  • Good one! would you make it read from stdin? so that one can feed it through a pipe. Would be good for eg piping a `find . -type f -amin -10` into that script.. – LL3 Mar 29 '19 at 15:00
  • 1
    You can use `xargs` or `find`’s `-exec` — `find . -type f -amin -10 -exec add-recent {} +`. – Stephen Kitt Mar 29 '19 at 15:27
  • Very true. I just thought it would be much slower though, what with re-executing the whole python executable’s bootstrap for each file – LL3 Mar 29 '19 at 15:40
  • 1
    `-exec {} +` calls the script with as many files as possible in one go, not once per file, so the overhead will be very small. – Stephen Kitt Mar 29 '19 at 15:46
  • That's great, thanks. I'd started going down this route myself via https://stackoverflow.com/questions/39836725/how-does-one-add-an-item-to-gtks-recently-used-file-list-from-python -- so I guess there's no "more official" route. I also found it useful to add an option to call `os.utime(arg, None)` to make the marked file appear at the top of the time-ordered Recent list for maximum convenience (at the cost of losing an "accurate" modification timestamp). – andybuckley Apr 01 '19 at 09:35