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
2
votes
1 answer

Cocoa and executable files

Hey, I am writing a Cocoa app for Mac OS X and I want it to be able to open executable files. That is, files with the 'execute' permission. These files usually do not have a file extension, so I don't know how to specify this in the plist file. How…
user142019
2
votes
1 answer

Setting NSDocument as delegate of NSMenu

I have a menu item whose state should depend on whichever NSDocument is open. From my understanding, to make its state change dynamically I should use the NSMenu delegate method menuNeedsUpdate:. It seems like I would want to have the menu's…
Judah Jacobson
  • 543
  • 4
  • 10
2
votes
1 answer

How can I add a custom document icon to my documents in a document-based Mac app?

To get custom document icons, do I need to add a new “Icon” set in Xcode’s Assets.xcassets folder by selecting Editor > Add Assets > App Icons & Launch Images > New macOS Generic Icon? Or do I just add a .icns file with appropriate sizes to the…
Mark Cowan
  • 93
  • 7
2
votes
0 answers

Restoring nested NSDocuments

I am working on an Xcode-like IDE for MacOS. The IDE opens source files (instances of TextDocument) and has a separate project file (an instance of ProjectDocument that contains project settings. Both are subclasses of NSDocument. When a source file…
2
votes
3 answers

Should I use an NSViewController for this?

I want my application to have a single document but several screens without resorting to several windows. What's the best way to achieve this? I'm thinking of using a single NSPersistentDocument that will start with a NSViewController displaying a…
ruipacheco
  • 15,025
  • 19
  • 82
  • 138
2
votes
1 answer

iCloud NSDocument save warnings - caused by lastUsedDate file attribute?

I have an app that uses NSPersistentDocument (without autosaving) on OS X and UIDocument (also without autosaving) on iOS. The file representation is Binary Core Storage. This app has been working fine since iOS 7 + macOS 10.10. If I open a document…
MichaelR
  • 1,681
  • 15
  • 28
2
votes
1 answer

Do macOS Document-Based Apps Automatically Enable Collaboration?

I am looking to build a document-based app on macOS using Xcode and targeting macOS High Sierra. Does NSDocument provide document collaboration similar to that of Pages? Apple's Document Based Apps page suggests that collaboration is a feature of…
Jake3231
  • 703
  • 8
  • 22
2
votes
0 answers

use NSDocument application via command line

here is the situation: Current situation I have an NSDocument application (macOS), which works currently only via the Graphical User Interface (GUI). The app is written in Swift 4. Wished situation I would like to offer a Commande-Line Interface…
vomi
  • 993
  • 8
  • 18
2
votes
0 answers

Adding window controller to NSDocument-based app ups retain count by two

I got an NSDocument-based app. In the document class, a window is created. This window controller is added in -makeWindowControllers (code below). If I check out the retain count of my controller ([_windowController retainCount]) right after adding…
2
votes
0 answers

Implementing renaming a document in an OS-X app

My end goal of this task is to have renaming feature similar to what Pages and Sketch have In the storyboard, I added a NSMenuItem, called Rename..., under File and I linked it to an action called renameDocument (which I believe already implemented…
James Park
  • 91
  • 1
  • 4
2
votes
0 answers

Best way to save a file using NSDocument?

I'm encountering problems saving a document as I can see in the debug area: NSFileVersion tried to tried to add a new generation and failed. Versioned file URL: file:///Users/mike97/Downloads/test%20copy.xml, contents URL:…
Mike97
  • 596
  • 5
  • 20
2
votes
0 answers

How to lock move, rename and delete operation of NSDocument file?

I am working on Mac OS multi-document application that uses NSDocument. I need lock rename, remove and move file operations when document is opened. How can I do it? I found lockDocumentmethod, but it locks file for writing too and it allows rename…
Victor.V
  • 177
  • 1
  • 9
2
votes
1 answer

Intercept Save from "NSDocument is about to close sheet"

When an NSDocument is marked as having changes and the user closes it, a sheet is shown with options for Don't Save, Close and Save. How can I intercept or disable the Save button? I have overridden -saveDocument: and -saveDocumentAs: but these are…
Trygve
  • 1,317
  • 10
  • 27
2
votes
1 answer

Multiple documents open at the same time, each with different menu item states

In a Cocoa document-based application, what's the best way to keep the menu item states in the menu bar in sync with the currently visible document? For example, say you have a document with a ruler view that can be hidden/shown via "Show Rulers"…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
2
votes
1 answer

How do I programmatically save an NSDocument?

I need to programmatically save the active document of an NSDocument based app from within a method of the NSViewController that controls the document view. Menu items do this by sending save() to the first responder. What is the best way to do…
Mike T
  • 1,163
  • 1
  • 11
  • 27