diff --git a/README.md b/README.md new file mode 100644 index 0000000..cd573e0 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# TXT and JSON to Mac CLR converter + +This script can be used convert TXT and JSON files to an Apple Mac .clr color palette file. + +## Examples + +### TXT + + +* [Html2Clr](https://github.com/ramonpoca/ColorTools) + +``` +#0ff aqua +#8B008B darkmagenta +#0003 Black 20% +``` + +### JSON + +Compatible with: + +* [eip/JSON To CLR.applescript.js](https://gist.github.com/eip/3837dfdaeb0326d44cf60c65f59e74c2) +* [IORoot/macos__json2clr--convert](https://github.com/IORoot/macos__json2clr--convert) + +```json +{ + "aqua": "#0ff", + "darkmagenta": "#8B008B", + "Black 20%": "#0003" +} +``` diff --git a/any2clr.applescript.js b/any2clr.applescript.js new file mode 100644 index 0000000..2ef420a --- /dev/null +++ b/any2clr.applescript.js @@ -0,0 +1,33 @@ +ObjC.import('AppKit'); + +var app = Application.currentApplication() +app.includeStandardAdditions = true + +var input = app.chooseFile({ofType: ["txt", "json"]}) +var name = input.toString().match(/.+\/([^.]+)/)[1] +var output = app.chooseFileName({ defaultName: name + '.clr' }) +var contents = app.read(input) +var nsclrlist = $.NSColorList.alloc.initWithName(name); +var colors = [] + +if (contents[0] == "{") { + colors = JSON.parse(contents) + colors = Object.keys(colors).map(s => ({ name: s, color: colors[s] })) +} else { + colors = contents.split("\n") + colors = colors.map(str => str.match(/(\S+)\s+(.+)/)) + colors = colors.filter(match => match) + colors = colors.map(match => ({ name: match[2], color: match[1] })) +} +colors.forEach(o => { + if (o.color.startsWith("#")) + o.color = o.color.substring(1) + if (o.color.length < 6) + o.color = o.color.split('').reduce((str, hex) => str + hex + hex, '') + o.color += 'ff' + ; [o.r, o.g, o.b, o.a] = o.color.match(/../g).map(hex => parseInt(hex, 16) / 255) +}) + +colors.forEach((o, i) => o.nscolor = $.NSColor.colorWithCalibratedRedGreenBlueAlpha(o.r, o.g, o.b, o.a)) +colors.forEach((o, i) => nsclrlist.insertColorKeyAtIndex(o.nscolor, o.name, i)) +nsclrlist.writeToFile(output.toString());