Adding suffix/prefix in instance panel

Hi. Scouring the archives and of the Glyphs documentation has so far yielded no results, so I’m afraid I have to ask here: Is it possible to bulk-rename instances, as in adding e.g. a suffix or prefix to multiple instances?

Scenario: I have a font with a lot of weight instances. I have added a width axis and now want to add, let’s say, three additional width instances per weight instance. Currently, I need to click through every (duplicated) weight instance and add the suffix Condensed (or whatever) at the end. I’m sure you can think of many other examples, but I guess my problem becomes apparent: Especially when planning a family with a very large amount of instances (e.g. Hair 2 to 50), quickly adding/changing suffixes or prefixes would be immensely useful. Basically, using the Cmd+Shift+F functionality from the glyph panel to batch rename instances.

Maybe there is something I’ve missed, in any case, help is very much appreciated. Thanks!

This is best done with a script.

The only existing one that offers a similar functionality is Rainer’s Insert Instances script (which is covered in one of the handbook articles I came across). I have absolutely zero scripting knowledge, I’m afraid, so my hope is that there is an existing script floating around already. I can’t imagine this is a new issue, is it? :grinning:

This is the ideal usecase for getting started in scripting then. The first two or three Scripting Glyphs tutorials should get you at a level where you can adjust this for yourself:

for i in Font.instances:
    if i.active:
        i.name += " suffix"
        i.name = "prefix " + i.name
1 Like

Thank you! Just reading through those. Your articles are extremely well written and very easy to follow, just wanted to point that out. I’ll hopefully have my issue solved then, soon, thanks!

1 Like

Thanks, you can always ask Python questions here in the forum, and you’ll get a lot of people answering. Welcome to the club :slight_smile:

Updated: In the sample, I changed the line with suffix, the space was on the wrong end of the string.

Great, I’ll do that, but you’ll need to forgive my beginner questions :slightly_smiling_face: I noticed the wrong space myself and have a working script now, mostly, except that I need to work around one issue: there doesn’t seem to be a GSInstance object for selected instances, only for active instances. I’ll figure something out. Thank you so much!

Ha. My first script. Not exactly what I described in the first post, but it currently does exactly what I need, assuming the weight/width values are set up correctly. Thank you very much for getting me started!

#generate instance names by weight and width values
for i in Font.instances:
	if i.active:
		if "Medium" in i.width:
			i.name = i.weight
		else:
			if "Regular" in i.weight:
				i.name = i.width
			else:	
				i.name = i.weight + ' ' + i.width