Hi, 
applicationDidFinishLaunching is called after loading the app (but not after 
loading documents).
To do the same after loading a document you must use an awakeFromNib method in 
your Document class.

The reason is as you an see from the log that your characterWindow isn't 
connected yet to the window.

BR,
Nikolaus

> Am 29.08.2024 um 14:34 schrieb Sebastian Reitenbach 
> <[email protected]>:
> 
> Hi Nikolaus,
> 
> On Thursday, August 29, 2024 09:54 CEST, "H. Nikolaus Schaller" 
> <[email protected]> wrote:
> 
>> Hi,
>> there may be a completely different approach:
>> 
>> - add an IBOutlet (e.g. called mainWindow) to your NSDocument for your 
>> NSWindow and connect it.
>> - then you get a pointer mainWindow that can be accessed in -awakeFromNib; 
>> for your makeKeyAndOrderFront:
>> 
>> This avoids scanning the top level objects.
> 
> thank you. That seems to do the trick for the simple test App I created. 
> After creating IBOutlet and wiring it up in Gorm, I added the following at 
> the end of applicationDidFinishLaunching:
> 
>      [mainWindow makeKeyAndOrderFront: self];      
>      NSLog(@"the key Window: %@", [[NSApplication sharedApplication] 
> keyWindow]);
> 
> which spits out:
> 2024-08-29 11:49:14.793 Test5[72122:2615662661832] the key Window: <NSWindow: 
> 0x2609f549008>Number: 4 Title: Test5 Window
> 
> However, that same approach doesn't work in my Document based app. I read 
> through David's "Cocoa Programming, Developers Handbook", and now I'm working 
> with ChatGPT as my mentor here, since never done that before, and don't have 
> any Mac programming experience ;)
> What I told ChatGPT I want to do: MVC Document based app, that should have a 
> central .gorm file for menu and other central stuff, and then .gorm files for 
> each of different types of documents to be handled.
> 
> Now I ended up with:
> * central .gorm file containing my menu, connected to an AppDelegate, that 
> loads the Gorm file.
> * a DocumentController class (NSDocumentController subclass), which based on 
> what types of documents I want to create new, calls out one of my two 
> Document classes (NSDocument subclasses). To each of these NSDocument 
> subclasses, I've separate .gorm files.
> 
> Opening the Window with your approach works so far that the window opens, but 
> it doesn't become the key window:
> 
> With this in the makeWindowControllers method:
> 
>  [characterWindow makeKeyAndOrderFront:self];
>  NSLog(@"THE KEY WINDOW: %@", [[NSApplication sharedApplication] keyWindow]);
> 
> it opens the Window, but logs:
> THE KEY WINDOW: (null)
> 
> With above output, it's the only window (besides the menu), that the 
> application has, and it takes 
> input, but I wonder why the application doesn't even have a keyWindow at all?
> I thought at least one should always be Key, and with that firstResponder, 
> but may have misunderstood? 
> 
> ChatGPT insists on going through the topLevelObjects. But now, I'm not sure, 
> even if that works,
> if it then would make the window key?
> 
> Sebastian
>> 
>> Hope this helps,
>> Nikolaus
>> 
>> 
>>> Am 29.08.2024 um 12:06 schrieb Sebastian Reitenbach 
>>> <[email protected]>:
>>> 
>>> Hi,
>>> 
>>> I'm trying to create a document based app with ProjectCenter and Gorm, 
>>> following different advices,
>>> it boils down to use NSBundles loadNibNamed: owner: topLevelObjects:
>>> Then afterward, find the window in the topLevelObjects, that I want to have 
>>> my key window, and make it makeKeyAndOrderFront:
>>> 
>>> Whatever I do, the topLevelObjects I get returned are empty.
>>> 
>>> To demonstrate, I just created a New Application in ProjectCenter, opened 
>>> the Interface in Gorm, and 
>>> dragged a NSWindow into the Objects. Example code here: 
>>> https://github.com/buzzdeee/testTopLevelObjects
>>> 
>>> Then added this to AppController.m applicationDidFinishLaunching:
>>> 
>>>  NSLog(@"here in AppController  applicationDidFinishLaunching...");
>>> 
>>> NSBundle *mainBundle = [NSBundle mainBundle];
>>> NSArray *topLevelObjects = nil;
>>> NSLog(@"going to load .gorm file");
>>> BOOL success = [mainBundle loadNibNamed:@"Test5" owner:self 
>>> topLevelObjects:&topLevelObjects];  
>>> NSLog(@"topLevelObjects: %@", topLevelObjects);
>>>   if (success) {
>>>       NSLog(@"Successfully loaded Test5.gorm");
>>> 
>>>       if (topLevelObjects == nil || [topLevelObjects count] == 0) {
>>>           NSLog(@"No top-level objects were loaded.");
>>>       } else {
>>>           NSLog(@"Top-level objects: %@", topLevelObjects);
>>>           for (id obj in topLevelObjects) {
>>>               NSLog(@"Loaded object: %@", NSStringFromClass([obj class]));
>>>           }
>>>       }
>>>   } else {
>>>       NSLog(@"Failed to load Test5.gorm");
>>>   } 
>>> }
>>> 
>>> The output is:
>>> 2024-08-29 09:20:30.103 Test5[81465:10651290713480] here in AppController  
>>> applicationDidFinishLaunching...
>>> 2024-08-29 09:20:30.104 Test5[81465:10651290713480] going to load .gorm file
>>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] topLevelObjects: (null)
>>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] Successfully loaded 
>>> Test5.gorm
>>> 2024-08-29 09:20:30.113 Test5[81465:10651290713480] No top-level objects 
>>> were loaded.
>>> 
>>> When in Gorm, I enable "Visible at launch" for the NSWindow object, output 
>>> changes to:
>>> 
>>> 2024-08-29 09:13:57.990 Test5[1172:4346915151048] here in AppController  
>>> applicationDidFinishLaunching...
>>> 2024-08-29 09:13:57.990 Test5[1172:4346915151048] going to load .gorm file
>>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Exception occurred while 
>>> loading model: Index 3 is out of range 3 (in 'objectAtIndex:')
>>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Failed to load Gorm
>>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] topLevelObjects: (null)
>>> 2024-08-29 09:13:57.998 Test5[1172:4346915151048] Failed to load Test5.gorm
>>> 
>>> However, the .gorm file still loads, and I get to see the menu and window. 
>>> But I guess that is because, NSMainNibFile = "Test5.gorm"; in my Info.plist.
>>> 
>>> In any case, shouldn't there be some topLevelObjects in the .gorm file? 
>>> 
>>> latest gnustep-make/base/gui/back/objc2/ProjectCenter releases, Gorm-1.3.1
>>> 
>>> thanks,
>>> Sebastian
>>> 
>>> 
>> 
> 


Reply via email to