3

In my NSDocument based app, my documents are to be associated with directories (they are an index of the directory)

I want to be able to open/save documents according to the following use case:

  1. Opening a directory: a) if there is a .myapp file in the folder, open it b) else look if there is HASH-OF-PATH-basename.myapp file in ~/Library/Application Support/MyApp and open that b) else create a new document in memory

  2. Opening an xxx.myapp file, as normal

  3. Save / Autosave a) if source of current doc was 1b, 1c, or 2 save file to ~/Library/Application Support/MyApp/HASH-OF-PATH-basename.myapp b) if source of current doc was 1a, save to that .myapp file

  4. Save As / Save To b) save an xxxxx.myapp to wherever the user specifies

The obvious thing seemed to be just to implement readFromURL:ofType:error:L and writeToURL:ofType:forSaveOperation:originalContentsURL:error: and switch URL as necessary based on what the saveoperation was.

This works in that I can open files and folders, and when I save an open folder my file is saved to Application Support, but then after writeToURL:ofType:forSaveOperation:originalContentsURL:error an error is raised and a dialog shown with the following error:

NSDocument could not delete the temporary item at
file://localhost/private/var/folders/6P/6PNpIB-6HreGE+Ikqf5dWU+++TI/
-Tmp-/TemporaryItems/(A%20Document%20Being%20Saved%20By%20MyApp%203)
/SomeDirectory. 
Here's the error:
Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x200583a00 
"“SomeDirectory” couldn’t be removed."
Michael Johnston
  • 5,298
  • 1
  • 29
  • 37
  • +1 because NSDocument is a strange beast :) – jtbandes Aug 21 '11 at 01:39
  • it was odd, but I guess cocoa is doing something between saveToURL and writeToURL. Maybe it has to do with making saving atomic or "safe"? I wonder if you lose that when you override writeTOURL? – Michael Johnston Aug 21 '11 at 06:00

1 Answers1

1

This turned out to be fairly easy to resolve.

I overrode saveToURL:ofType:forSaveOperation:error: and did the url & type swapping based on SaveOperation type there.

Now things are working as expected.

Michael Johnston
  • 5,298
  • 1
  • 29
  • 37