Save/load content of vanilla List

I’m making a script with an editable vanilla list. I want to save the edits made to that list to be able to keep them at the next run.

For now, I have something like this:

def __init__(self):
    self.w.myList = List((m, yPos, -m, 120), self.return_list_content(), columnDescriptions=self.return_columns_titles(),editCallback=self.save_preferences)

def save_preferences(self,sender):
      Glyphs.defaults[ "com.joachimvu.Interpolator.ListSettings" ] = self.w.myList.get()
      return False
    return True

def return_list_content(self):
    if not Glyphs.defaults[ "com.joachimvu.Interpolator.ListSettings" ]:
      #build the list by default
      return Glyphs.defaults[ "com.joachimvu.Interpolator.ListSettings" ]

Every time I try to edit a field of the list, Glyphs crashes with the following error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

Doing either saving or loading preferences is working, the crash is when I want to do both (load at launch, AND save when edited), so I guess I have an idea of what is going wrong here, but don’t know how to solve it.

Any help appreciated! :pray:

When you get a dict from UserDefaults, it is always not mutable NSDictionary. So you need to convert that into a python dict or into an NSMutableDictionary.

The last line could be changed like this:

     return dict(Glyphs.defaults[ "com.joachimvu.Interpolator.ListSettings" ])

and the test should be:

     if "com.joachimvu.Interpolator.ListSettings" in Glyphs.defaults:

But that doesn’t work. I’ll fix it.

1 Like

Thanks Georg!

What about:

if not Glyphs.defaults["com.joachimvu.Interpolator.ListSettings"] is None:

that not need to be between the is and the None