3

I need to have a service I created enabled by default in the services menu.

I've created a service for my OS X app (running on Snow Leopard). I've configured the Info.plist like so:

<key>NSServices</key>
<array>
    <dict>
        <key>NSSendTypes</key>
        <array>
            <string>NSStringPboardType</string>
        </array>
        <key>NSMessage</key>
        <string>dropService</string>
        <key>NSMenuItem</key>
        <dict>
            <key>default</key>
            <string>Drop Service</string>
        </dict>
        <key>NSPortName</key>
        <string>MyApp</string>
    </dict>
</array>

So far so good, I follow the other steps outlined in: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/SysServices/introduction.html and everything seems to be working correctly.

I ran the command:

/System/Library/CoreServices/pbs

And now my service shows up under "System Preferences -> Keyboard -> Keyboard Shortcuts -> Services". So the system knows about it. But it won't show up in the services menu unless I manually activate it.

When I try to debug the service, I get this message:

/Applications/TextEdit.app/Contents/MacOS/TextEdit -NSDebugServices com.myapp.MyApp
Drop Service (com.myapp.MyApp) is disabled in the services menu and disabled in the context menu, by the standard Services policy.

I have no idea what the "standard Services policy" is and I can't find any reference to this error on the Apple developer site.

I would think that automatically enabling service items is impossible but I've confirmed that certain apps do it (like Things from Cultured Code) so I know it can be done.

Any ideas?

3 Answers 3

12

The correct way to get your service to be enabled by default without hacking any system plists is to edit your own plist. Specifically, add the NSRequiredContext key in each service dictionary. This information comes from this mailing list post (taken from this answer on a similar question).

As for the value you need to provide for that key, the documentation will fill you in.

3
  • I tried this but it still doesn't automatically activate them. Maybe I'm doing something wrong. Oct 7, 2010 at 18:05
  • Levi Nunnink: Please edit your question to include your updated service dictionary with NSRequiredContext included. Oct 7, 2010 at 21:30
  • 1
    Ok, the NSRequiredContxt does work. pbs was indexing an older build on my machine. Jul 20, 2012 at 20:22
11

Just add a NSRequiredContext key with a blank dictionary as its object in your Info.plist. Here's how it should look:

<key>NSRequiredContext</key>
<dict>
</dict>
3
  • 1
    Or a non-blank dictionary, if you want to specify a context to require. Jul 21, 2012 at 4:40
  • 1
    I couldn't figure out what to add (none of the restrictions documented really applied). It doesn't make much sense to me that a blank dictionary would affect this behavior, but it certainly does. Thanks for the answer. Apr 5, 2013 at 0:51
  • 1
    It's important to note that this entry needs to be added into the Services array's dictionaries, not at the root of the Info.plist (Peter Hosey point that out more clearly). Aug 3, 2015 at 15:09
2

I don't know any public API to do that, but I found a way to work around it. The list of all the activated services is kept in

~/Library/Preferences/pbs.plist

You just need to add an entry and reset pbs. I'm using the following code snippet:

NSString*pbsPlistPath=[@"~/Library/Preferences/pbs.plist" stringByExpandingTildeInPath];
NSMutableDictionary*dict=[NSPropertyListSerialization 
                                  propertyListFromData:[NSData dataWithContentsOfFile:pbsPlistPath]
                                    mutabilityOption:NSPropertyListMutableContainers
                                                format:NULL
                                      errorDescription:NULL];
NSMutableDictionary*status=[dict objectForKey:@"NSServicesStatus"];
if(!status){
    status=[NSMutableDictionary dictionary];
    [dict setObject:status forKey:@"NSServicesStatus"];
}
if(status){
    NSMutableDictionary*m=[NSMutableDictionary dictionary];
    [m setObject:[NSNumber numberWithBool:YES] forKey:@"enabled_context_menu"];
    [m setObject:[NSNumber numberWithBool:YES] forKey:@"enabled_services_menu"];
    [status setObject:m
               forKey:@"com.my.app - Drop Service - dropService"];
}
NSData* data=[NSPropertyListSerialization dataWithPropertyList:dict
                                                        format:NSPropertyListBinaryFormat_v1_0
                                                       options:0
                                                         error:NULL];
[data writeToFile:pbsPlistPath atomically:YES];
system("/System/Library/CoreServices/pbs -flush");
system("/System/Library/CoreServices/pbs -flush_userdefs");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.