Created
July 19, 2015 06:32
-
-
Save chrisbrandow/099fd016667f1b641332 to your computer and use it in GitHub Desktop.
Command Line tool to turn .clr file into Theme plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// CreatePListFromColorLists | |
// | |
// Created by Christopher Private on 7/13/15. | |
// Copyright (c) 2015 Flouu. All rights reserved. | |
// | |
@import AppKit; | |
const NSString *hexStringForColor(NSColor *color); | |
NSDictionary *updatedDictionary(NSDictionary *original, NSDictionary *newDict); | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSString *path = [NSString stringWithUTF8String:argv[1]]; | |
NSLog(@"path %@", path); | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSColorList *colorList; | |
if (![fileManager fileExistsAtPath: path]) { | |
NSLog(@"no file at %@", path); | |
} | |
colorList = [[NSColorList alloc] initWithName:@"colorList" fromFile:path]; | |
NSString *displayName = [fileManager displayNameAtPath:path]; | |
NSString *extension = [displayName componentsSeparatedByString:@"."].lastObject; | |
if (![extension isEqualToString:@"clr"]) { | |
NSLog(@"Must be .clr file"); | |
return 0; | |
} | |
NSArray *allKeys = colorList.allKeys; | |
NSMutableDictionary *colorDictionary = [NSMutableDictionary new]; | |
for (NSString *key in allKeys) { | |
NSArray *splitName = [key componentsSeparatedByString:@"-"]; | |
if (splitName.count == 1) { | |
colorDictionary[key] = hexStringForColor([colorList colorWithKey:key]); | |
} else if (splitName.count == 2) { | |
NSMutableDictionary *themeDict = colorDictionary[splitName.firstObject]; | |
if (!themeDict) { | |
themeDict = [NSMutableDictionary new]; | |
colorDictionary[splitName.firstObject] = themeDict; | |
} | |
themeDict[splitName.lastObject] = hexStringForColor([colorList colorWithKey:key]); | |
} | |
} | |
NSString *fileName = [displayName componentsSeparatedByString:@"."].firstObject; | |
NSString *plistPath = [fileManager.currentDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", fileName]]; | |
if ([fileManager fileExistsAtPath: plistPath]) { | |
NSDictionary *plistDictionary = [[NSDictionary alloc] initWithContentsOfFile: plistPath]; | |
plistDictionary = updatedDictionary(plistDictionary, colorDictionary); | |
NSLog(@"updated success %zd",[plistDictionary writeToFile:plistPath atomically:YES]); | |
return 0; | |
} | |
NSLog(@"new plist success %zd",[colorDictionary writeToFile:plistPath atomically:YES]); | |
} | |
return 0; | |
} | |
NSDictionary *updatedDictionary(NSMutableDictionary *original, NSDictionary *newDict) { | |
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:original]; | |
for (NSString *key in newDict) { | |
if ([newDict[key] isKindOfClass:[NSString class]]) { | |
//don't need to test, b/c if same, assignment's fine, if nil, will create. | |
d[key] = newDict[key]; | |
} else if ([newDict[key] isKindOfClass:[NSDictionary class]]) { | |
d[key] = updatedDictionary(d[key], newDict[key]); | |
} | |
} | |
return [NSDictionary dictionaryWithDictionary:d]; | |
} | |
const NSString *hexStringForColor(NSColor *color) { | |
const CGFloat *c = CGColorGetComponents(color.CGColor); | |
return [NSString stringWithFormat:@"%02X%02X%02X", (int)((*c++) * 255), (int)((*c++) * 255), (int)((*c) * 255)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment