Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to add item in Finder's contextual menu using services in Cocoa


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 <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate,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:

<key>NSServices</key>
<array>
    <dict>
        <key>NSKeyEquivalent</key>
        <dict>
            <key>default</key>
            <string>E</string>
        </dict>
        <key>NSMenuItem</key>
        <dict>
            <key>default</key>
            <string>My Application</string>
        </dict>
        <key>NSMessage</key>
        <string>IClicked</string>
        <key>NSPortName</key>
        <string>TestService</string>
        <key>NSSendFileTypes</key>
        <array>
            <string>public.item</string>
        </array>
        <key>NSSendTypes</key>
        <array>
            <string>NSPasteboardTypeString</string>
        </array>
        <key>NSRequiredContext</key>
        <dict>
            <key>NSServiceCategory</key>
            <string>public.item</string>
        </dict>
        <key>NSReturnTypes</key>
        <array>
            <string>NSPasteboardTypeString</string>
        </array>
    </dict>
</array>
</dict>

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 !

like image 915
Laurent Crivello Avatar asked Feb 24 '12 11:02

Laurent Crivello


1 Answers

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.

like image 192
Laurent Crivello Avatar answered Oct 26 '22 10:10

Laurent Crivello