NSWindow Programming

Hello everyone!

I have this Objective-C code that I am trying to convert to ASOC.

NSRect frame = NSMakeRect(0, 0, 200, 200); NSWindow* window = [[[NSWindow alloc] initWithContentRect:frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO] autorelease]; [window setBackgroundColor:[NSColor blueColor]]; [window makeKeyAndOrderFront:NSApp];
I know how to make the NSRect and make the window key and order it front, but the thing that I’m stuck on is the NSBorderlessWindowMask and the NSBackingStoreBuffered. My question is how to convert them to ASOC. The problem is that even wandering through the Developer docs for a while I still can’t find the parent class for those.

I hope this will be easy to solve.

Thank you!

I’m not sure what you can’t find the in the docs. The method is initWithContentRect:styleMask:backing:defer: and all the documentation is there with that method.


Well, if I were to convert the code right now, it would look like this:

property NSRect : class "NSRect"
	property NSWindow : class "NSWindow"
	global mainWindow, winRect
	set winRect to current application's NSMakeRect(0, 0, 200, 200)
	set mainWindow to NSWindow's alloc()'s initWithContentRect_styleMask_backing_defer_(winRect, NSBorderlessWindowMask(), NSBackingStoreBuffered(), false)
	mainWindow's autoRelease()
	mainWindow's setBackgroundColor_((class "NSColor"'s blueColor()))
	mainWindow's makeKeyAndOrderFront_(me)

I know this is wrong, and I am stuck again on the NSBorderlessWindowMask() and the NSBackingStoreBuffered(). I know that those are subclasses of some class, the problem is that I don’t know which class is their parent.

I don’t know where you got that idea. Masks are not classes they are usually enumerations. If you read the description of the method in question it says:

The window’s style. It can be NSBorderlessWindowMask, or it can contain any of the options described in “Window Style Masks,” combined using the C bitwise OR operator. Borderless windows display none of the usual peripheral elements and are generally useful only for display or caching purposes; you should normally not need to create them. Also, note that a window’s style mask should include NSTitledWindowMask if it includes any of the others.

You will also note that styleMask is typed as an integer. If you click on the “Window Style Masks” link it will take you to the constants section of the NSWindow class reference which shows you the possible values you can use – you can either use the names (which is probably best, but must be preceded by “current application’s”) or the number that the name represents.

The same is true for the backing parameter – there is a link that takes you to (or close to) the constants section that gives you the choices.


Thank you, rdelmar, for your patience as well as your help.

I have discovered the numbers I needed for the two settings. But I have one question left. How do I call the NSMakeRect method?

set winRect to current application’s NSMakeRect(0,0,200,200)

does not work. This

set winRect to NSRect’s NSMakeRect_(0,0,200,200)

also does not work.

I’m sure I’m doing something silly.

Once again, thank you!

NSMakeRect() is 10.7-only. Better to use:

 {origin:{x:0, y:0}, |size|:{|width|:100, height:200}}



I am using 10.7, but I used your method. It worked. Thank you, rdelmar and Shane!

Here’s the working script:

property NSRect : class "NSRect"
	property NSWindow : class "NSWindow" of current application
	global mainWindow, winRect
	on makeWindow()
		set winRect to {{100, 100}, {200, 200}}
		set mainWindow to NSWindow's alloc()'s initWithContentRect_styleMask_backing_defer_(winRect, 7, 2, false)
		--mainWindow's autoRelease()
		--mainWindow's setBackgroundColor_((class "NSColor"'s blueColor()))
		mainWindow's makeKeyAndOrderFront_(me)
	end makeWindow