I asked what I thought would be a simple question on cocoa-dev:
I've added a system service to my app. I'd like it to appear in the "Search" group of the Services menu (where "Look Up in Dictionary" and "Search With Google" are) rather than the "Text" group.
I would have guessed there is some key-value pair I could set in the NSServices dictionary in Info.plist, but as far as I can tell my NSServices looks just like Safari's.
Here's what I meant by the "Search" group (actually I meant "Searching"):
I waited a few days and no one replied, so I poked around some more. The short answer is that you add an entry to the NSRequiredContext dictionary using the undocumented key NSServiceCategory:
<key>NSRequiredContext</key> <dict> <key>NSServiceCategory</key> <string>Searching</string> </dict> |
You can't create your own category. If you change "Searching" to "My Great Services", the Services menu doesn't add a "My Great Services" category containing your service. Instead, your service goes back into the catch-all "Text" category. There are at least eight names you can use:
- Pictures
- Internet
- Files and Folders
- Messaging
- Searching
- Text
- Development
- General
I got this list from System Preferences, in the Keyboard > Keyboard Shortcuts > Services pane:
I had looked in the Info.plist files of Safari and Dictionary, but they don't use NSServiceCategory or I'd have found this answer a lot sooner. Nor does Mail, which appears in Services under "Messaging". Besides using Info.plist, Cocoa must have a secret registry somewhere that maps apps to Services categories.
I only discovered NSServiceCategory, which finally gave me something good to Google, when I thought to look inside Twitter and Tweetings, two apps that also manage not to be in the "Text" category. Instead of a category name, those apps use the UTI "public.item", which puts their services under "Files and Folders". That seems odd to me, but maybe that's their way of breaking out of the big undifferentiated "Text" category.
You can find other apps that use NSServiceCategory by running this command and grepping the results:
/System/Library/CoreServices/pbs -dump_pboard |
Using NSServiceCategory was unofficially advised by Peter Ammon back in 2009. I've filed Radar #11945677 asking Apple to make it official in the Services Implementation Guide so that users can see their services properly categorized.
UPDATE: NSServiceCategory isn't the only setting that affects what category the service goes into. I noticed the Versions app doesn't have a NSServiceCategory setting, yet it appears in the "Internet" category, because of its NSTextContent setting:
<key>NSRequiredContext</key> <dict> <key>NSTextContent</key> <string>URL</string> </dict> |
I updated my Radar to ask that this be documented as well.