sketch-hq / SketchAPI

The JavaScript plugin library embedded in Sketch
https://developer.sketch.com/reference/api
MIT License
842 stars 126 forks source link

Expose boolean operations on ShapePath #590

Open KevinGutowski opened 5 years ago

KevinGutowski commented 5 years ago

Right now you have to drop down to the private api to do this

let sketch = require('sketch')

let document = sketch.getSelectedDocument()
let selectedLayer = document.selectedLayers.layers[0]

console.log(selectedLayer.sketchObject.booleanOperation())
matteogratton commented 2 years ago

Up for this one.

More context: in order to create a boolean group, we have to perform more actions:

  1. Create a Shape (which will become the group for the items)
    let shapeGroup = new Shape({
    frame: {
        x: layer.frame.x,
        y: layer.frame.y,
        width: layer.frame.width,
        height: layer.frame.height,
    },
    name: "Name",
    parent: someParent,
    layers: [],
    });
  2. Add each layer to the shapeGroup (based on selection or other lists) and apply the boolean option we need:
    document.selectedLayers.layers.forEach((inner) => {
    inner.parent = shapeGroup;
    inner.sketchObject.setBooleanOperation(0);
    });

    where 0 = Union 1 = Substract 2 = Intersect 3 = Difference -1 = None