Macruby: using ruby method as AXObserverCallback
I am trying to follow the change of a textbox using macruby
.
AXObserverCreate
expects a parameter AXObserverCallback
.
How do 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 call this method:
rb_main.rb
:
def my_callback *args
p args
end
p AX.addNotificationWithElement ref,
forEvent:'AXValueChange',
callback: *** What to write here?***
What should I pass to my function addNotificationWithElement
that expects AXObserverCallback
as a callback parameter?
For reference: Apple Documentation forAXObserverCallback
You should be able to pass any Proc
(or lambda
) that takes the arguments given in the Apple documentation. In this case:
proc { |observer, element, notification, refcon| ... }
In your example, you can use the following (i.e. if the callback is a method):
proc { |*args| my_callback *args }
I haven't tested it where required AXObserverCallback
, but it seems to be a general pattern.
a source to share