我希望上下文菜单显示我的应用程序的服务。为此,我编写了以下代码。
根据这个例子:Services Implementation Guide,我在AppDelegate类中为我的服务创建了一个处理程序。
- (void)ServiceHandler:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
NSLog(@"I clicked");
}
- (void)ServiceHandler2:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
NSLog(@"I clicked");
}
- (void)ServiceHandler3:(NSPasteboard *)pboard userData:(NSString *)data error:(NSString **)error
{
NSLog(@"I clicked");
}
然后我在appDidFinishLaunching:方法中将AppDelegate设置为服务提供者,并按如下方式安装服务:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSApp setServicesProvider:self];
NSUpdateDynamicServices();
}
然后我在info.plist中创建了我的所有服务,如下所示:
<key>NSServices</key>
<array>
<dict>
<key>NSKeyEquivalent</key>
<dict>
<key>default</key>
<string>E</string>
</dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Item 1</string>
</dict>
<key>NSMessage</key>
<string>ServiceHandler</string>
<key>NSPortName</key>
<string>MyApp</string>
<key>NSRequiredContext</key>
<dict>
<key>NSApplicationIdentifier</key>
<string>com.apple.finder</string>
</dict>
<key>NSReturnTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
<key>NSSendFileTypes</key>
<array>
<string>public.item</string>
</array>
<key>NSServiceDescription</key>
<string>Description of this service</string>
</dict>
<dict>
<key>NSKeyEquivalent</key>
<dict>
<key>default</key>
<string>H</string>
</dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Item 2</string>
</dict>
<key>NSMessage</key>
<string>ServiceHandler3</string>
<key>NSPortName</key>
<string>MyApp</string>
<key>NSRequiredContext</key>
<dict>
<key>NSApplicationIdentifier</key>
<string>com.apple.finder</string>
</dict>
<key>NSReturnTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
<key>NSSendFileTypes</key>
<array>
<string>public.item</string>
</array>
<key>NSServiceDescription</key>
<string>Description of this service</string>
</dict>
<dict>
<key>NSKeyEquivalent</key>
<dict>
<key>default</key>
<string>Z</string>
</dict>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Item 3</string>
</dict>
<key>NSMessage</key>
<string>ServiceHandler2</string>
<key>NSPortName</key>
<string>MyApp</string>
<key>NSRequiredContext</key>
<dict>
<key>NSApplicationIdentifier</key>
<string>com.apple.finder</string>
</dict>
<key>NSReturnTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
<key>NSSendFileTypes</key>
<array>
<string>com.MyApp.anytype</string>
</array>
<key>NSServiceDescription</key>
<string>Description of this service</string>
</dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.item</string>
</array>
<key>UTTypeDescription</key>
<string>extension for anytype</string>
<key>UTTypeIconFile</key>
<string></string>
<key>UTTypeIdentifier</key>
<string>com.MyApp.anytype</string>
<key>UTTypeReferenceURL</key>
<string>http://anytype.MyApp.com</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>anytype</string>
</array>
</dict>
</dict>
</array>
我通过以下方式注册了我的新文件:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /path/to/MyApp
当然我也运行了这个命令:
/System/Library/CoreServices/pbs -update
/System/Library/CoreServices/pbs -flush
/System/Library/CoreServices/pbs -dump_cache
我尝试通过以下方式调试NSServices:
/Applications/TextEdit.app/Contents/MacOS/TextEdit -NSDebugServices com.MyApp
这个命令的结果没问题。
2016-03-14 13:21:16.697 TextEdit[885:6374] NSDebugServices=com.MyApp
Item 1 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.
Item 2 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.
Item 3 (com.MyApp) is enabled in the services menu and enabled in the context menu, by the standard Services policy.
所有内容都已启用,我可以在所选复选框的首选项中看到它们,但在上下文菜单中仍然无法显示此项目。我真的不知道问题出在哪里。有任何想法吗 ?
编辑:我改变了 NSFilenamesPboardType 上的每个 public.item ,现在正在运行,但只有两个。我仍然无法在上下文菜单服务中查看我自己的扩展程序。有谁知道为什么 public.item 错了?
EDIT2:解决了。我的文件扩展名标识有问题。我改变它现在它的工作原理。