My script to change suffixes in all open fonts not working

Hi there,

I can’t make this old FL script to work well in Glyphs. Currently it’s only working for the font on top but it doesn’t loop all open fonts as intended, I can’t find why. Can anyone spot the problem?
Thanks in advance.

from robofab.world import AllFonts
from robofab.world import CurrentFont

f = CurrentFont()

l = (('TOSF', 'tosf'), ('oldstyle', 'osf'))
	
for font in AllFonts():
	for (t1, t2) in l:
		x = len(t1) + 1
		for g in f:
			c = g.name
			if c[-x:] == '.' + t1:
				g.name = c.replace('.' + t1, '.' + t2)
				g.update()
	f.update()

What about:

replacements = (
	('TOSF', 'tosf'),
	('oldstyle', 'osf'),
)
	
for thisFont in Glyphs.fonts:
	for thisGlyph in thisFont.glyphs:
		for thisReplacement in replacements:
			searchFor, replaceBy = thisReplacement
			if thisGlyph.name.endswith("."+searchFor):
				thisGlyph.name = thisGlyph.name.replace("."+searchFor,"."+replaceBy)

You are inly ever accessing the CurrentFont.

    for g in f:

should be:

    for g in font:

And you don’t need the f.update(), but instead use disableUpdateInterface(), enableUpdateInterface(). That will speed things up considerably.

To add that to Rainers version:

replacements = (
	('TOSF', 'tosf'),
	('oldstyle', 'osf'),
)
	
for thisFont in Glyphs.fonts:
	thisFont.disableUpdateInterface()
	for thisGlyph in thisFont.glyphs:
		for thisReplacement in replacements:
			searchFor, replaceBy = thisReplacement
			if thisGlyph.name.endswith("."+searchFor):
				thisGlyph.name = thisGlyph.name.replace("."+searchFor,"."+replaceBy)
	thisFont.enableUpdateInterface()

Or in your code:

from robofab.world import AllFonts

l = (('TOSF', 'tosf'), ('oldstyle', 'osf'))
	
for font in AllFonts():
	font._font.disableUpdateInterface()
	for (t1, t2) in l:
		x = len(t1) + 1
		for g in font:
			c = g.name
			if c[-x:] == '.' + t1:
				g.name = c.replace('.' + t1, '.' + t2)
				g.update()
	font._font.enableUpdateInterface()
1 Like

Thanks a lot, guys!