diff --git a/recent.py b/recent.py index 84fd508..4a7f247 100755 --- a/recent.py +++ b/recent.py @@ -1,26 +1,42 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 import sys import time import gi -gi.require_version('Gtk', '3.0') -from gi.repository import Gtk, GObject, Gio - -try: - arg = sys.argv[1] - input_file = Gio.File.new_for_path(arg) - if not input_file.query_exists(): - print("No such file or directory: %s" % arg) - exit(1) - info = input_file.query_info('time::access', Gio.FileQueryInfoFlags.NONE, None) - info.set_attribute_uint64("time::access", time.time()) + +gi.require_versions({ + 'Gtk' : '3.0', + 'GLib': '2.0', +}) +from gi.repository import Gtk, Gio, GLib + + +def add_to_recent(file_path): + input_file = Gio.File.new_for_path(file_path) + if not input_file.query_exists(None): + print(f"No such file or directory: {file_path}") + sys.exit(1) + + info = input_file.query_info("time::access", Gio.FileQueryInfoFlags.NONE, None) + info.set_attribute_uint64("time::access", int(time.time())) input_file.set_attributes_from_info(info, Gio.FileQueryInfoFlags.NONE, None) + uri = input_file.get_uri() - rm = Gtk.RecentManager() - rm = rm.get_default() + rm = Gtk.RecentManager.get_default() rm.add_item(uri) - GObject.idle_add(Gtk.main_quit) + + GLib.idle_add(Gtk.main_quit) Gtk.main() -except IndexError: - exit(0) + + +def main(): + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} filename") + sys.exit(0) + + add_to_recent(sys.argv[1]) + + +if __name__ == "__main__": + main()