1

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.

orschiro
  • 13,317
  • 17
  • 87
  • 161
  • 1
    Well since the recent only checks opened files what you could do is a script that logs in the files in downloads and opens them. After logged, it will not open old ones but only news ones. Just in case it will open and then close them. That should trigger the recent list. – Luis Alvarado Oct 26 '16 at 14:47
  • 1
    I suppose you could write a script to add your own bookmarks to ~/.local/share/recently-used.xbel (because that is where the list resides) and run that periodically. – Jos Oct 26 '16 at 15:07
  • @LuisAlvarado nice idea! Can I open files in the background without seeing them pop up? – orschiro Oct 27 '16 at 05:54

1 Answers1

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