1
votes

I am trying to enable Services which operate on selected Files and Folders similar to those which can be invoked in Finder.

I have the following based on the Services Implementation Guide which works, but only for Text etc

+ (void)initialize {
    static BOOL initialized = NO;
    /* Make sure code only gets executed once. */
    if (initialized == YES) return;
    initialized = YES;

    NSArray *sendTypes = [NSArray arrayWithObject:NSStringPboardType];
    [NSApp registerServicesMenuSendTypes:sendTypes returnTypes:nil];
    NSLog(@"initialize MyTableView");
}
- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType {
    NSLog(@"validRequestorForSendType %@%@", sendType, returnType);
    if ([sendType isEqual:NSStringPboardType]) {
            return self;
    }
    return [super validRequestorForSendType:sendType returnType:returnType];
}

How do I implement URL?

The documentation states

Your NSResponder object can register any pasteboard data type, public or proprietary, common or rare. If it handles the public and common types, of course, it has access to more services. For a list of standard pasteboard data types, see NSPasteboard Class Reference.

There seems to be no corresponding pasteboard data type. I have tried NSURL, NSURL class NSFilenamesPboardType partially works, but does not show all the Services Finder does, and seems to belong to (OS X v10.5 and earlier)

Edit Clarification

I have been following the Services Implementation Guide, which seems to have 3 steps

1.Registering Objects for Services

NSApp registerServicesMenuSendTypes: returnTypes:

2.Validating Services Menu Items

validRequestorForSendType:returnType:

3.Sending Data to the Service

writeSelectionToPasteboard:types:

I have got the first 2 to partially work (although I cannot get the same Menu I see in Finder when a File is selected in particular the Files and Folders )

The 3rd seems to be the sticking point This seems to be supported for NSTextView, but deprecated for NSObject

I am really looking for some idea how to use Services for Files and Folders in the same way many other applications do.

Desired Services Menu

1
This question is confusing me. Do you want to display the same "Services" menu that appears in the Application menu (e.g. under the "TextEdit" menu when running TextEdit; under the "Safari" menu when running Safari).Michael Dautermann
What finder menu are you talking about??markhunte
also look at this answer stackoverflow.com/a/18579656/261305markhunte
I have no problem creating Services. What I want is to add to an application a Services Menu similar to the image edited aboveMilliways

1 Answers

1
votes

I have made this work, and am posting the result for the benefit of others who may want to do similar.

The first 3 methods are in my Class which subclassed a View

+ (void)initialize {
    static BOOL initialized = NO;
    /* Make sure code only gets executed once. */
    if (initialized == YES) return;
    initialized = YES;

    [NSApp registerServicesMenuSendTypes:[NSArray arrayWithObject:(__bridge NSString *)kUTTypeFileURL] returnTypes:nil];
}
- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType {
    if ([sendType isEqual:(__bridge NSString *)kUTTypeFileURL]) {
            return self;
    }
    return [super validRequestorForSendType:sendType returnType:returnType];
}
- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types {
    if([self.keyDelegate respondsToSelector:@selector(writeSelectionToPasteboard:types:)])
        return [self.keyDelegate writeSelectionToPasteboard:pboard types:types];
    return FALSE;
}

The last in the delegate to actually write to the pasteboard.

- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types {
    return [pboard setString:[[[self selectedFile] url] absoluteString] forType:(__bridge NSString *)kUTTypeFileURL];
}

There was a certain amount of guesswork/experimentation because the requirements do not seem to be clearly specified. This is simple implementation, because I am only interested in one type.