Registering NSService with Alt NSKeyEquivalent Team
my application provides a global service. I would like to install a service using the command-alt-key combination. what I am doing now is not very error prone and really hard to debug as I don't really see what's going on:
inside Info.plist:
<key>NSServices</key>
<array>
<dict>
<key>NSSendTypes</key>
<array>
<string></string>
</array>
<key>NSReturnTypes</key>
<array>
<string></string>
</array>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Go To Window in ${PRODUCT_NAME}</string>
</dict>
<key>NSMessage</key>
<string>bringZFToForegroundZoomOut</string>
<key>NSPortName</key>
<string>com.raskinformac.${PRODUCT_NAME:identifier}</string>
</dict>
</array>
and in the code:
CFStringRef serviceStatusName = (CFStringRef)[NSString stringWithFormat:@"%@ - %@ - %@", appIdentifier, appName, methodNameForService];
CFStringRef serviceStatusRoot = CFSTR("NSServicesStatus");
CFPropertyListRef pbsAllServices = (CFPropertyListRef) CFMakeCollectable ( CFPreferencesCopyAppValue(serviceStatusRoot, CFSTR("pbs")) );
// the user did not configure any custom services
BOOL otherServicesDefined = pbsAllServices != NULL;
BOOL ourServiceDefined = NO;
if ( otherServicesDefined ) {
ourServiceDefined = NULL != CFDictionaryGetValue((CFDictionaryRef)pbsAllServices, serviceStatusName);
}
NSUpdateDynamicServices();
NSMutableDictionary *pbsAllServicesNew = nil;
if (otherServicesDefined) {
pbsAllServicesNew = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary*)pbsAllServices];
} else {
pbsAllServicesNew = [NSMutableDictionary dictionaryWithCapacity:1];
}
NSDictionary *serviceStatus = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, @"enabled_context_menu",
(id)kCFBooleanTrue, @"enabled_services_menu",
@"@~r", @"key_equivalent", nil];
[pbsAllServicesNew setObject:serviceStatus forKey:(NSString*)serviceStatusName];
CFPreferencesSetAppValue (
serviceStatusRoot,
(CFPropertyListRef) pbsAllServicesNew,
CFSTR("pbs"));
Boolean result = CFPreferencesAppSynchronize(CFSTR("pbs"));
if (result) {
NSUpdateDynamicServices();
JLog(@"successfully installed our alt-command-R service");
} else {
ALog(@"couldn't install our alt-command-R service");
}
and change the service:
// once installed, its a bit tricky to set new ones (works only in RELEASE somehow?)
// quit finder
// open "~/Library/Preferences/pbs.plist" and remove ch.ana.Zoom - Reveal Window in Zoom - bringZFToForegroundZoomOut inside NSServicesStatus and save
// start app
// /System/Library/CoreServices/pbs -dump_pboard (to see if it hat actually done what we wanted, might be empty)
// /System/Library/CoreServices/pbs (to add the new services)
// /System/Library/CoreServices/pbs -dump_pboard (see new linking)
// and then /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder -NSDebugServices MY.APP.IDENTIFIER to restart finder
so my question is, is there an easier way to enable the service with the cmd-option key? if so, I would happily implement it in my software.
a source to share
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Go To Window in ${PRODUCT_NAME}</string>
</dict>
and the middle component serviceStatusName should be exactly the same. I changed the code in the question.
NSString *appServiceName = @"Go To Window in ${PRODUCT_NAME}"
CFStringRef serviceStatusName = (CFStringRef)[NSString stringWithFormat:@"%@ - %@ - %@", appIdentifier, appServiceName, methodNameForService];
a source to share
Perhaps you try to simply set your equivalent string with the "~" character as the value for NSKeyEquivalent
in the service dictionary. The Service Implementation Guide states that it should be a single character, but I find it worth trying the full key equivalent string.
Remember, like NSMenuItem, it must be wrapped in a dictionary:
<key>NSKeyEquivalent</key>
<dict>
<key>default</key>
<string>~r</string>
</dict>
(I've omitted the '@; if it doesn't work without it, you can try it with it.)
If that works, I suggest filing a bug in the documentation.
Perhaps Apple wants all services to use either ⌘-something or ⇧⌘-something the equivalent of a key, leaving other modifiers for menu items and global hotkeys. So even though I would try the above, I wouldn't be too surprised if it doesn't work.
a source to share