How can I modify multiple selected glyphs at once?

Hi,
Dear GeorgSeifert,

I want to modify multiple selected glyphs at once.
This can only be processed one by one at a time.
Can you help me?:sob:

for myGlyph in Glyphs.font.glyphs:
	layer = Glyphs.font.selectedLayers[0] # current layer
	from Foundation import NSMidY
	bounds = Layer.bounds
	centerY = NSMidY(bounds)
	myAscender = Font.selectedFontMaster.ascender
	myDescender = Font.selectedFontMaster.descender
	mycenterY = (myAscender + myDescender) / 2.0
	shift = mycenterY - centerY
	Layer.applyTransform([
		1, # x scale factor
		0.0, # x skew factor
		0.0, # y skew factor
		1, # y scale factor
		0.0, # x position
		shift # y position
	])

Put this at the top, you do not need to import it again in every loop.

Why always refer to the first selected layer? Why not iterate through all selectedLayers? Like this:

from Foundation import NSMidY
myAscender = Font.selectedFontMaster.ascender
myDescender = Font.selectedFontMaster.descender
mycenterY = (myAscender + myDescender) / 2.0
for layer in Glyphs.font.selectedLayers:
	bounds = Layer.bounds
	centerY = NSMidY(bounds)
	shift = mycenterY - centerY
	layer.applyTransform([
		1, # x scale factor
		0.0, # x skew factor
		0.0, # y skew factor
		1, # y scale factor
		0.0, # x position
		shift # y position
	])

Thank you very much, but still only one by one operation.
This can do it, I don’t know what the principle is between them.

import sys
import os
from GlyphsApp import *
import objc
def main():
	Doc = Glyphs.currentDocument
	Font = Doc.font
	SelectedLayers = Doc.selectedLayers()
	for Layer in SelectedLayers:
		LSB = Layer.LSB
		RSB = Layer.RSB
		Width = Layer.width
		newSB = (LSB + RSB) / 2.0
		Layer.LSB = newSB
		Layer.width = Width
if __name__ == '__main__':
	main()

To get better code formatting, put a line of three backticks (grave accents) above and below the code block:

import sys
import os

Screenshot

You are asking very basic questions about basic principles of programming. Did you read the python tutorials on out website: https://glyphsapp.com/tutorials/articles?q=python

I want to operate on multiple selected glyphs at once

The code you posted earlier will process all selected glyphs.

No, it works. Make sure you are running the right piece of code.

Glyphs.font.selectedLayers gives you a list of currently selected layers. If you iterate over them, you are working on the selected layers. Note that glyphs are not selected, but layers are. This is good because you should only change stuff that is visible for the user.

So, going over the current selection could look something like this:

for thisLayer in Glyphs.font.selectedLayers:
    thisLayer.removeOverlap() # example