Code Monkey home page Code Monkey logo

sketchscriptfail's Introduction

Sketch Script Fail

Version 3.8.3 (29802)

I’ve reviewed .sketch docs and tried many different solutions, but I couldn’t add a MSSliceLayer with the same frame as the selected layer.

So here’s my failed attempt at creating a .sketch script. When I added MSSliceLayer to the current document it’s center point is the current document center point and not the selected layer.

Maybe, I’m missing something simple.

var documentName = context.document.displayName();

var selectedLayers = context.selection;
var selectedCount = selectedLayers.count();

if (selectedCount == 0) {

  log('No layers are selected.');
  
} else {

  log('Selected layers:');
  for (var i = 0; i < selectedCount; i++) {
    var layer = selectedLayers[i];
    var sliceLayer = MSSliceLayer.alloc().init();

    var rect = MSRect.alloc().init();
    rect.x = layer.frame().x();
    rect.y = layer.frame().y();
    rect.width = layer.frame().width();
    rect.height = layer.frame().height();

    sliceLayer.frame = rect;

    context.document.addLayer(sliceLayer);
    
  }

};

###Solution Thanks to BohemianCoding:

var documentName = context.document.displayName();

var selectedLayers = context.selection;
var selectedCount = selectedLayers.count();

if (selectedCount == 0) {

 log('No layers are selected.');
 
} else {

 log('Selected layers:');
 for (var i = 0; i < selectedCount; i++) {
   var layer = selectedLayers[i];

   var sliceLayer = MSSliceLayer.sliceLayerFromLayer(layer);
   context.document.addLayer(sliceLayer);
   
 }

};

As you can see this solution uses MSSliceLayer.sliceLayerFromLayer() which is not apart of the official docs, at least currently, but apart of MSSliceLayer’s unofficial header.

###Extra I've also noticed a few other things not working properly. Updating a MSSliceLayer.frame in order to add a 10px padding,

MSSliceLayer.frame.scaleBy()

will update the frame.size, but not maintain its center point. So since I do have access to NSFoundation I can simply call CGRectInset(), which will returns a rectangle that is smaller or larger than the source rectangle, with the same center point, and update MSSliceLayer.rect.

var sliceLayer = MSSliceLayer.sliceLayerFromLayer(layer);
sliceLayer.rect = CGRectInset(sliceLayer.frameForTransforms(), -10, -10)
  

sketchscriptfail's People

Contributors

rgssoftware avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.