Thursday, February 13, 2025

NSDocument Auto Saving and File Types

Gus Mueller:

Has anyone successfully come up with strategies for opting into NSDocument’s autosavesInPlace, but only for certain file types? I’ve looked into overriding scheduleAutosaving and friends, but nothing really works. TextEdit just throws up an alert saying “hey, lossy file format”. Is this the best I can do?

Brian Webster:

The issue is if you Save As where the original file type supports auto save but the new one doesn’t (or vice versa). The override is a class method and not an instance method, so there’s no way for the existing instance to flip its auto save boolean to reflect the new file type.

This is an interesting API problem. The core issue is that NSDocument wants you to have a single subclass for each family of file types that can be mutually converted via Save As. The configuration is done at the class level, so it assumes that each file type is just a different flavor that works in the same way.

One could argue that the Cocoa document architecture is missing several abstractions that would be needed for a proper general solution. The basic stuff both easy and quite configurable, with a small API surface, but to go beyond that you need to reimplement a lot yourself or try to hack it into the desired shape.

Dave DeLong:

Override NSDocumentController to provide unique NSDocument subclasses for each document so that each one can swap its own +autosavesInPlace method IMP without fear of messing up other documents, while also still preserving KVO behavior?

Update (2025-03-12): Gus Mueller:

Autosave has also had a revamp. There are three options now: “Off”, “Native Acorn Images”, and “All Images”. The default is set to saving native images (.acorn).

In Acorn 8.0 (and previous versions), when autosave was enabled, non-native files (.jpeg, .png) would open without a reference to the original file on disk. This is no longer the case in Acorn 8.1, where non-native files open with a reference to the original file, and pressing ⌘S will save back to the original, regardless of the autosave setting.

Why the change? I found myself wanting autosaving of files where full fidelity would always be preserved (which is what happens when you save .acorn files), but that behavior didn’t always make sense when opening a .jpeg file. JPEG files are lossy, so opening and saving the image multiple times would degrade the quality of the image. That’s not awesome. And you would also lose the edibility of text and layers.

1 Comment RSS · Twitter · Mastodon


FWIW, I filed FB16468885 saying "NSDocument's + (BOOL)autosavesInPlace should be an instance method"

I've also been using Dave's trick for making unique classes at runtime. There are a handful of gotchas and things to look out for, but so far it seems to be working.

Leave a Comment