Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

yorikvanhavre commented Mar 5, 2016

To get the icon:

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

This comment has been minimized.

Copy link
Owner Author

yorikvanhavre commented Mar 5, 2016

To open a file with standard app:

    xdg open somefile.ext
@yorikvanhavre

This comment has been minimized.

Copy link
Owner Author

yorikvanhavre commented Mar 5, 2016

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
You can’t perform that action at this time.