views:

56

answers:

2

I have a Cocoa document-based app that (currently at least) functions as a basic text editor. It saves .txt, .rtf, and .rtfd, and loads those plus .doc and .docx. If I open a .doc or .docx file and edit it, then try to close, it reminds me to save, but the save option doesn't do anything since the application is only a viewer for those types of files. How can I make that function as "Save As" for types that can only be viewed, like .doc and .docx?

A: 

Not completely clear if you actually want to write the updated file after editing, or prevent editing and thus prevent the warning that the document has been modified.

To not see the Save warning, first you would want to set your document type role to "Viewer", if it happens to be "Editor". This is in the Target settings.

Then you need to 1. ensure that the contents of the document isn't changed, and/or 2. tell the document not to bother showing itself as dirty

However, if you want to allow editing and saving the document, you would have to write those files back in the proper format. That's non-trivial, except for the fact that the source code for TextEdit is available and included with Xcode. But from a cursory glance, it appears that NSDocument already supports .doc and .docx.

You will find the Project folder for TextEdit in /Xcode/Examples.

Bored Astronaut
I want to show the warning, but change "Save" to "Save As". That way if someone opens a read-only file and edits it, the "Save As" will allow them to save it in a supported write format. Currently all the action sheet allows them to do is "Save", which since it's read-only just returns them to the document without any feedback. I will take a look at the TextEdit source though, in case it contains .doc and .doxc save code.
jfm429
+1  A: 

Override the saveDocumentWithDelegate::: in your customised NSDocument to the following:

- (void)saveDocumentWithDelegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo
{
  if (delegate != nil)
  {
    // use delegate or contextInfo to decide what operation you need to use...

    [self runModalSavePanelForSaveOperation:NSSaveAsOperation
                                   delegate:delegate 
                            didSaveSelector:didSaveSelector 
                                contextInfo:contextInfo];
  }
  else
  {
    [super saveDocumentWithDelegate:delegate 
                    didSaveSelector:didSaveSelector
                        contextInfo:contextInfo];
  }
}

By default the delegate is either an NSWindow on window close or NSDocumentController if you quit the application and the controller enumerates the windows for reviewing changes.

psaghelyi
Thanks, it took me a while to implement it (got busy working on other things) but that code works great!
jfm429