[Goodies-dev] Proposed goodies contribution: Recent Folders

Serge Stroobandt serge at stroobandt.com
Fri Jul 6 17:41:46 CEST 2012


Dear XFCE4-friends,

Attached is "Recent Folders", a thoroughly tested Python3 script,
that -in my humble opinion- has potential for becoming a XFCE4 goodies 
panel application.
It is licensed under GPL v3.

It requires packages python3, python3-lxml and zenity to be installed on 
the system in order to run.

If there is interest from this group, I would be more than happy to add 
i18n language support and
would welcome instructions to properly package it.

Brief history: My family and I were missing this functionality in 
"Places" (xfce4-places-plugin).
I noticed that the Places bug 
https://bugzilla.xfce.org/show_bug.cgi?id=3737 is open since 2007.
I had a look at the Places code, but had great difficulty to find my way 
in the C code. Hence, this more accessible Python script.

All comments are invited.

Kind regards,
Serge

-------------- next part --------------
#!/usr/bin/python3

# VERSION
# 0.1.2

# DESCRIPTION
# Opens a list of recently used folders on the desktop.
# The chosen folder is opened with the default file manager.

# USAGE
# $xfce4-recentfolders-plugin

# REQUIRES
# python3, python3-lxml, zenity

# COPYRIGHT
# Copyright (C) 2012  Serge YMR Stroobandt

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# CONTACT
# serge at stroobandt.com

# All imports are standard on most modern Python implementations
import os
from lxml import etree
import urllib.parse
import subprocess
import sys

# XDG Recent File Spec 0.2
recently = os.path.expanduser('~/.recently-used')
try:
    tree = etree.parse(recently)
except:
    # XFCE4 is not yet compliant with XDG Recent File Spec 0.2.
    # Hence, module xdg.RecentFiles was not employed.
    recently = os.path.expanduser('~/.local/share/recently-used.xbel')
    try:
        tree = etree.parse(recently)
    except:
        print()
        print('Recent Folders - Error: recently-used file was not found or is corrupt. Exiting.')
        print()
        sys.exit(1)

chosen = ''
while chosen == '':
    # All bookmarks retrieved by the parser
    bookmarks = tree.findall('bookmark')
    # Put most recent folders first
    bookmarks.reverse()
    hrefs = []
    folders = []
    # Always add user home at top of folders list
    folders.append('~/')
    for bookmark in bookmarks:    
        # Get href attribute and remove file:// and filename
        href = bookmark.attrib['href']
        href = href.replace('file://', '')
        href = href.rpartition('/')[0] + '/'
        # Remove %20 etc.
        folder = urllib.parse.unquote(href)
        if folder == '/tmp/' or folder.find('/.cache') >= 0:
            continue
        # Test if the folder still exists
        if os.path.exists(folder):
            try:
                # Test if the folder is already in the folders list
                folders.index(folder)
            except ValueError:
                # Add path to the folders list
                folders.append(folder)
    # Add clear list option
    folders.append('CLEAR FOLDER LIST')
    
    try:
        # Choose a folder from the list
        chosen = subprocess.check_output('zenity --width=860 --height=610 --title'.split() + ['Recent Folders'] + '--list --text'.split() + ['Open ...'] + '--column Folder'.split() + folders)
        # Convert to a string and slice off the newline
        chosen = str(chosen,  'utf-8')[:-1]
    except:
        # Main windom was cancelled
        sys.exit()
    
    if  chosen == 'CLEAR FOLDER LIST':
        try:
            # Confirm deletion
            subprocess.check_output('zenity --question --title'.split() + ['Recent Folders'] + '--text'.split() + ['Delete the recently-used list?'] + '--ok-label Delete --cancel-label Abort'.split())
            # Delete bookmarks
            for bookmark in bookmarks:
                bookmark.getparent().remove(bookmark)
            tree.write(recently, pretty_print=True, xml_declaration=True,  encoding='UTF-8')
            chosen = ''
        except:
            # Deletion aborted
            chosen = ''

# Open the chosen folder
subprocess.Popen(['/usr/bin/xdg-open', os.path.expanduser(chosen)])


More information about the Goodies-dev mailing list