Select glyphs in font view with Python?

Is it possible to script selecting a range of glyphs based on a provided list in Glyphs? I’m working on some automations and I’ve got a defined list of glyphs that I’d like to be able to perform an action on but haven’t seen if there’s a way to use python to select them.

I already have something working based on manually selecting glyphs and checking with Glyphs.font.selection but am not sure if there’s an API to have Python do the select action for me.

There is a way, but first I would ask: do you really need the selection in the UI for your actual task that you want to do?

I ask, because you can also just perform your action on a list of glyphs without the need to select them.

If you can tell what you need to do, we can help you to get to that in the optimal way.

What Mark is referring to is whether you really care about visually selecting the glyphs in order to perform your actions. You most probably don’t.

In order to perform an action on a glyph, you can “select” it in Python like so:

glyph = Font.glyphs["a"]
for layer in glyph.layers:
    print(layer.name)
glyph.color = 2
# etc.

Read about the Glyphs Python API here: docu.glyphsapp.com

Thanks Mark and Sebastian. For some reason I’ve had it stuck in my head that the script needs to actually have glyphs selected to then do something but of course you’re right and there’s an easier way. Appreciate the nudge to unstick my brain!

You’re not wrong – a script can also use the glyphs currently selected in font view (or edit view). Have a look at the Font.selectedLayers property. This is useful when you want to run a script on selected glyphs again and again, but for differing selections.