1
votes

I am trying to get the path that an NSDocument is being saved to at save time. I tried overriding writeToURL but that would pass me an obscure temp file URL, which was not where it was getting saved. Also asking the document like this [document fileURL] only works after it has been saved. I can get the path when it is loaded but I need some way for that initial save. Is there a way I can get the real file path when an NSDocument is saved?

1
Why? NSDocument passes you the URL to a temporary file for atomic writing; if you intend to change anything about the file, that's the file you should change. It will replace the real file with the temporary file (atomically) when you're done. If you need to get something from the original file, then the original file must exist for you to get it from there. - Peter Hosey
@Peter Hosey Thats great, but thats not what I need which is why I asked this question. - Justin Meiners
Justin Meiners: You didn't answer my question. What do you need the real destination URL for? - Peter Hosey
Justin, in which case you almost certainly want to store bookmark data for referencing those files. It can do a far better job of locating files than any sane third party is willing to spend the time writing. In which case, all you need to do is generate the bookmarks relative to the document URL being written and they should still resolve fine - Mike Abdullah
The point is to do away with the notion of relative paths. They are inherently fragile. Instead, generate bookmark data using the document URL as the relativeURL. Cocoa will take care of encoding that efficiently for you, and relocating the file on-demand. - Mike Abdullah

1 Answers

5
votes

It would really help if you told us more about why you want this.

That said, the lowest level method with this information is -writeSafelyToURL:ofType:forSaveOperation:error: