i’d like to save some “workspace” type settings: settings that are tied to the document rather than to the app itself.

things like the selected tab, the sidebar widths, etc.these need to persist even if the user doesn’t save the document — so they’re definitely settings, not document data.

anyone else doing this?

i was hoping there was some hidden corner of NSUserDefaults that handled this, but that doesn’t seem to be the case.

BTW: AI says i should roll my own and just save it to the App Support folder.

Fine, just feels pretty manual. Surprised there’s not a similar facility that is integrated with other user defaults.

@isaiah YMMV, but we've found from user feedback that (in document-based apps) people generally expect this sort of thing to be saved with the document (they'll temporarily change something, close the file, later reopen, and complain if it wasn't like it was when last saved).

So instead, we make those things part of the document format, but not be undoable (and so don't mark the document as edited, and don't prompt for save on close, etc).

@gregtitus that’s how i have it implemented today. it just feels a bit random when i close the document without saving and it reopens back where it was last time i opened it.

what id really like is something like Xcode where the file is located inside the document bundle, but not actually part of the document model. in other words i’m just sneakily storing it inside the bundle.

but there are about a billion complications with doing that — but it keeps the data with the document, which is nice.

@isaiah I've been doing that for years now. Just a dictionary of key values stored in a plist file within the document bundle. I just load it in when the document is instantiated. I modeled it directly after NSUserDefaults. I called it ..... TFUserDefaults. Very original 😆

@tapforms and do you save that data when the user saves the document?

if so, then that’s what i’m doing now.

or do you save a bit of extra data somewhere else with the project bundle — even if the user saves nothing?

@tapforms if you do save it to the project bundle, even when the NSDocument model hasn’t changed, can i ask how you decide when to write that file?

i can do it, but it seems like there’s a a potential race condition. when the user chooses save the project bundle will be replaced atomically i think — thus nuking any extra files in the bundle.

@isaiah I save whenever I call defaults.setXxxFor(key:). Just a plist write to the file system within the document bundle folder. DocumentProperties.plist is the file I wrote to. And since my app is a database app I write to the database all the time as users make changes. Not just by pressing save. My document is never really in a dirty state.
@isaiah For SwiftUI, SceneStorage is like AppStorage only per window. I don’t know how to map this to UserDefaults but there must be a link.
@troz @isaiah I think SceneStorage would roughly map to the AppKit state restoration stuff.

@bwebster @troz i thought state restoration was just to get the app back to where it was when it was last running. that’s not quite what i’m after.

i can open an Xcode project. select a specific item. then close the project.

when i come back to that project a year later (even if i’ve viewed 100 projects in between. and xcode will open my old project just as it was when i last looked at it.

it’s not app state. it’s not project file data.

it’s project/workspace state.

@isaiah @troz That’s correct, so scene storage/state restoration definitely wouldn’t fit the bill. I don’t think the frameworks have any built in solution for per document state, so it’s a roll-your-own world.

I have seen a couple apps store stuff in xattrs if it’s small. Might not survive copying to certain drive formats, but otherwise pretty easy out-of-band storage.

@troz per window isn’t really sufficient — unless the document is mapped to a specific window.

what i want is basically Xcode. if i open a project, i want the the same files open, the same left sidebar tab should be selected, as the last time i viewed that project.

this is neither a user defaults sort of thing (which is global, nor the settings of a particular UI element) nor is it part of the NSDocument model. it’s the stuff in the xcuserdata of each xcode project.

@isaiah @troz I suspect that Xcode does not use NSDocument, I think it predates that API. It probably has a bespoke implementation all its own. I don’t think you can duplicate that behavior with NSDocument. In Panorama, I just save that information as part of the document. If you want it to automatically save even if nothing else changes, it has to be hooked into the undo say. I didn’t do that, so metadata changes aren’t saved unless something else is changed, or if the user explicitly saves. Never had a single complaint about this behavior.
@isaiah I sometimes save these things as xattributes in the document itself. If you want them to be user-specific though, you'd probably be better off saving a separate per-document file in ApplicationSupport or something