Action listener in ASOC

Hey guys.

Im looking for a way to track property changes.

For example:

[format]
script ImageViewSubClass
property parent : class “NSImageView”
property test: “”

someFunction()
    set  test to "someValue"
end

end script
[/format]

I have a sublassed image that sets the value of its own property when a specific ation is executed (someFunction)
I’d like to execute a function OUTSIDE of this class when test is changed by observing it somehow… i know actionListeners from another language. Is there something similar in ASOC?

Hm i found the Observers but still have problems registering them

in my appDelegate i have

-- IB Outlets
myImage: missing value -- connected with the image that has the subclass from above assigned

on applicationWillFinishLaunching_(aNotification)
       myImage's addObserver_forKeyPath_options_context(me,current application's NSString's stringWithString_("test"),"NSKeyValueChangeOldKey",missing value)
    end applicationWillFinishLaunching_

but it always throws an error:

-[AppDelegate applicationWillFinishLaunching:]: *** -[myImage addObserver:forKeyPath:options:context]: unrecognized selector sent to object <shDragAndDrop @0x600000192f10: OSAID(46) ComponentInstance(0x810000)> (error -10000)

I don’t think that would do what you hope in any case – it would only tell you when the value of myImage changed, which presumably will never happen.

Hm… ok. Any way to achieve what I am looking for?

You need to have the someFunction handler post a notification to the notification center. So something like:

current application's NSNotificationCenter's defaultCenter()'s postNotificationName:"com.mybusiness.someNameOrOther" object:(missing value)

And then to receive the notification:

current application's NSNotificationCenter's defaultCenter()'s addObserver:me selector:"itHappened:" name:"com.mybusiness.someNameOrOther" object:(missing value)

on itHappened:notif
-- do stuff
end

And to stop receiving notifications:

current application's NSNotificationCenter's defaultCenter()'s removeObserver:me name:"com.mybusiness.someNameOrOther" object:(missing value)

or if it’s your only notification, simply:

current application's NSNotificationCenter's defaultCenter()'s removeObserver:me

Thank you! i got the notification to work.

is there a way to pass a value with the notification?

Yes – use -postNotificationName:object:userInfo:, and put the value in a dictionary that you pass to userInfo. The receiver can then ask for notif’s userInfo().

Great! Thank you