Center components via script


I was reading the Glyphs documentation looking for a method I could apply to a layer with several layers. I would like to center all components in a layer using a script like the following button does:

Screenshot 2020-12-28 at 10.59.49

Thank you so much,

The button centers the selected elements in the bounding box of the selection. You mean how to center in the width of the layer?

from Foundation import NSMidX
for c in Layer.shapes:
	pos = c.position
	pos.x += Layer.width / 2.0 - NSMidX(c.bounds)
	c.position = pos

True, I forgot to say I would like to center them in the middle of the layer just like your script does. I ended up with the same result but with much more code.

Thank you so much,