Draw in NSView

Hi,

can someone enlighten me? I don’t understand how I can do something simple as drawing a rectangle in a view.

I tried this:

script MainView
property parent : class “NSView”
property NSBezierPath : class “NSBezierPath”
on initWithFrame_(frame)
continue initWithFrame_(frame)
return me
end initWithFrame_
on drawRect_(aRect)
set vRect to {{10, 10}, {100, 100}}
set vPath to NSBezierPath’s bezierPath
vPath’s appendBezierPathWithRect_(vRect)
end drawRect_
end script

There is no error message, but also no rectangle.

With Objective-C, it should look like:

NSBezierPath* vPath = [NSBezierPath bezierPath];
[vPath appendBezierPathWithRect:NSMakeRect(10.0, 10.0, 100.0, 100.0)];

but if I try:

vPath’s appendBezierPathWithRect_(NSMakeRect(10.0, 10.0, 100.0, 100.0))

I get the error message:

[MainView drawRect:]: *** -[MainView NSMakeRect]: unrecognized selector sent to object <MainView @0x200755260: OSAID(7)> (error -10000)

Thank you very much in advance,

Thomas

Hi,

I found the answer myself - I simply forgot

vPath’s stroke

Silly my :frowning:

Thomas

Should someone want to see a very simple example - just the bare bones: http://gutzmann.blogspot.com/2010/02/applescriptobjc-nsview-drawrect-example.html

FWIW, I tend to use a list of records rather than a list of lists for NSRect, like so:

{{x:100,y:50},{height:200,|width|:400}}

Helps remind me of the order, and you need to use something like that if you retrieve NSRects.

Hi Shane,

Thank you very much for this hint. I was considering creating an NSRect explicitely should I need to access the properties later, but this is very elegant.

I still have to find the optimal way of coding ASOC. As far as I can see from examples and posts, I’m not the only one struggling to find the right balance between AppleScript and ObjectiveC; very often ObjC seems to be easier to read and understand, but then there are tricks like yours which render many ObjC constructs unneeded.

Cheers,

Thomas