Skip to content

Instantly share code, notes, and snippets.

function hexToColor(hex,alpha) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex),
r = parseInt(result[1],16) / 255,
g = parseInt(result[2],16) / 255,
b = parseInt(result[3],16) / 255,
a = (typeof alpha !== 'undefined') ? alpha : 1;
return NSColor.colorWithCalibratedRed_green_blue_alpha(r,g,b,a);
}
@sonburn
sonburn / getSetValueOnLayer.js
Last active January 8, 2021 03:16
Get or set a value on a layer in Sketch
var value = nil,
key = "kModelPropertiesKey",
layer = context.selection[0],
plugin = "com.animaapp.stc-sketch-plugin";
// Get the value
context.command.valueForKey_onLayer_forPluginIdentifier(key,layer,plugin);
// Set the value
context.command.setValue_forKey_onLayer_forPluginIdentifier(value,key,layer,plugin);
// [0] represents first override, replace with appropriate index if other override is desired
symbolInstance.setValue_forOverridePoint("New text", symbolInstance.overridePoints()[0]);
var textLayers = getAllLayersOfClass("MSTextLayer"),
textLayerLoop = textLayers.objectEnumerator(),
textLayer;
while (textLayer = textLayerLoop.nextObject()) {
log(textLayer.attributedStringValue());
}
function getAllLayersOfClass(className) {
var array = NSArray.array(),
var symbol = context.document.documentData().symbolWithID_('aa-bb-cc-dd');
var layer = context.selection[0],
attributes = layer.attributedString().treeAsDictionary().value.attributes;
attributes.forEach(function(attr) {
log("Text:" + attr.text);
log("Color:" + attr.MSAttributedStringColorAttribute.value);
});
function truncateString(string,length) {
return (string.length <= length) ? string : "…" + string.substr(string.length-length);
}
function createViewWithImage(context,file,frame) {
var imageView = NSView.alloc().initWithFrame(frame),
imagePath = context.plugin.urlForResourceNamed(file).path(),
image = NSImage.alloc().initByReferencingFile(imagePath);
imageView.setWantsLayer(1);
imageView.layer().setBackgroundColor(CGColorCreateGenericRGB(255,255,255,1));
imageView.layer().contents = image;
return imageView;
for (var obj in arr) {
var i = Object.keys(arr).indexOf(obj);
}
var libraries = AppController.sharedInstance().librariesController().libraries(),
libraryLoop = libraries.objectEnumerator(),
library;
while (library = libraryLoop.nextObject()) {
// Do something
}