Chapter 13. Saving, Loading, and Printing

In the previous chapter we saw how the contents of a text object can be translated into a Rich Text stream of characters. In this chapter, we’ll see how to take that stream and save it into a file; we’ll also see how to load one of those files and place its contents in a new window. Finally, we’ll learn about printing. All of these tasks will be made dramatically easier by using the Cocoa Application Kit framework.

Data Management with NSDocument

At this point, our MathPaper application does a great job with math, fonts, and handling multiple windows. However, it’s missing a lot of basic functionality, such as:

  • Saving the contents of a window into a file

  • Loading a saved file, so that you can continue calculating where you left off

  • Marking an edited window with the “unsaved” close button with a dot inside so that you know it has been edited (the “saved” close button has an X inside:

    Data Management with NSDocument

    )

  • Alerting a user who tries to close an edited window without first saving the edited file

  • Printing the contents of a window

  • Graying out menu items that are not appropriate in a given context (e.g., the Save menu item when there are no open documents)

Writing the code for document saving and loading can be quite an ordeal in some computing platforms. However, it’s easy in Cocoa because most of the required code is already part of the multiple-document framework. ...

Get Building Cocoa Applications: A Step by Step Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.