Catch before / after closing model events?

I wonder if there is any way to catch that a model is going to be closed (PreClose event) and to catch when a model has been close (PostClose event).

Looking at ModelObserver there is the two convenient methods:

  • [# onPostSaveModel (model) ⇒ nil ] / [# onPreSaveModel (model) ⇒ nil ]*

Are there similar methods for the close operation?

If so, is it possible to cancel the close operation on certain conditions?


Perhaps the AppObserver onQuit ?

This sounds familiar. Perhaps this has been requested before ?

Check the issue tracker.

No, and I seriously doubt the SketchUp Team would ever implement something like this.

Ultimate control of (whether or not) to close the model and / or the application is a user feature.

Ie, … if the model has been modified then calling Sketchup.quit can still be cancelled by the user via the save dialog.

I can think of nothing that would anger users more than the application not doing what they tell it to do. (An extension that does something like this would IMO not be accepted for EW posting. It also would get a bad community reputation and be “in the doghouse” as an extension to avoid.)

Implementing a hack like causing some error to occur is likely to corrupt the model or the SketchUp user settings (that are written during error-free application close cycle.)

Note that some API methods like Sketchup.quit and Sketchup::Model#close were implemented for batch processing of folders of model files. (This predated the C API and it’s various wrappings in other programming languages which may be a better fit now for such batch work.)

If you explain what you wish to achieve, then perhaps some other members know of other workflows that may work.

1 Like

Can you elaborate on what you are trying to do. At a high level.

I would like to inform the user about the need to save its file in order to keep specific user data that has been embedded to model metadata. So the high-level needs are to catch the close event, display a dialog box and depending on the user choice cancel or continue the close operation.

This pipeline requires to be able to catch a pre-close event and possibly to cancel the close operation.

Sure cancelling a close operation must only be done under the user validation. In the scenario I thought about, the dialog box warns the user about something and asks him if he want to continue the close operation.

Asking a confirmation before closing a document might have sense. For example, in Photoshop, if you perform a crop operation on an unsaved document and close the document, the software first asks if you want to perform the crop (yes/no/cancel dialog), then it asks if you want to save the file (yes/no/cancel dialog) with different issues depending on what is answered.

SketchUp itself will always prompt the user to save the model if they simply close it, and it has been changed since the last save.
If they answer No then changes to the model and those changes that might result from your tool are lost.
If they answer Yes then you could use this Observer:
You can then either automatically save any metadata changes needed from your tool before the main save is done, or prompt the user with a choice - “Save changes to ‘MyTool’ ? Yes|No” - then the main save either saves or skips saving that metadata included…
Alternatively why not save the metadata as it arises ?
Why add the complication ?

1 Like

The goal is to warn the user that some metadata has been added to the model and that such meta data is important to be saved (the meta data adds is a process a little hidden to the user and the dialog box on the pre-close event gives some explanation on the fact it is important to save the model)

Moreover if you think to the example give above about the crop operation, you can realize that there is some case were this is important to cancel a close operation (with the user agreement).

When/how is this metadata added? As said Sketchup already asks if the user wants to save the changes, so if the user is even aware some metadata was added, I don’t see why the normal prompt isn’t enough.

1 Like

The meta data is added after an external visualization process.
Sketchup asks for saving. There is no problem with that and this could seem sufficient mechanism.

But if no edition has been performed on the mesh (only a visualization operation), most of the users answer no to the save question (they feel that nothing changed on the model).

Then I’d argue the visualization process changing the model even if the user doesn’t make any changes is the root cause of the issue. Are we talking about choices the user makes, like rendering settings, or just the renderer saving its default values to the model?

I’ve noticed that various other extensions also make “behind the curtain” modifications to a model, causing it to show as modified even if I have done nothing myself. These cause SketchUp to raise the “Save?” dialog on close. I find that situation annoying because I have no idea which extension made the change or what it did.

So, I can understand the rationale of wanting to inform the user that an extension has made changes the user did not knowingly perform themselves. That is a separate question from whether extensions should be making secret modifications at all.


on a mac you can add an xattr to the file itself without the need to save…

I posted example code here or at sketchucation 5 or 6 years ago…

that was the other time someone asked for this, so I can’t see it as a high priority for the SU dev team…

whether you should is another question, but at least it dosen’t mess with the model…


Thanks for all your comments. xattr can be a way to store the attribute on MacOS.
It seems that there is something equivalent on Windows (NTFS but not FAT, so not really universal).

Finally the answer to the post question is the following:

  • There is no PreClose/PostClose events, and consequently no way to cancel a close operation (with user consent)
  • There is no plan to add such events

Consequently I probably have to find an alternate way to notify the user that saving the file is a good idea :wink:

I think this is really about letting the user know they made a change to the document while they do it, not tack on a message that may show up much later when they cannot connect it to any specific action.

An alternative is to save the metadata externally with your extension’s user settings instead of inside the file. Or alongside the file (using the same base filename) but in a data JSON file specific to your extension.

1 Like