Skip to content

Instantly share code, notes, and snippets.

@yorikvanhavre
Last active August 2, 2018 12:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorikvanhavre/5bfe3cece687405310f5 to your computer and use it in GitHub Desktop.
Save yorikvanhavre/5bfe3cece687405310f5 to your computer and use it in GitHub Desktop.
get list of recent documents in linux
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re,os,gtk,gio,magic
def geticon(filename):
m = magic.open(magic.MAGIC_MIME)
m.load()
mime = m.file(filename).split(";")[0]
mime = gio.content_type_get_icon(mime).get_names()
theme = gtk.icon_theme_get_default()
for mtype in mime:
icon = theme.lookup_icon(mtype,16,gtk.ICON_LOOKUP_NO_SVG)
if icon:
return icon.get_filename()
return ""
f1 = open("/home/yorik/.local/share/recently-used.xbel")
s = f1.read()
f1.close()
fileslist = re.findall("href=\"file\:\/\/(.*?)\"",s)[-10:]
# this generates a fluxbox entry
f2 = open("/home/yorik/.fluxbox/recent","wb")
f2.write("[submenu] (Recent) {} </usr/share/icons/Clarity/16x16/places/folder-bookmarks.png>\n")
for filename in fileslist:
if os.path.exists(filename):
f2.write(' [exec] ('+ os.path.basename(filename) +') {xdg-open "'+filename+'"} <'+geticon(filename)+'>\n')
f2.write('[end]\n')
f2.close()
@yorikvanhavre
Copy link
Author

To get the icon:

    import gio
    i = gio.content_type_get_icon("image/png")
    i.get_names()

@yorikvanhavre
Copy link
Author

To open a file with standard app:

    xdg open somefile.ext

@yorikvanhavre
Copy link
Author

To get the file path from an icon:

    import gtk
    icon_theme = gtk.icon_theme_get_default()
    icon_info = icon_theme.lookup_icon("my-icon-name", 16, gtk.ICON_LOOKUP_NO_SVG)
    print icon_info.get_filename()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment