Yeah this is a case that I haven’t quite found a solution to yet.
Unfortunately I can’t implement as you suggest because of Sandbox restrictions. When the user opens a file the system gives my app read/write access to that file, but not to it’s owning directory. That’s why it works if you drag the owning directory to the app … then you are giving the app read/write access to that entire directory hierarchy and I can show a library for that folder. (My app always has read/write access to it’s iCloud library, so that’s why I’m able to always show that).
I just looked at iA Writer and they solve the problem by showing an import button and explanation text in the library view when you are viewing a document that’s outside the library. But that seems kinda funky to me.
Another option might be to just disable the library view (hide it and don’t allow showing it) when you open a document that’s not already in the library. That’s a nice clean solution, but maybe confusing because you’ll wonder why “show library” doesn’t work.
Anyway, I’m open to suggestions… but alas when you open a file outside of the library I can just show the containing folder as the library as would seem to make the most sense.