Hey Everybody, I’d like to understand the fundamental impossibility of a subclassed NSView to read its own properties, like frame or bounds.
The NSView’s description says that: <MYView @0x60000015a6d0: OSAID(4) ComponentInstance(0x810002)>
All I wanted is to read frame size of my view inside its implementation. I tried several approaches: declare a property, write a method. I’m clearly missing something of how AppleScript works. When I expect the frame message I get nothing but the error:
script MYView
property parent : class "NSView"
property NSColor : class "NSColor"
on initWithFrame:frame
continue initWithFrame:frame
return me
end initWithFrame:
on drawRect:dirtyRect
continue drawRect:dirtyRect
NSColor's brownColor()'s setFill()
current application's NSRectFill(dirtyRect)
end drawRect:
on mouseUp:theEvent
display dialog my frame's |description|() as text
end mouseUp:
end script
Actually, the problem is more likely the fact that you’re trying to call the description method on what is probably a record at that stage. Try something like this:
display dialog (current application's NSStringFromRect(my frame())) as text
I presume you mean subclassing in ASObjC. It was covered fairly extensively in my earlier book on using ASObjC in Xcode. That was discontinued for a few reasons, one of which is that AppleScript’s memory management can make using ASObjC like that problematic.
Unfortunately that fails in recent versions, where NSRects are returned as lists of lists ({{x, y}, {width, height}}) rather than records. Depending on what you’re doing, you need to either check the class of the result, or use functions to extract individual values.