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.

In the event this is useful to anyone, I ended up figuring how to do what I originally inquired about to support a demo of the real tool I initially thought I needed this for. I wish I could share more about exactly what I was building but… pesky NDAs (hint: it involves type — and laser projection directly from Glyphs).

Anyway, the way to do this is very simple:

from GlyphsApp import *

Glyphs.clearLog()

# Unicode for glyphs to select
glyphsList = [
  '1F318',
  '1F64F',
  '1F49A',
  '1F600',
  '1F60D',
]

if Font:
  # clear any existing selected layers
  Font.selection = []

  newList = []

  for myGlyph in glyphsList:
    newList.append(Font.glyphs[myGlyph])

  Font.selection = newList

else:
  print("No font open in Glyphs.")

Great you figured it out. Based on your code, here is a shorter version.

glyphsList = [
  '1F318',
  '1F64F',
  '1F49A',
  '1F600',
  '1F60D',
]
Font.selection = [Font.glyphs[name] for name in glyphsList]
  • No need to clear the selection beforehand, if you assign it anyway later with the = operator.
  • You can keep the check for the font of course (if Font: ...)
2 Likes