-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathxdg-recent
executable file
·81 lines (71 loc) · 2.64 KB
/
xdg-recent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
# xdg-recent (part of ossobv/vcutil) // wdoekes/2023-2024 // Public Domain
#
# Mark files as "recently used" so they show up first in the File Browser in
# GNOME/Gtk/X. This is useful when you're editing/preparing files using
# the command line and want to upload them using a (graphical) web
# brower.
#
# Usage:
#
# - <create file1.txt>
# - xdg-recent file1.txt
# - <open graphical application, click recent, see file there>
#
# Original from unix.stackexchange.com/questions/509413 by Stephen Kitt.
# Named it xdg-recent to match xdg-open, which is the other commonly used
# tool when switching from CLI to X. Not called xdg-mark-recent because
# (a) there exists a xdg-m(ime) and (b) we may want to amend this to do
# more than just "mark".
#
# Todo:
# - add -n for do-not-mark utime()
# - do other stuff, like list/delete the RecentManager data
#
# See also: xdg-open(1) from xdg-utils.
#
import os
import sys
import gi
gi.require_version('Gtk', '3.0')
if True:
# Indent so PEP tools don't complain.
from gi.repository import Gtk, Gio, GLib
rec_mgr = Gtk.RecentManager.get_default()
# rd = Gtk.RecentData()
# rd.description = None
# rd.app_name = 'xdg-recent' # free form
# rd.app_exec = '/usr/bin/xdg-open %u' # %u = file://url, %f = filename
# rd.is_private = False
for arg in sys.argv[1:]:
try:
# Update mtime so an 'ls -ltr' will quickly show it as well.
# Do this first. It resets 'gio info' recent access times if
# done afterwards.
# Additional benefit is that the existence of arg is checked
# before it is pushed onto the RecentManager stack.
os.utime(arg, None)
except PermissionError:
# Failed to update mtime because of lacking permissions. Not a
# problem for us.
pass
gfile = Gio.File.new_for_path(arg)
# rd.display_name = os.path.basename(arg)
# rd.mime_type = mimetypes.guess_type(arg)[0] or 'application/octet-stream'
# rec_mgr.add_full(gfile.get_uri(), rd)
# add_item() is just as capable as add_full() and it's better at
# populating app_exec.
rec_mgr.add_item(gfile.get_uri())
try:
# Open the file to simulate access: this will update the
# "time::access" as seen with "gio info FILE".
stream = gfile.read() # Open the file
stream.read_bytes(1) # Read at least one byte
stream.close() # Close it right away
except GLib.GError:
# Failed to set the "time::access" property. If we can't because
# we cannot read it, the file will still show up in the Recent list,
# but at the bottom.
pass
GLib.idle_add(Gtk.main_quit)
Gtk.main()