absoluteRect replacement?

Could someone suggest an update to the following plugin code to replace ayer.absoluteRect, I understand that is now retired.

For context this is the Artboard from Selection function within the Automate Sketch plugin which seems to now be abandoned.


    var ga = require("../modules/Google_Analytics");
    ga("Artboard");

    var doc = context.document;
    var selection = context.selection;

    if (selection.count() > 0) {
        var loopSelection = selection.objectEnumerator();
        var layer;
        while (layer = loopSelection.nextObject()) {
            if (layer.class() != "MSArtboardGroup" && layer.class() != "MSSymbolMaster") {
                var artboard = MSArtboardGroup.alloc().init();
                artboard.setName(layer.name());
                artboard.setFrame(MSRect.alloc().initWithRect(layer.absoluteRect().rect()));
                doc.currentPage().addLayers([artboard]);
                artboard.frame().setConstrainProportions(false);
                layer.moveToLayer_beforeLayer(artboard, nil);

                if (layer.class() == "MSLayerGroup") {
                    var layerGroup = artboard.layers().firstObject();
                    layerGroup.ungroup();
                }
            }
        }
    }

};

I was using this function 10 times a day to create 100+ artboards at a time, I am a little lost without it now that I’ve updated to sketch 100!

Thanks

Welcome to the forum!

One way to fix this code is to use a small helper function to convert a layer’s relative frame to absolute coordinates. There’s also a no-longer available setConstrainProportions API call that we might just get rid of entirely:

+// https://forum.sketch.com/t/property-absoluterect-is-removed-on-app-v97/1176
+function absoluteRectForLayer(layer) {
+    let parent = layer.parentObject();
+    let relativeRect = layer.frame().rect();
+    return parent.convertRect_toCoordinateSpace_(relativeRect, nil);
+}

var doc = context.document;
var selection = context.selection;

if (selection.count() > 0) {
    var loopSelection = selection.objectEnumerator();
    var layer;
    while (layer = loopSelection.nextObject()) {
        if (layer.class() != "MSArtboardGroup" && layer.class() != "MSSymbolMaster") {
            var artboard = MSArtboardGroup.alloc().init();
            artboard.setName(layer.name());
-           artboard.setFrame(MSRect.alloc().initWithRect(layer.absoluteRect().rect()));
+           artboard.setFrame(MSRect.alloc().initWithRect(absoluteRectForLayer(layer))); 
            doc.currentPage().addLayers([artboard]);
-           artboard.frame().setConstrainProportions(false);
            layer.moveToLayer_beforeLayer(artboard, nil);

            if (layer.class() == "MSLayerGroup") {
                var layerGroup = artboard.layers().firstObject();
                layerGroup.ungroup();
            }
        }
    }
}
2 Likes

Hi Rick :wave:,

Thank you for posting this and welcome to the forum!

We appreciate you bringing this issue to our attention. The good news here is that we are actively exploring ways to incorporate a similar feature in future updates. As soon we have update, we are happy to let you know. :wink:

Cheers :pray:

1 Like

Thanks, It would be great to see something like this natively in Sketch.

Thank you so much, you’re my hero!

1 Like