Find files with an emblem in Gnome

With the desktop on Ubuntu Linux (indeed, with any Gnome desktop) it’s possible to add emblems to any file. An emblem is a little reminder, a small icon that appears on top of the file. Think of it as a tag, perhaps, in this Web 2.0 world. The 'Sam presents' file has a 'money' emblem. Anyway, I needed a way of establishing which files had a particular emblem from a script. So here’s such a script. Call as follows:

[email protected]:~$ python money
/home/aquarius/Sam presents

The actual script is:

import re, glob, sys, urllib, os
from xml.dom import minidom

def grep(pattern, files):
  search = re.compile(pattern).search
  matches = []
  for file in files:
    for index, line in enumerate(open(file)):
      if search(line):
  return matches

def findEmblemedFiles(emblem):
  metafilesWithKeyword = grep('<keyword ', 
  retfiles = []
  for dirxmlfile in metafilesWithKeyword:
    dom = minidom.parse(dirxmlfile)
    filenodes = [x.parentNode.getAttribute('name') 
                 for x in dom.getElementsByTagName('keyword')
                 if x.getAttribute("name") == emblem]
    files = dict([(x,'') for x in filenodes]).keys()
    # turn /home/foo/.nautilus/metafiles/file:%2F%2F%2Fhome%2Ffoo%2FWork.xml
    # into /home/foo/Work/
    dirname = urllib.unquote(os.path.split(dirxmlfile)[1])[7:-4] + '/' 
    retfiles += [os.path.join(dirname,urllib.unquote(f)) for f in files]
  return retfiles

if __name__ == "__main__":
  print 'n'.join(findEmblemedFiles(sys.argv[1]))
I'm currently available for hire, to help you plan, architect, and build new systems, and for technical writing and articles. You can take a look at some projects I've worked on and some of my writing. If you'd like to talk about your upcoming project, do get in touch.

More in the discussion (powered by webmentions)

  • (no mentions, yet.)