• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDocument with fixed/auto-generated filename
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument with fixed/auto-generated filename


  • Subject: Re: NSDocument with fixed/auto-generated filename
  • From: James Bucanek <email@hidden>
  • Date: Tue, 20 Jun 2006 10:40:53 -0700

Martin Redington wrote on Tuesday, June 20, 2006:

>
>I have pretty much exactly the same problem as Lon and James.
>
>I would like to provide a default name for the document, and avoid
>the user having to see the Save Panel.
>
>In an ideal world, I would simply be able to call setFileName or
>setFileURL on the NSDocument, and then call saveDocument: to write it
>out.
>
<clip>
>
>So it would seem that according to the docs, this should do exactly
>what I want. I'm not keen to ditch the NSDocument framework, in order
>to get, what seems to me, to be a fairly simple variation.
>
>As it stands, when I call saveDocument: having set the fileName/URL
>(fileType) is correctly set as well, I get "The location of the
>document "DocumentName" cannot be determined", with Cancel and Save
>As options.
>
>I also get the disabled Save Menu that other people have reported.
>
>Any pointers on how to programmatically set the filename and save the
>file without user intervention would be great. Note that I would
>still like Save As, and Save to be operational.

I got around this problem with some, but not an overwhelming, hack of NSDocument. I think the fundamental problem is NSDocument's interpretation of fileName/setFileName:. In NSDocument's world, a file name set on a document means that the document *has* been saved to that file, not that it *should* or *will* be saved in that file. So manually setting the file name of a document sends NSDocument in to a tizzy, thinking that the document file is lost.

Looking back, I fixed the Save menu using validateMenuItem:

    case MENU_SAVE_ITEM:
        return ([self isDocumentEdited]);

I fixed the confusing Save dialog by overriding canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:. My replacement simply checks to see if the document has been edited and either puts up a modal sheet asking the user if they want to save their changes (no mention of a filename or where), or it just falls through and invokes the shouldClose selector with YES.

Finally, I overrode saveDocument to simply generate my internal filename and save the document. Do not call [super saveDocument:] to do the saving! NSDocument will get all confused with a file name for a document with no file.

I completely disable Save As... becuase it makes no logical sense to allow the user to specify a name for a file they can't name.

And for cosmetic reasons, I also override displayName so the window title is something other than the name of the actual file (which will make no sense to the user).
--
James Bucanek
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: NSDocument with fixed/auto-generated filename (From: Martin Redington <email@hidden>)

  • Prev by Date: Displaying Audio & Video in NSTextView
  • Next by Date: Re: Slice a .txt file in lines
  • Previous by thread: Re: NSDocument with fixed/auto-generated filename
  • Next by thread: Re: NSDocument with fixed/auto-generated filename
  • Index(es):
    • Date
    • Thread