Questions tagged [nsdocument]

NSDocument is an abstract class that defines the interface for documents, objects that can internally represent data displayed in windows and that can read data from and write data to files.

NSDocument is an abstract class that defines the interface for documents, objects that can internally represent data displayed in windows and that can read data from and write data to files. Documents create and manage one or more window controllers and are in turn managed by a document controller. Documents respond to first-responder action messages to save, revert, and print their data.

Conceptually, a document is a container for a body of information identified by a name under which it is stored in a disk file. In this sense, however, the document is not the same as the file but is an object in memory that owns and manages the document data. In the context of the Application Kit, a document is an instance of a custom NSDocument subclass that knows how to represent internally, in one or more formats, persistent data that is displayed in windows.

A document can read that data from a file and write it to a file. It is also the first-responder target for many menu commands related to documents, such as Save, Revert, and Print. A document manages its window’s edited status and is set up to perform undo and redo operations. When a window is closing, the document is asked before the window delegate to approve the closing.

NSDocument is one of the triad of AppKit classes that establish an architectural basis for document-based applications (the others being NSDocumentController and NSWindowController).

References:

NSDocument Class Reference

479 questions
4
votes
2 answers

How to know when a Cocoa app is about to quit?

I have a NSDocument based application. I'd like to know when the application is about to quit to validate some things. I'd hoped there might be a method such as a applicationWillQuit, but looking through the docs for both NSDocument and…
Joey FourSheds
  • 479
  • 1
  • 7
  • 18
3
votes
1 answer

My NSDocument subclass needs to accept any kind of file

I am building a viewer application, which needs to accept "any" kind of file as the selected file to view. In fact it even should accept folders, because in that case I want to show information relevant about the folder such as number of files and…
robject
  • 808
  • 1
  • 6
  • 12
3
votes
4 answers

Check if file is open in any application

Is it possible to check if a file or bundle is opened by any Application? For example, lets say that I know that /Users/Foo/AwesomeDocument.txt exists and its open in TextEdit, can I reliable check from my app that the document is open? I'm okay…
JustSid
  • 25,168
  • 7
  • 79
  • 97
3
votes
1 answer

Is there a better way to access the Document from its subviews?

I have some classes located on my Document such as NSNotificationCenter and NSUndoManager that I need access to from my subviews. Right now I can access them by doing something like this: NSUndoManager *undoManager = [[[[[self view] window]…
panupan
  • 1,212
  • 13
  • 15
3
votes
1 answer

NSDocument subclass "close" method called twice

I have a document-based Cocoa application which subclasses NSDocument as MyDocument. Each MyDocument manages a separate background process (as an NSTask). I want to make sure that the NSTask is terminated when its corresponding MyDocument closes…
Judah Jacobson
  • 543
  • 4
  • 10
3
votes
1 answer

Prevent Opening new NSDocuments and show a warning message

I have an NSDocument based app in which I want to limit the number of documents open at the same time (for a Lite version). I just want to have n documents, and if the user tries to open more than n, show a message with a link to the full app…
Ernesto
  • 510
  • 3
  • 10
3
votes
2 answers

macOS - how do I remove 'duplicate', 'move to', 'rename' and 'save as' menu items from the File menu?

My document-based app doesn't want these items, they aren't in my storyboard. They get inserted by the system via this call chain. -[NSMenu insertItem:atIndex:] () -[NSMenu insertItemWithTitle:action:keyEquivalent:atIndex:]…
mr. fixit
  • 1,404
  • 11
  • 19
3
votes
0 answers

MacOS Document based application open file dialog search box not working

I am writing a macOS document based application in Swift with the following versions: MacOS: 10.13.4 Xcode: 9.3 Deployment 10.13 Sandbox: On I have noticed that the search box in the open file dialog is non-functional (ie it returns no results, date…
Bill Aylward
  • 31
  • 1
  • 1
3
votes
1 answer

Initial app launch from storyboard fails to call NSDocument init

I've written a macOS Document-type app with a storyboard, using the Xcode template, and somewhere along the line the association between the initial app launch and the document has varied from the expected pattern such that none of the NSDocument…
Feldur
  • 1,121
  • 9
  • 23
3
votes
2 answers

How to open programmatically a document in a NSDocument macOS app?

I'm new to macOS programming and I created a NSDocument app project to learn this architecture. All works correctly, I can create a document, save it, and open one from Finder using standard UI controls. I'm trying to save and open a document…
Cue
  • 2,952
  • 3
  • 33
  • 54
3
votes
0 answers

Change position of default NSWindow title for document-based app

I have a NSDocument-based Mac app. Each window gets a default title with the document name and its edit state automatically. My window is made up of a split view with a sidebar and the actual document contents. I use an NSVisualEffectView for the…
Mark
  • 6,647
  • 1
  • 45
  • 88
3
votes
1 answer

What's the standard way for NSDocument to send events to NSViewControllers?

(Background: I've got a Cocoa app I'm writing that works fine, but is too complex because it doesn't have good separation of model/view/controller. Therefore, I'm updating it to use NSDocument and NSWindowController/NSViewController. On the whole,…
3
votes
1 answer

Capturing Window Events in NSDocument

I have an document-based Cocoa application with a TextView and I would like to capture clicks on it, so I'm trying to intercept Window events like mouseDown, mouseUp, etc. then relate them to my TextView. I've tried a two things: 1.) I made the…
Donald
  • 1,718
  • 10
  • 18
3
votes
0 answers

Non-NSView NSResponder to handle NSDocument responder actions

My NSDocument subclass (MyDocument) is quite large and I was thinking about making a MyDocumentResponder (subclass of NSResponder) to handle document responder events in order to clean up the code. This seems better to me than a category on…
Trygve
  • 1,317
  • 10
  • 27
3
votes
1 answer

How to implement a complex NSDocument-based app that imports and exports non-native file types?

I'm working on an app that supports a very complicated set of operations. It's akin to something like Xcode, in that the main document is represented by something like a project window, with numerous entries in it. The document on disk is a bundle…
Rick
  • 3,298
  • 3
  • 29
  • 47