On Sep 21, 2011, at 13:34 , Nick wrote:

> I am trying to implement some kind of "memory" for the documents in my
> application. For example, each  document window has a position on the
> screen. So I'd like to see this position next time the user opens this saved
> document.

This is normally and ideally done by saving the state within the document file, 
whose contents are in a format suitable to the saving of that state along with 
the rest of the document contents.

> I can't save this info in the document file itself, as documents are plain
> text files.

Well, you've kinda shot yourself in the foot. There are two possible reasons 
for trying to use plain text as your document format:

1. Simplicity. That is, when you started, a string was all you thought you 
needed to store. If this was your reason (unlikely, I know), then it's time to 
design a new document format.

2. Interoperability. You need the file to be openable by other apps that deal 
only in text files.

In the latter case, there's no good solution. As soon as the file has been 
opened by a different app (or moved to a different machine, or a different 
volume, or whatever), the meaning of your saved state such as window position 
becomes meaningless or lost (in the worst case). Your file can't be a standard 
format and a custom format at the same time.

> How it is usually done? Where it is best to store this 'meta' information
> that is attached to the document, provided that it can't be stored in the
> document itself? Does Mac OS provide some kind of tracking of saved earlier
> documents (i believe it does, since it keeps history of recently opened
> documents. i don't know where though…).

xattrs are probably your best choice, if you want to try to get the information 
preserved as the file gets moved around. xattrs will get preserved by Mac OS on 
several different file systems. The information may still get lost if something 
else edits your files, or if the files are handled by a different OS.

I think NSURL resource methods are the easiest way to implement the xattr 
approach, if you can require 10.6.

> Should I store this information in some HFS's 'file fork'? Or is it better
> to create a "shadow file" for every document that keeps this metainformation
> (wouldn't like to do so, i keep that as the last resort).

It would probably cleaner to keep the information in user preferences, rather 
than using shadow files, if you decide to try to keep the information 
separately from the file itself. At least you wouldn't be littering the file 
system with invisible files, and proper use of NSURL bookmarks should allow you 
to track file movement within the file system in a lot of useful cases.

However, if there's anything *important* in your meta-information, something 
that it would really annoy a user to lose, then I strongly suggest you consider 
switching to a private document format, and use an Export function of some kind 
to spin off text files. But obviously there are many app scenarios where that 
isn't feasible.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to