1

The Mac OS X Human Interface Guidelines says in their section about windows:

Note that if a user opens, moves, and closes a document window without making any other changes, you should save the new window position but you should not modify the file’s date stamp.

How are we supposed to do this? If I have a document-based application and I want to save documents’ windows’ positions and sizes in their files—without touching their date stamps—I override dataOfType:error, etc. But, as far as I can tell, all of these automatically modify the file’s date stamp.

There’s only one way I see to do this, and it’s kind of a hack: use NSFileManager to temporarily save the file’s old date stamp, save the window’s position in the file, and then use setAttributes:ofItemAtPath:error to change it back.

Is there a better way? Surely if it’s a common task...

jschoi
  • 1,884
  • 1
  • 12
  • 26

1 Answers1

1

How about saving the window's position in NSUserDefaults instead of the document?

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Thanks for the answer! However, as far as I can tell, using `NSUserDefaults` would involve having to keep track of every document ever saved in the application. Whenever the application launches it would need to check whether any of the files in its set of saved files were removed from the system--actually even that might not work because they might just be in a removable disk.So maybe I'm missing something but I don't think this is viable. This is bothering me, since the HIGs say that this should be a common task... – jschoi Nov 01 '11 at 18:57
  • Sure - you can save the string-encoded rectangle using some unique document ID (a hidden UUID property in your doc's data should be fine). Realistically, I think it's a better idea (since it's user-specific on that machine (since other machines can have other screen geometry)) to put this in user defaults rather than writing the file and fudging the time stamp. Apple guidelines aren't *always* the best way to go... :-) – Joshua Nozzi Nov 01 '11 at 19:09
  • Consider this: If you store the rect in the doc and that rect follows the file to a new computer with smaller screen, you have to make sure the restored rect is adjusted for the smaller screen, thereby overwriting a perfectly valid position on the original computer. Now you re-oopen on the original computer with the other computer's "corrected" frame. Why? Why not store the user+computer-specific doc window frame on that computer so it's always valid no matter where else it's opened? – Joshua Nozzi Nov 01 '11 at 19:11
  • These are persuasive points, but what about accumulation of old document records in the app's data? Deletion checks can't be taken for granted—the document might be on a removable disc that might happen to be unmounted when the app does its missing documents sweep. Well, maybe that's a sacrifice worth making…in any case, thanks for the answer. This really does seem like the only other way. – jschoi Nov 09 '11 at 03:01
  • It's a tough call but you're right about old doc data in user defaults. If the user regularly creates/opens new docs, there'd be a lot of stuff in the defaults data. If you keep them in an array, the oldest will be the first n. At app launch, if there're more than, say, 10 docs remembered, remove the first count-10. Simple fix. – Joshua Nozzi Nov 09 '11 at 14:03