tags:

views:

43

answers:

1

I trying to watch out for a text field to change, using macruby.

AXObserverCreate expects an AXObserverCallback as parameter. How can I pass a ruby function as a callback?

My function in AX.m:

+ (AXError) addNotificationWithElement: (AXUIElementRef) element
             forEvent:(CFStringRef) event_type
             callback:(AXObserverCallback) acallback;

This is how I want to invoke this method:

rb_main.rb:

def my_callback *args
  p args
end

p AX.addNotificationWithElement ref,
                      forEvent:'AXValueChange',
                      callback: *** What to write here?***

What to pass to my addNotificationWithElement function that expects an AXObserverCallback as callback parameter?

For reference: Apple documentation for AXObserverCallback

A: 

You should be able to pass any Proc (or lambda) that takes the arguments given in Apple's documentation. In this case:

proc { |observer, element, notification, refcon| ... }

You could use the following in your example (i.e. if the callback is a method):

proc { |*args| my_callback *args }

I haven't tested it in cases where AXObserverCallback is required, but this seems to be the general pattern.

molf