Yes, both Umlauts and characters transcribed into entities (& and ' happen to be in directory-names I have in my list of recently used files) work as they should.
What you've forgotten about is the case of a directory having been deleted after being put in recently-used.xbel. I've tried my hand at using minidom and fixing that problem and keeping path and date separate by having dictionaries as elements of the list
Code:
import os
from xml.dom import minidom
from datetime import date
import urllib.parse
import subprocess
from tkinter import *
# Do not show paths that were modified more than MaxDays before today.
MaxDays = 100
# Extract the directory path names and modification dates from the recently-used.xbel file
mydom=minidom.parse(os.environ['HOME'] + '/.local/share/recently-used.xbel')
bookmarkNodeList=mydom.getElementsByTagName('bookmark')
PathDate = []
for bookmarkNode in bookmarkNodeList:
filepath=bookmarkNode.getAttribute('href')
moddate=bookmarkNode.getAttribute('modified')
if os.path.exists(urllib.parse.unquote(filepath)[7:]):
PathDate.append({'path':os.path.dirname(filepath),'date':moddate})
def keyfunc(item):
return item['path']+item['date']
# Sort into descending order of path and then modification date.
PathDate.sort(reverse=True,key=keyfunc)
# Remove old or duplicate rows.
Pruned = []
PrevPath = ''
for row in PathDate:
Path = row['path']
Year = int(row['date'][0:4])
Month = int(row['date'][5:7])
Day = int(row['date'][8:10])
Date = date(Year, Month, Day)
DeltaDays = (date.today() - Date).days
if Path != PrevPath and DeltaDays <= MaxDays:
Pruned.append(Path)
PrevPath = Path
# Event handler for a left mouse click on a list box entry.
def on_click_listbox(event):
# Get the selected line index tuple.
index = listbox.curselection()
# Open Thunar for the selected path name.
subprocess.Popen(['thunar', Pruned[index[0]]])
# Create the root window.
root = Tk()
root.title("Recently Accessed Folders")
# Create a Listbox and attach it to the left side of the root window.
listbox = Listbox(root, height = 20, width = 100)
listbox.pack(side = LEFT, fill = BOTH)
# Create a Scrollbar and attach it to the right side of the root window.
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT, fill = BOTH)
scrollbar.config(command = listbox.yview)
# Attach the Listbox to the Scrollbar.
listbox.config(yscrollcommand = scrollbar.set)
# Bind a left mouse click event to the listbox.
listbox.bind('<ButtonRelease-1>', on_click_listbox)
# Insert the path names with special characters into the listbox.
for Path in Pruned:
listbox.insert(END, urllib.parse.unquote(Path))
root.mainloop()
Holger
Bookmarks