I would like to periodically search for newly added (created) files inside ~/Downloads
and add these files to the list of Recent
so that they are able to be listed and recognised by applications such as Recent Files Indicator.
Asked
Active
Viewed 59 times
1
1 Answers
1
Introduction
Technically speaking, each GUI application that chooses to register the files it opens with Gtk.RecentManager
can do so, and the files will appear in any application that reads the recent files, including the recent files indicator. However, command line applications don't do that. It is possible , however, to track a directory and add newly created files to RecentManager. The script presented below allows doing exactly that.
Usage
The usage is very simple - run the script with python3
and give it list of directories as command line arguments. Standard rules for quoting command-line arguments apply. Example:
python3 indexdir.py ~/Downloads
Source Code:
Also available on GitHub:
#!/usr/bin/env python3
# Author: Serg Kolo
# Date: November 19, 2016
# Written for: http://askubuntu.com/q/842038/295286
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GLib
import urllib.parse
import signal
import time
import sys
import os
class Indexer(object):
def __init__(self):
self.callback()
def callback(self,*args):
self.indexdir(sys.argv[1:])
time.sleep(3)
GLib.idle_add(Gtk.main_quit)
def get_file_uri(self,*args):
file = Gio.File.new_for_path(args[-1])
if not file.query_exists(): return None
return file.get_uri()
def indexdir(self,*args):
mgr = Gtk.RecentManager().get_default()
recent = [i.get_uri() for i in mgr.get_items()]
for dir in args[-1]:
full_path = os.path.realpath(dir)
for file in os.listdir(full_path):
file_path = os.path.join(full_path,file)
file_uri = self.get_file_uri(file_path)
if not file_uri: continue
if file_uri in recent: continue
print('>>> adding: ',file_uri)
mgr.add_item(file_uri)
def run(self,*args):
Gtk.main()
def quit(signum,frame):
Gtk.main_quit()
sys.exit()
def main():
while True:
signal.signal(signal.SIGINT,quit)
indexer = Indexer()
indexer.run()
if __name__ == '__main__': main()

Sergiy Kolodyazhnyy
- 105,154
- 20
- 279
- 497
~/.local/share/recently-used.xbel
(because that is where the list resides) and run that periodically. – Jos Oct 26 '16 at 15:07