javascriptsketchappsketchapp-plugin

Get width of selected layer with Sketch API


Writing my first Sketch plugin and I'm trying to get the width of a selected layer.

export default function(context) {
  const selectedLayers = context.selection;
  const selectedCount = selectedLayers.length;

  if (selectedCount === 0) {
    context.document.showMessage('Please select a circle shape layer');
  } else {
    selectedLayers.forEach(function (layer) {
      log(layer.frame.width);
    })
  }
}

The log shows:

<MOUndefined: 0x6040000048f0>

The documentation states that the frame of the layer is a Rectangle and that a Rectangle has x, y, width and height properties. So I don't understand why I'm getting undefined.

I tried log(layer.frame) and I do get:

<MOMethod: 0x60400263b420 : target=0x7f9dbf5b8ee0<MSShapeGroup: 0x7f9dbf5b8ee0> Oval (691540C6-7B18-4752-9BA6-A3A298754C9A), selector=frame>

So I'm targetting it right.


Solution

  • try with

    layer.frame().width()