Question or issue on macOS:
I would like to add an item in my Finder’s contextual menu whenever I right-click on files or folders, and this menu being linked to a method of my Cocoa app.
I am following CocoaDev’s example and Apple’s documentation, but I can’t get the service being displayed.
Here is my .h
#import @interface AppDelegate : NSObject @property (assign) IBOutlet NSWindow *window; -(void)IClicked:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error; @end
.m
#import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { [NSApp setServicesProvider:self]; } - (void)IClicked:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error { NSLog(@"I clicked"); } @end
and the extract of my Application-plist.info:
NSServices NSKeyEquivalent default E NSMenuItem default My Application NSMessage IClicked NSPortName TestService NSSendFileTypes public.item NSSendTypes NSPasteboardTypeString NSRequiredContext NSServiceCategory public.item NSReturnTypes NSPasteboardTypeString
I uploaded the code to http://www.petits-suisses.ch/TestService.zip.
What did I wrong, or is there any available Cocoa code I can download to understand what I did wrong ?
Thanks !
How to solve this problem?
Found the issues:
1. I really had to store my application at least once into the Applications folder (which is not done by default when you compile with Xcode)
2. Should have added NSUpdateDynamicServices(); after the [NSApp setServicesProvider:self]; command.
3. Wait roughly 10 secs after having started the app to get Finder’s Services menu populated.