Adding NSServices to Xojo apps

After a conversation on the forums earlier this week I decided to restart in my attempts to add NSServices to Xojo apps. I created a small class that creates the necessary command for a post build script. When the plist file for the app is inspected, the services appear as expected, and they even appear in the contextual menu of Safari when text is highlighted. However, the selection of the menuitem is not properly dispatched to my app to be used, and the console reports “Cannot find service provider for selector doTest:userDate:error: or doTest:: for service doTest.” As far as I can tell from the Apple docs I have registered my service and set the service provider in my app. I created a subclass of NSObject with the runtime library then attached a new method to it with class_addMethod and mapped it to a shared method in a Xojo class. Creating the subclass is successful and adding the method returns true which means it should have worked. The shared method is never called in my class, and I’ve been banging my head against a wall for the last two days trying to figure it out. Does anyone have any ideas on how to do this through declares without plugins? Any help would be appreciated, and I will definitely share it for others to use here on the forums or through MacOSLib. Thanks
Link to my project.

Did you do the part “Registering the Service Provider” from the Apple docs? Seems like that might be the missing piece of the puzzle…

Just a guess, but you’re setting up the object callback in the Window1 Open event. That may not be soon enough, maybe try App.Open.

I did that, I used sharedApplication to get the NSApp reference then setServiceProvider with the app ref and the instanceId (pointer to my class). Unfortunately I don’t think that’s the problem.

I just tried that, but it didn’t fix it.

I got it working with these changes

Private Shared Sub impl_Service(pid as Ptr, sel as Ptr, pb As Ptr, udata as ptr, err As Ptr)
in constructor…

  if not class_addMethod(myNSObjectServiceProvider, NSSelectorFromString("doTest:userData:error:"), AddressOf impl_Service, "v@:@@@") then

and also I called init here, not sure if that matters.

oh, and “provider=new NSServiceProvider” is done in App.Open if that matters.

Excellent thanks. I tried close to that but didn’t include the “pb as ptr” and corresponding @. The doesn’t matter, it works either way, just so you know.