# Keeping references to glyph names up to date

**URL:** <https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535>\
**Category:** Scripting\
**Created:** [July 5, 2021, 12:12pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535 "2021-07-05T12:12:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 12:12pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/1 "2021-07-05T12:12:59Z")

</div>

I’m storing a bunch of data in `GSGlyph.userData` that reference other glyph names, and I want to keep this list up to date when the glyph names are changed. There does not seem to be a callback for when glyph is renamed or when the font is changed in general.

I thought of reading the `userData` in a `DOCUMENTOPENED` callback, saving it in a parallel `tempData` structure that uses `GSGlyph` objects, and updating the `userData` in a `DOCUMENTWASSAVED` callback, but the callback will be called after the file is saved so the `userData` will not be saved and triggering another save in the callback will result in an infinite loop.

Any better ideas?

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 1:11pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/2 "2021-07-05T13:11:37Z")

</div>

Alternatively, is there a way to influence how an object is serialized to `.glyphs` file? I can imagine making a class, say `GlyphName`, that keeps reference to the `GSGlyph` but upon serializing it serializes only the name.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 5, 2021, 2:26pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/3 "2021-07-05T14:26:32Z")

</div>

There is a class that is handling the glyph name updates: GSComponent. But most it is doing is storing the name and the glyphID (a UUID, not to be confused with the index).

You can put an NSObject that implements the method:  
`- (BOOL)propertyListToData:(CFMutableDataRef)data format:(int)formatVersion error:(NSError **)error;`  
or  
`- (id)propertyListValueFormat:(int)format;`  
into the userdata. So that object can write itself to the file. The structure has to be valid plist. The first method expects you to add the data to the data object, the second expects a plist object on return.

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 3:18pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/4 "2021-07-05T15:18:22Z")

</div>

> [@GeorgSeifert](#):
>
> There is a class that is handling the glyph name updates: GSComponent.

I tried using `GSComponent`, but the user data still saves the component with old glyph name.

> [@GeorgSeifert](#):
>
> You can put an NSObject that implements the method:  
> `- (BOOL)propertyListToData:(CFMutableDataRef)data format:(int)formatVersion error:(NSError **)error;`  
> or  
> `- (id)propertyListValueFormat:(int)format;`

Thanks, I did something like this and it seems to work, but sometimes Glyphs would crash on me, so I think I’m doing something wrong:

```auto
class GlyphName:
    def __init__ (self, glyph):
        self.glyph = glyph
    def propertyListValueFormat_(self, format):
        return self.glyph.name 

```

---

<div class="post-metadata">

**Author:** ![FlorianPircher](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/florianpircher/32/30457_2.png) [@FlorianPircher](https://forum.glyphsapp.com/u/FlorianPircher)\
**Post date:** [July 5, 2021, 3:22pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/5 "2021-07-05T15:22:32Z")

</div>

Your class needs to be a subclass of `NSObject`. Change its definition like so:

```python
class GlyphName(NSObject):

```

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 3:30pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/6 "2021-07-05T15:30:58Z")

</div>

Thanks, I did that first but it I couldn’t do `Class()` style construction, and had to use `Class.new()` instead, but I guess I have to live with that.

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 3:41pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/7 "2021-07-05T15:41:37Z")

</div>

This seems to do it:

```auto
class GlyphName(NSObject):
    @staticmethod
    def __new__ (cls, *args, **kwargs):
        return cls.new()

    @objc.python_method
    def __init__ (self, glyph):
        self.glyph = glyph

    def propertyListValueFormat_(self, formatVersion):
        return self.glyph.name

```

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 5, 2021, 3:46pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/8 "2021-07-05T15:46:12Z")

</div>

Now if I can get Glyphs to load the names to this class directly, or at least find a way not to mark the font modified when I do it manually, but it is pretty good already.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 5, 2021, 5:34pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/9 "2021-07-05T17:34:53Z")

</div>

You need to check your data when the file is loaded and put your custom object in place. Didn’t we just had the problem that setting the userdata would not dirty the document? Do it the old way here.

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 6, 2021, 8:17pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/10 "2021-07-06T20:17:27Z")

</div>

Good point, I did that and no longer mark the font modified (this also helps reduce the crashes I’m experiencing).

But now I have a more serious issue, when I open the font and try to save it immediately (up to a few seconds), Glyphs will immediately crash. If I wait a little bit longer it will save normally. When the font was marked modified at open, it would crash as soon as the autosave is triggered. I’m not sure how to debug this and whether I’m doing something that I shouldn’t be doing.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 6, 2021, 8:29pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/11 "2021-07-06T20:29:35Z")

</div>

Can you send me your script and a sample file?

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 6, 2021, 9:09pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/12 "2021-07-06T21:09:29Z")

</div>

Sent to support email.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 8, 2021, 12:35pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/13 "2021-07-08T12:35:49Z")

</div>

I had a look and I don’t understand what is going on. But I discovered that if I use this:

```auto
    def propertyListValueFormat_(self, formatVersion):
        NSLog("A")
        return self.glyph.name

```

It isn’t crashing. It is spamming the console but this is better than crashing (for now).

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 8, 2021, 2:17pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/14 "2021-07-08T14:17:53Z")

</div>

This made no difference for.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 8, 2021, 2:29pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/15 "2021-07-08T14:29:56Z")

</div>

What python version do you have?

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 8, 2021, 4:33pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/16 "2021-07-08T16:33:33Z")

</div>

3.9.5 (Homebrew)

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 8, 2021, 5:19pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/17 "2021-07-08T17:19:50Z")

</div>

FWIW, I think something goes wrong if the file gets saved before my code processes all the glyphs e.g. if I added a log message to the ` __init__ ` method and try try to save while it is still printing output the application will crash, but if I wait until the messages stop it will not crash.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 8, 2021, 5:57pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/18 "2021-07-08T17:57:15Z")

</div>

I did that logging, and it was always finished.

---

<div class="post-metadata">

**Author:** ![khaled](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/khaled/32/28528_2.png) [@khaled](https://forum.glyphsapp.com/u/khaled)\
**Post date:** [July 8, 2021, 6:02pm UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/19 "2021-07-08T18:02:33Z")

</div>

Yes, it started to crash again after finishing, looks like it was a mistaken observation. I’m thinking now if finding away to block any user interaction for few seconds and hope this is enough. `Message()` would do if there were away to close it automatically.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [July 9, 2021, 8:27am UTC](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535/20 "2021-07-09T08:27:17Z")

</div>

I played around with this a bit more. When I defined the class in ObjectivC directly, it worked fine. So it seems a strange bug in pyObjC.

[Next page](https://forum.glyphsapp.com/t/keeping-references-to-glyph-names-up-to-date/19535.md?page=2)
