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

眉间皱痕 提交于 2019-12-03 07:33:23

问题



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 !


回答1:


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.



来源:https://stackoverflow.com/questions/9430216/unable-to-add-item-in-finders-contextual-menu-using-services-in-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!