Jonathan Eunice  committed 080669b

cleaned up UTI handling; added paste_format()

  • Participants
  • Parent commits 69f76b7

Comments (0)

Files changed (3)

 RTF seems to be the *lingua franca*. There aren't AFAIK any 
 particularly good Python tools for parsing and transforming RTF.
 The handy `textutil <>`_
-tool will, however, convert an RTF file into quite clean HTML, like so:
+tool will, however, convert an RTF file into quite clean HTML, like so::
-``textutil -convert html temp.rtf``
+    textutil -convert html temp.rtf
 yielding ``temp.html``. This can be parsed and manipulated
 with `lxml <>`_ or your favorite HTML/XML library.


 # UTIs are Uniform Type Identfifiers (
 # ...even if they sound a lot like urinary tract infections
+def UTI(fmt):
+    """
+    Return the UTI for the given simple format name ('rtf', 'html', 'text').
+    If the format name is unknown, returns it directly, assuming it's an
+    explicit UTI.
+    """
+    return format2uti.get(fmt, fmt)
 def paste(formats=None):
     Get data from the Mac pasteboard (aka clipboard). Prefers a list of
     formats = formats or preferred_formats
     pb = NSPasteboard.generalPasteboard()
     for f in formats:
-        uti = format2uti.get(f, f)
-        contents = pb.stringForType_(uti)
+        contents = pb.stringForType_(UTI(f))
         if contents:
             return unicode(contents)
     return None
+def paste_format(fmt):
+    """
+    Return data of the given format ('rtf', 'html', 'text', or an explict UTI) from
+    the Mac pasteboard (aka clipboard), if any, otherwise None.
+    """
+    pb = NSPasteboard.generalPasteboard()
+    return pb.stringForType_(UTI(fmt)) or None
 def available(dyn=False):
-    Returns list of UTIs (data types) of data available on the pasteboard. By
+    Return list of UTIs (data types) of data available on the pasteboard. By
     default, excludes items whose type starts with 'dyn.', which is a dynamic
-    type lookup scheme for 'unregistered' type codes--and beyond our current
+    type lookup scheme for 'unregistered' type codes--beyond our current
     scope of operations.
     pb = NSPasteboard.generalPasteboard()
     provide a direct kwargs dict with actual UTIs.
     pb = NSPasteboard.generalPasteboard()
-    copy_utis = [ format2uti.get(f, f) for f in kwargs.keys() ]
-    pb.declareTypes_owner_(copy_utis, None)
+    pb.declareTypes_owner_([ UTI(f) for f in kwargs.keys() ], None)
     for fmt, value in kwargs.iteritems():
-        uti = format2uti.get(fmt, fmt)
         if isinstance(value, unicode):
             value = value.encode('utf-8')
         new_str = Foundation.NSString.stringWithString_(value).nsstring()
         new_data = new_str.dataUsingEncoding_(Foundation.NSUTF8StringEncoding)
-        pb.setData_forType_(new_data, uti)
+        pb.setData_forType_(new_data, UTI(fmt))
 def clear():
-    version='0.05',
+    version='0.06',
     author='Jonathan Eunice',
     description='copy/paste for Mac OS X for rich text (HTML/RTF) rather than plain text',