How to call an exit function from a class

I am trying to modify a plugin (showNextFont by Guido Ferreyra ) so that an exit function is called just before the plugin is closed.

Not sure if this is the correct way but I’ve been trying variations of this kind of setup of __init__ __enter__ __exit__ functions and a with outside of the class.

Am I on the right track here? Are there plugin examples which use this approach? Is there another way of achieving this?

class showNextFont(ReporterPlugin):

	def __init__(self, name):
		self.filename = filename

	def __enter__(self):
		print ("enter")
		self.file = open(self.filename, 'w')
		return self.file

	def __exit__(self, type, value, traceback):
		if self.file:
		print ("exit")

snf = showNextFont.alloc().init()

with snf as file:
	print ("working")

I have never used this. But what are you trying it do?

I am trying to modify the showNextFont plugin to work as a toggle between previewing the first and next font.

When the plugin is running I am trying to turn off the preview of the font in view (by changing the master color custom parameter to white) to only see the preview of the next font…then when clicking to turn of the plugin, I want the preview color to return to the default.

I am looking for a way to call a final function just before the plugin closes to return everything back (maybe by setting the master color back to black).

You can’t do it like that. The those are quite different scopes. You are out of the with a long time ago. The reporters have a willActivate() and a willDeactivate(). That should do the trick. You need to implement those methods in your plugin.

So you try to disable the drawing of the current fonts outlines? There is a needsExtraMainOutlineDrawingForInactiveLayer_(layer) method for that. You just need to return False and Glyphs will not draw anything.

1 Like

Of course it would be as simple as something like that!
Okay that works perfectly.
Thank you Georg!

The plug-in is about to undergo a major update, probably tomorrow. Let me know your repo, so I can help you update yours.