Apple's Services Implementation Guide - Sample Code Not Working
-
02-03-2021 - |
Question
Whenever I'm like surfing on the web and I highlight a bunch of text, I see a "New TextWrangler Document with Selection" item on the contextual menu.
How do I replicate this functionality with my app so that whenever a user selects text in any other application, he can see a "New (My App) Document with Selection" item in the contextual menu and upon clicking on it, it feeds the selected text into my app?
Update
To try to answer my own question, I followed the sample code here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/SysServices/Articles/providing.html
which implements a simple rot13 encryption service on highlighted text. but it ain't working.
http://i42.tinypic.com/2z4co0o.jpg
Although my "Encrypt Text" appears in the system preferences menu like "New TextWrangler Document with Selection" as shown above, it doesnt actually appear in an application's services menu or right-click contextual menu when text is selected.
Can someone tell me why the sample code in apple's documentation is not working
@PeterHosey here it is, with NSRequiredContext too:
http://i39.tinypic.com/51d06o.jpg
my app name is called "simpleEncrypt" and the message is exactly the same as the one copied from apple's services implementation guide.
- (void)simpleEncrypt:(NSPasteboard *)pboard
userData:(NSString *)userData error:(NSString **)error;
La solution
In the Info.plist for my Translate Text application, I'm using NSStringPboardType
as the send type for each of its services.
The system-declared UTIs list says that the modern equivalent to that is public.utf8-plain-text
, so try changing your service's send and return types to that.
If it works, it'd probably be a good idea to list all the plain-text variants. Those are listed there in the Uniform Type Identifiers Reference; they're the types that conform to public.plain-text
.
Autres conseils
You need to include an NSRequiredContext
dictionary in your service dictionary that describes when the service is appropriate to enable. Until you do, it is disabled in all contexts by default. (I'm not sure why it still doesn't show up even after you enabled it, but this is a required first step.)