AS-Studio -->> ASOC with multiple nibs

Well I don’t have a real code question but more a model question.

The Old model
For example when I had an addressbook application I would had main, main window and a edit panel script. Also I would had mainMenu.nib, mainWindow.nib and editPanel.nib file. All the events of every window went to the main script. Here it checks the window name of theObject where the event came from and then call a customClickHandler(theObject) of the assosiated script object of that window if a button was clicked. This model of programming a application gave me the nicest code and could keep the number of if statements small. Also loading everything to the main script would give every script object the same parent script. So after making a new record in edit panel script I could tell main window script object to update it’s table view.

The new model in ApplescriptObjC
Well the truth is that I don’t know exactly how to make this work in ApplescriptObjC. I still want the concept of every window (object) has it’s own nib file (like Safari) and not one nib file with 20 different windows, panels and drawers in one file. I don’t need anyone to write code for me but if someone could help me finding a model how I should begin from the appdelegate to the window beeing showed. I’ve watched the tutorials of allan craig and searched here as well but couldn’t find a real answer. So I know how to make actions and oulets if I have my window in the mainMenu.xib.

Any help would be appreciated!

Sorry I overlooked this post from mr stanley

http://macscripter.net/viewtopic.php?id=31200

If I were writing that again, I’d drop the init handler and change this:

           set myWindowController to current application's class "NewWindowController"'s alloc()'s init()

to this:

           set myWindowController to current application's class "NewWindowController"'s alloc()'s initWithWindowNibName_("NewWindow")

Both probably work fine, but I’m still a bit uneasy about AS inits.

Well I need the alloc’s, init() I guess otherwise the myWindowController isn’t set wich is strange because alloc will return an instance of itself.
Well after cleaning and cleaning and trying I came up with this. I think it suits best for me.

I make a new class that returns a NSWindowController with a nib already loaded.

property NSWindowController : class "NSWindowController"

script aWindowController
	property parent : NSWindowController
	
	on init()
		set thisWindowController to current application's NSWindowController's alloc()'s init()
		thisWindowController's initWithWindowNibName_("aWindow")
		return thisWindowController
	end init
end script

In the appDelegate when I load the class above I can call the instance methods of NSWindowController directly

script appnameAppDelegate
	property parent : class "NSObject"
	property mainWindowManager : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
		set mainWindowManager to current application's class "aWindowController"'s alloc()'s init()
		mainWindowManager's showWindow_(me)
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
end script

You shouldn’t use call init more than once on a class – the idea is that you call initWithWindowNibName_ instead of init. The vanilla init is not even the designated initializer for NSWindowController.

And you only really need to subclass NSWindowController if you want to add other stuff to it, otherwise you might just as well use NSWindowController itself.

The other thing is that initializing it at startup like that is negating some of the benefits of having a separate .nib, which is to defer handling it until it’s going to be used.

Hi stanley,

I’ve redesigned it again and don’t use another class anymore already. So I was goign already the right way after reading the alloc and init methods. In the documentation I found the same as your told me now. So this is the code how it lookd now (I removed the separate class).


property NSWindowController : class "NSWindowController"

script appnameAppDelegate
	property parent : class "NSObject"
	property aWindowManager : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
		set aWindowManager to current application's NSWindowController's alloc()'s initWithWindowNibName_("aWindow")
		aWindowManager's showWindow_(me)
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
end script

The initialisation at startup is with this nib, in reality it is a login window. Other nibs will be loaded when needed so after a user logged in on the server, another nib will be loaded.

That looks better.

Thanks for the help mr. stanley. It is good for my self-confidence that I’m working in the right direction.