NSColor class methods

I’m trying to use the NSColor class methods for predefined colors, but the color change does not happen, for the
control I’m trying to change, I’m sure its simple but i’m stuck with it, any help would be appreciated.

for example.

[code]property borderColor : class “NSColor” of current application
property myBox : missing value --IBOutlet bound to an NSBox on the main window

myBox’s setBorderColor_(borderColor’s blueColor()) --not changing the NSBox’s border color, but compiles and runs app[/code]
Many thanks

Regards Mark

Is you box the custom type? The default box you get in IB is “Primary” and you can’t change its border or interior colors. Also there’s no need for the “of current application” in your property declaration. It could be quite confusing to anyone reading your code to call the NSColor class “borderColor”, it would be better to call it NSColor or to just use "current application’s NSColor’s blueColor() in your setBorderColor_ method.

Ric

Also when you want to use a class in a property to avoid current application you should use it outside the class.


property NSColor : class "NSColor"

script myClass
property parent : class "NSObject"
property myBox : missing value

on myMethod()
myBox's setBorderColor_(NSColor's blueColor())
end
end script

Ric i’ve set it to both primary and custom settings, but neither work.
Yes I agree with your comments about my syntax, I have not really written the code as above, that
was for example purposes.
I usually use a syntax of this type.

property NSColor : class “NSColor”

NSColor’s doSomething

The post was about wether I was calling the NSColors blueColor: method correctly in ASOC.

Thanks DJ, the above code was for demonstration purposes only, not the way I use it in
my projects, I was only asking if the class method was used this way or not.

for example blueColor()

is that correct.

Regards Mark

To set the border color, you probably need to include:

myBox's setBoxType_(current application's NSBoxCustom)
myBox's setBorderType_(current application's NSLineBorder)
myBox's setBorderWidth_(1) -- whatever

But both the box type and line border can be set in IB – I’ve tried it and it works. If you have the box set to custom, and the border set to line it should work. Are you sure you have the property myBox hooked up?

Ric

DJ,

Why? I’ve always included it inside the class and it works fine.

Ric

Thanks Shane.

I have set various attributes of the NSBox in IB, and in code, but the color change method was not working as expected.
The original question was more about the ASOC way of doing things, than the syntax or control settings.

for example.

in Objective-C I would use this type of thing, which would work fine.

[code]NSColor borderColor; //instance variable in .h file

borderColor = [NSColor blueColor]; //in the .m file[/code]
but in ASOC, I was looking for the equivilent.

I thought is was this.

[code]property NSColor : class “NSColor”

set borderColor to NSColor’s blueColor()[/code]
was I incorrect, just trying to get a handle on the correct ASOC of ObjC code.

regards Mark

was I incorrect, just trying to get a handle on the correct ASOC of ObjC code.

nope, you were correct. The class returns an new NSColor object and should work properly. But not knowing how and were your lines are makes it harder to find where your problem is so I post the complete code not because to tell you what to do but to show you how colors need to be set and how they work well. So what I did was starting a new project and changed the app delegate to this:

property NSColor : class "NSColor"

script NSBoxBorderColorAppDelegate
	property parent : class "NSObject"
	property myBox : missing value
	
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
	
	on makeBoxBorderColorRed_(sender)
		myBox's setBorderColor_(NSColor's redColor())
	end makeBoxBorderColorRed_
	
	on makeBoxBorderColorBlue_(sender)
		myBox's setBorderColor_(NSColor's blueColor())
	end makeBoxBorderColorBlue_
end script

Then I opened the mainMenu.xib and added a NSBox to the default window. Set (like shane suggested programmatically) the box type to custom and border type to bezel or line and connect the box with myBox property of the app delegate. Then I added two buttons and connected both actions with the button. Then I only hit built and run and I saw the box changing the color. No magic tricks here…

AS DJ says, using a property like that works, and it avoids repeated use of “current application’s”. But once you start copying and pasting code, it gets messy; you have to copy and paste the code, and then do the same for the required properties. It’s easy to forget a class. For that reason, I skip the property and just use:

set borderColor to current application's NSColor's blueColor()

Good point shane… I’ll keep that in mind in future posts.

Back to your original question. I cut and pasted your code from the first post, and it worked fine, when the box was set to custom in IB. Here’s my whole script:

script AppDelegate
	property parent : class "NSObject"
	property borderColor : class "NSColor" of current application
        property myBox : missing value --IBOutlet bound to an NSBox on the main window
    
	on applicationWillFinishLaunching_(aNotification)
		myBox's setBorderColor_(borderColor's blueColor())
	end applicationWillFinishLaunching_
	
end script

If it’s not working for you, I can’t see why, unless your setBorderColor method is not being called (where is it in the script?) or myBox is not hooked up to the box in IB.

Ric

Thanks everyone I do now have it working.

I was also manipulating the box with code in other ways, which was affecting the border color action.

But you guys did confirm that it was as simple as NSColor’s blueColor.

Thanks again

Regards Mark