2

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?

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • 2
    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 Oct 16 '10 at 22:18
  • 1
    @Peter Hosey Thats great, but thats not what I need which is why I asked this question. – Justin Meiners Oct 16 '10 at 22:29
  • Justin Meiners: You didn't answer my question. What do you need the real destination URL for? – Peter Hosey Oct 17 '10 at 01:05
  • In my file that I save I reference other files on the computer relatively from the save destination. So I don't really need to physicaly modify anything after its saved I just need to know where its headed in my save. – Justin Meiners Oct 17 '10 at 02:10
  • 1
    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 Apr 08 '13 at 15:17
  • @MikeAbdullah I don't quite follow... I won't know relative paths until I know where it is saved – Justin Meiners Apr 09 '13 at 01:53
  • 1
    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 Apr 09 '13 at 08:11

1 Answers1

6

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:

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75