Today I updated to beta version 97 and property absoluteRect is removed.
How to fix it.
And I can not export header files from class-dump.I try to dump header and check other version.Only beta version can not dump.
If you’re talking about -[MSLayer absoluteRect] then the following function would be a decent replacement:
function absoluteRectForLayer(layer) {
// NOTE: the code below assumes that `layer` is anything but MSPage instance
let parent = layer.parentObject()
let relativeRect = layer.frame().rect()
return parent.convertRect_toCoordinateSpace_(relativeRect, /* to absolute/page coordinates */nil)
}
@rodionovd any ideas on how can this be translated to be used through the Javascript API? So far i 've managed to find the parent and the relative rect let parent = layer.sketchObject.parentObject(); let relativeRect = layer.sketchObject.frame().rect(); parent.changeBasis({from: relativeRect, to: null});
and after searching found changeBasis which unfortunately doesn’t work/
If you want to stick to the official Sketch JS API, calculating a given layer’s absolute rect could be done as follows:
// this example takes a currently selected layer as an input
const doc = require('sketch/dom').getSelectedDocument()
const layer = doc.selectedLayers.layers[0]
let relative = layer.frame
let absolute = relative.changeBasis({ from: layer.parent })
Note that we pass selected.parent as a from argument for changeBasis() since a layer’s relative frame is actually in its parent coordinate space.
@rodionovd i used the code u provided, but got ‘Struct CGRect has no member named Symbol.toPrimitive’, i seems convertRect_toCoordinateSpace_ doesn,t work. i am using sketch 98.3
I guess one way to solve this is to go in the opposite direction and convert your given absolute frame to a relative one, then assign this relative frame to a layer:
function setAbsoluteRectForMSLayer(layer, newAbsoluteRect) {
let parent = layer.parentObject()
if (!parent.convertRect_fromCoordinateSpace) {
return
}
let newRelativeRect = parent.convertRect_fromCoordinateSpace(newAbsoluteRect, nil)
layer.frame().setRect(newRelativeRect)
}
let layer = ... // an MSLayer
// print(layer.frame()) => {0, 0, 90, 90}
// print(absoluteRectForLayer(layer)) => {100, 100, 90, 90}
let newAbsoluteRect = NSMakeRect(135, 120, 90, 90)
setAbsoluteRectForMSLayer(layer, newAbsoluteRect)
// print(layer.frame()) => {35, 20, 90, 90}