#!/usr/bin/python # This rather broken little bit of hackery will take a TextMate theme and # "normalize" it by scanning the settings array and eliminating duplicates. It # preserves order, but the latter setting of any duplicate (or triplicate, etc) # will be the one that survives in the written file. # # The UUID is changed and the name has "(processed)" appended to it # # It is a crap script. It does exactly what I need. # # bill bumgarner import sys import os from objc import * from Foundation import * def bytesToString(bytes): return "".join(["%02x" % ord(v) for v in bytes]) def uuid(): f = open('/dev/random', 'r') uuid = '%s-%s-%s-%s-%s' % (bytesToString(f.read(4)), bytesToString(f.read(2)), bytesToString(f.read(2)), bytesToString(f.read(2)), bytesToString(f.read(6))) f.close() return uuid def processPath(p): p = os.path.normpath(p) p = os.path.expanduser(p) print "Processing %s..." % p theme = NSDictionary.dictionaryWithContentsOfFile_(p) newTheme = NSMutableDictionary.dictionary() newTheme['name'] = '%s (processed)' % theme['name'] newTheme['uuid'] = uuid() originalSettings = theme['settings'] newSettings = NSMutableArray.array() newTheme['settings'] = newSettings newSettings.addObject_(originalSettings[0]) order = [] settingsByName = {} for aSetting in originalSettings[1:]: aName = aSetting['name'] order.append(aName) settingsByName[aName] = aSetting for aName in order: newSettings.addObject_(settingsByName[aName]) pathRoot, ext = os.path.splitext(p) newPath = '%s-processed.%s' % (pathRoot, ext) newTheme.writeToFile_atomically_(newPath, YES) if len(sys.argv) is 1: path = "~/Library/Application Support/TextMate/Themes/Vibrant Ink.tmTheme" processPath(path) else: # never tested. Didn't need it. for p in sys.argv[1:]: processPath(p)