peerless
peerless

Reputation: 548

NSService not calling its NSMessage

I'm trying to add a Finder service and all looks fine until I want the service to do its job.

This is the method in my AppDelegate.m:

-(void)uploadFromPasteboard:(NSPasteboard *)pboard userData:(NSString *)udata error:(NSString **)err
{
    NSString *filename = [pboard stringForType:NSURLPboardType];
    dbg(@"file: %@", filename);
}

The plist configuration:

<key>NSServices</key>
<array>
    <dict>
        <key>NSRequiredContext</key>
        <dict/>
        <key>NSMenuItem</key>
        <dict>
            <key>default</key>
            <string>Upload File</string>
        </dict>
        <key>NSMessage</key>
        <string>uploadFromPasteboard</string>
        <key>NSPortName</key>
        <string>Finder</string>
        <key>NSSendTypes</key>
        <array>
            <string>NSURLPboardType</string>
        </array>
        <key>NSReturnTypes</key>
        <array/>
    </dict>
</array>

All seems fine, the service is displayed in the service menu, but when I click it, nothing happens, no logs or anything else, like its not called at all.

Could someone point me whats wrong cos I'm starting to pull my hair hardly :(

Upvotes: 2

Views: 771

Answers (1)

ipmcc
ipmcc

Reputation: 29946

Are you setting your service provider instance? Like this (from: Providing a Service):

EncryptoClass* encryptor = [[EncryptoClass alloc] init];
[NSApp setServicesProvider:encryptor];

Merely having this method in your app delegate class is not enough. In the standard application set-up, having this in your app delegate might be sufficient:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [NSApp setServicesProvider: self];
}

Also you're specifying "Finder" for NSPortName. This is probably not correct. From Services Properties (emphasis mine):

NSPortName is the name of the port on which the application should listen for service requests. Its value depends on how the service provider application is registered. In most cases, this is the application name. This property is ignored for Automator workflows being used as services.

My reading of the documentation is that the application whose name is in NSPortName is the application that will be used to handle the service request. If the name of your app isn't "Finder" (and it shouldn't be, for obvious reasons) then your app will never be called by the service.

Upvotes: 2

Related Questions