Skip to content

Instantly share code, notes, and snippets.

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;
function truncateString(string,length) {
return (string.length <= length) ? string : "…" + string.substr(string.length-length);
}
var layer = context.selection[0],
attributes = layer.attributedString().treeAsDictionary().value.attributes;
attributes.forEach(function(attr) {
log("Text:" + attr.text);
log("Color:" + attr.MSAttributedStringColorAttribute.value);
});
var symbol = context.document.documentData().symbolWithID_('aa-bb-cc-dd');
var textLayers = getAllLayersOfClass("MSTextLayer"),
textLayerLoop = textLayers.objectEnumerator(),
textLayer;
while (textLayer = textLayerLoop.nextObject()) {
log(textLayer.attributedStringValue());
}
function getAllLayersOfClass(className) {
var array = NSArray.array(),
// [0] represents first override, replace with appropriate index if other override is desired
symbolInstance.setValue_forOverridePoint("New text", symbolInstance.overridePoints()[0]);
@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);
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);
}
MSLayerSelector.alloc().init().updateSelectionWithRect_page_extendSelection_options_(NSMakeRect(0,0,1000,1000),context.document.currentPage(),false,0);
var layers = context.selection[0].children(),
layersLoop = layers.objectEnumerator(),
layer,
minX = 0,
maxX = 1000,
minY = 0,
maxY = 1000,
bounds = NSMakeRect(minX,minY,maxX,maxY);
// Deselect the container