views:

51

answers:

2

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?

+1  A: 

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");
Yuji
+1  A: 

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.

Peter Hosey
I tried this but it still doesn't automatically activate them. Maybe I'm doing something wrong.
Levi Nunnink
Levi Nunnink: Please edit your question to include your updated service dictionary with NSRequiredContext included.
Peter Hosey