I’ve moved some of my code into a new GitHub repo which may be useful for other coders. In particular, it contains helper functions to call a function on the current font, all fonts, all layers of a font, or selected layers.
Once you have installed the module where Glyphs can find it, you can import and use those functions:
from jkGlyphsHelpers.forAll import (
forCurrentFont,
forFonts,
forAllLayersOfAllGlyphs
)
forCurrentFont(call_function: Callable, **kwargs)
- Call a function for the currently active Glyphs file. Any keyword arguments are passed on to the function.
forFonts(call_function: Callable, fonts: List[GSFont] | None = None, **kwargs)
- Call a function for each font of the fonts list, passing the font and any keyword arguments. If
fonts
is None, the function will be called for each open Glyphs file.
forAllLayersOfAllGlyphs(call_function: Callable, font: GSFont | None = None, **kwargs)
- Call a function for each layer of each glyph in the supplied font, passing the layer and any keyword arguments. If
font
is None, the function will be called for the currently active Glyphs file.
from jkGlyphsHelpers.forSelected import (
forAllLayersOfSelectedGlyphs,
forSelectedLayers
)
forAllLayersOfSelectedGlyphs(call_function: Callable, font: GSFont | None = None, **kwargs)
- Call a function for each layer of each selected glyph in the supplied font, passing the layer and any keyword arguments. If font is None, the function will be called for the currently active Glyphs file.
forSelectedLayers(call_function: Callable, font: GSFont | None = None, **kwargs)
- Call a function for each selected layer of the supplied font, passing the layer and any keyword arguments. If font is None, the function will be called for the currently active Glyphs file.
Example script without helper functions:
# MenuTitle: Remove PostScript Hints
# Remove PostScript hints in all glyphs of the current font
from GlyphsApp import Glyphs
Glyphs.font.disableUpdateInterface()
for g in Glyphs.font.glyphs:
for layer in g.layers:
delete = []
for i, hint in enumerate(layer.hints):
if hint.isPostScript:
delete.append(i)
if delete:
for i in reversed(delete):
del layer.hints[i]
Glyphs.font.enableUpdateInterface()
This can be rewritten using one of the helper functions:
# MenuTitle: Remove PostScript Hints
# Remove PostScript hints in all glyphs of the current font
from jkGlyphsHelpers.forAll import forAllLayersOfAllGlyphs
def remove_hints(layer):
delete = []
for i, hint in enumerate(layer.hints):
if hint.isPostScript:
delete.append(i)
if delete:
for i in reversed(delete):
del layer.hints[i]
forAllLayersOfAllGlyphs(remove_hints)