gpt4 book ai didi

objective-c - 在服务菜单中设置自定义 KeyEquivalent

转载 作者:行者123 更新时间:2023-12-03 17:03:29 24 4
gpt4 key购买 nike

OmniFocus 有一个 Cocoa 服务,允许您根据所选项目创建任务。

它有一个首选项,允许您设置触发服务的键盘快捷键。这不仅仅是一个全局热键,它是一个显示在菜单中的真正的服务。

您可以使用键盘快捷键进行几乎任何组合,包括与 ⌥ 和 ^ 的组合。此功能未记录 - docs似乎是说 KeyEquivalents 必须是 ⌘+[⇧]+someKey .

一旦设置完毕,我会观察到三件事:

  1. OmniFocus Info.plist 文件不包含列出的 KeyEquivalent。这并不奇怪,因为该文件是只读的。
  2. pbs -dump_pboard 实用程序列出 NSKeyEquivalent = {};为了服务。
  3. 使用 NSDebugServices 列出了这一有趣的行,该行在大多数调试 session 中都不会显示(显然,对于键盘快捷键 ⌃⌥⌘M ): OmniFocus: Send to Inbox (com.omnigroup.OmniFocus) has a custom key equivalent: <NSKeyboardShortcut: 0x7fb18a0d18f0 (⌃⌥⌘M)>.

所以我的问题是双重的,我怀疑它们是相关的:

  1. 如何动态更改服务的 KeyEquivalent?
  2. 如何将 KeyEquivalent 设置为包含 ⌃ 和 ⌥ 的组合

谢谢!

最佳答案

想通了。基本过程描述如下:Register NSService with Command Alt NSKeyEquivalent

代码是这样的:

//Bundle identifier from Info.plist
NSString* bundleIdentifier = @"com.whatever.MyApp";
//Services -> Menu -> Menu item title from Info.plist
NSString* appServiceName = @"Launch My Service";
//Services -> Instance method name from Info.plist
NSString* methodNameForService = @"myServiceMethod";

//The key equivalent
NSString* keyEquivalent = @"@~r";

CFStringRef serviceStatusName = (CFStringRef)[NSString stringWithFormat:@"%@ - %@ - %@", bundleIdentifier, appServiceName, 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",
keyEquivalent, @"key_equivalent", nil];
[pbsAllServicesNew setObject:serviceStatus forKey:(NSString*)serviceStatusName];
CFPreferencesSetAppValue (
serviceStatusRoot,
(CFPropertyListRef) pbsAllServicesNew,
CFSTR("pbs"));
Boolean result = CFPreferencesAppSynchronize(CFSTR("pbs"));
if (result) {
NSUpdateDynamicServices();
NSLog(@"successfully installed our alt-command-r service");
} else {
NSLog(@"couldn't install our alt-command-r service");
}

如果代码成功,您可以在~/Library/Preferences/pbs.plist中查看

您应该看到类似以下内容:

NSServicesStatus = {
"com.whatever.MyApp - Launch My Service - myServiceMethod" = {
enabled_context_menu = :true;
enabled_services_menu = :true;
key_equivalent = "@~r";
};

关于objective-c - 在服务菜单中设置自定义 KeyEquivalent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18776369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com