My app needs to open a bunch of files/documents in order to gather information from them into an overview table in a separate window. Since it can be a bit cluttered to really display all these document windows, I’d like to have them hidden/invisible by default. To open the files, I use the method openDocumentWithContentsOfURL_display_error_ of NSDocumentController. The display argument may seem promising, but as it turns out it has to be set to true, since otherwise I can’t inspect the document for its contents.
My current solution is to set the alpha value of the window to 0 and then also tell the window to setExcludedFromWindowsMenu_(true). It works pretty well in simulating hidden document windows, but it feels like a crutch, and sometimes there is a small glitch during the launch of the app where a window might be seen for a tiny fraction of a second before it becomes invisible. And if the user uses the keyboard shortcut for “Move focus to next window in application” the focus will move to the next invisible document, and this might confuse the user.
I wonder if there is a better method to achieve what I want?
In the method used, openDocumentWithContentsOfURL_display_error_ of NSDocumentController, if the display argument is true, then the method will run both makeWindowControllers() and showWindows() of the document.
I have found that if the display argument is false, I must “manually” run makeWindowControllers() and (for some strange reason) also poke the document’s windowForSheet(). If I do these two things, the documents will open hidden and I can still inspect them for various stuff I need to collect.
So with the display argument false, I simply run: theDoc’s makeWindowControllers()
theDoc’s windowForSheet()
The last line is strange, because it really doesn’t appear to do anything explicitly, but still, the line is needed. Without that line I can’t inspect the document for all the stuff I want. Although it would be nice to know why that line is needed, It’s not absolutely necessary because my code now does what I originally wanted. No glitches so far.
That’s right, using windowControllers()'s objectAtIndex_(0)'s |window|() works just as well.
(I assume this suggestion was only as a test? It’s not going to be “better” or more efficient or safer or clearer, is it?)
The “comfortable” explanation might thus be: the document isn’t actually created until something calls for the window (whereas calling properties of the document is not good enough).