Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Last active December 11, 2015 02:58
Show Gist options
  • Save davidchambers/4534417 to your computer and use it in GitHub Desktop.
Save davidchambers/4534417 to your computer and use it in GitHub Desktop.
Print known Uniform Type Identifiers as JSON
#!/usr/bin/env python
import json
import re
import subprocess
import sys
if len(sys.argv) > 1:
lsregister = sys.argv[1]
else:
lsregister = ('/System/Library/Frameworks/CoreServices.framework'
'/Versions/A/Frameworks/LaunchServices.framework'
'/Versions/A/Support/lsregister')
p = subprocess.Popen([lsregister, '-dump'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
chunks = ['']
for line in out.splitlines():
if line == '-' * 80:
chunks.append('')
else:
chunks[-1] += line + '\n'
kvsplit = re.compile(r'^\s*(?P<key>.*?):\s*(?P<value>.*)$').search
def parse(chunk):
entry = {}
for line in chunk.splitlines():
match = kvsplit(line)
if match is not None:
entry[match.group('key')] = match.group('value')
return entry
entries = {}
csvsplit = re.compile(r'\s*,\s*').split
for entry in map(parse, chunks):
if 'uti' in entry:
content_types, extensions = [], []
for tag in csvsplit(entry['tags']):
if tag.startswith('.'):
extensions.append(tag)
elif '/' in tag:
content_types.append(tag)
entries[entry['uti']] = {
'conforms_to': csvsplit(entry['conforms to']),
'content_types': content_types,
'description': entry['description'],
'extensions': extensions,
}
print json.dumps(entries)
@davidchambers
Copy link
Author

$ ./cuti.py | python -mjson.tool | grep org.bittorrent.torrent --after-context=13
    "org.bittorrent.torrent": {
        "conforms_to": [
            "public.data", 
            "public.item", 
            "com.bittorrent.torrent"
        ], 
        "content_types": [
            "application/x-bittorrent"
        ], 
        "description": "BitTorrent Document", 
        "extensions": [
            ".torrent"
        ]
    },

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