Thursday, January 19, 2017

#1 2016-07-28 11:00:21 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Action listener in ASOC

Hey guys.

Im looking for a way to track property changes.

For example:


script ImageViewSubClass
    property parent : class "NSImageView"
    property test: ""

    someFunction()
        set  test to "someValue"
    end
end script

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?

Offline

 

#2 2016-07-28 11:39:33 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Action listener in ASOC

Hm i found the Observers but still have problems registering them

in my appDelegate i have


Applescript:

-- 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:

Applescript:

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

Last edited by Jonathan Engelhardt (2016-07-28 11:40:50 am)

Offline

 

#3 2016-07-28 06:50:15 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4885

Re: Action listener in ASOC

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#4 2016-07-29 12:08:27 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Action listener in ASOC

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

Offline

 

#5 2016-07-29 12:20:16 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4885

Re: Action listener in ASOC

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

Applescript:

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

And then to receive the notification:

Applescript:

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:

Applescript:

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

or if it's your only notification, simply:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#6 2016-07-29 01:27:18 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Action listener in ASOC

Thank you! i got the notification to work.

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

Offline

 

#7 2016-07-29 02:04:28 am

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 4885

Re: Action listener in ASOC

Jonathan Engelhardt wrote:

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().


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2016-07-29 05:50:46 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Action listener in ASOC

Great! Thank you

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.041 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)