Skip to content

Instantly share code, notes, and snippets.

@LukeFinch
Created May 2, 2020 16:38
Show Gist options
  • Save LukeFinch/a168679ef43dc8bf64b0fa60ab5fff24 to your computer and use it in GitHub Desktop.
Save LukeFinch/a168679ef43dc8bf64b0fa60ab5fff24 to your computer and use it in GitHub Desktop.
A Sketch script for NewsKit libraries that updates the colours of text styles by inheriting them from ink styles in the system
var sketch = require("sketch");
var SharedStyle = require('sketch/dom').SharedStyle
var Style = require('sketch/dom').Style
var document =sketch.getSelectedDocument()
function getSharedStyles(type) {
var myStyles = []
if (sketch.version.sketch < 52) {
var styles = (type == 0) ? MSDocument.currentDocument().documentData().layerStyles().objects() : MSDocument.currentDocument().documentData().layerTextStyles().objects();
} else {
var styles = (type == 0) ? MSDocument.currentDocument().documentData().allLayerStyles() : MSDocument.currentDocument().documentData().allTextStyles();
}
var sortByName = NSSortDescriptor.sortDescriptorWithKey_ascending("name",1);
styles = styles.sortedArrayUsingDescriptors([sortByName]);
styles.forEach(style => {myStyles.push(style)})
return myStyles;
}
var inks = getSharedStyles(0).filter(style => { return style.name().startsWith('01')}).map(ink => {
rObj = {}
rObj.name = ink.name().split('/')[1]
rObj.color = ink.style().fills()[0].color()
return rObj
})
var textStyles = getSharedStyles(1)
// selectedStyle = document.getSharedTextStyleWithID(textStyles[0].objectID());
// s = selectedStyle.getAllInstancesLayers()
textStyles.forEach(style => {
selectedStyle = document.getSharedTextStyleWithID(style.objectID())
il = style.name().split('/').length
token = style.name().split('/')[il - 1]
index = findWithAttr(inks, 'name', token)
if(index > -1){
inkColor = inks[index].color
inkHex = inkColor.immutableModelObject().hexValue()
inkAlpha = Math.round(inkColor.alpha()*255).toString(16)
selectedStyle.style.textColor = '#'+inkHex+inkAlpha
style.resetReferencingInstances()
}
})
function findWithAttr(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment