简体   繁体   中英

How do I automatically activate an item in the OS X Services Menu

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?

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 .

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>

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM