简体   繁体   English

可可:如何通过服务从Finder的上下文菜单中删除项目?

[英]Cocoa: how to remove item from Finder's context menu via service?

I created a service in order to show an item in the Finder's context menu. 我创建了一项服务,以便在Finder的上下文菜单中显示项目。

I have the following code in my application: 我的应用程序中包含以下代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [NSApp setServicesProvider:self];
    NSUpdateDynamicServices();
}

I'd like to remove the item from the context menu using manipulations on the service itself. 我想使用服务本身的操作从上下文菜单中删除该项目。

I tried to do: 我试着做:

NSUnregisterServicesProvider([[NSBundle mainBundle] bundleIdentifier]);

But it killed all the services for few seconds, and when it revived my service was in the services list in the Finder context menu. 但是它杀死了所有服务几秒钟,并且当它恢复时,我的服务在Finder上下文菜单的服务列表中。

How can I remove my NSService item from the finder's context menu? 如何从查找器的上下文菜单中删除我的NSService项目?

The documentation says : 该文件说

You should not use this function to unregister the services provided by your application. 您不应使用此功能注销应用程序提供的服务。 For your application's services, you should use the setServicesProvider: method of NSApplication, passing a nil argument. 对于应用程序的服务,应使用NSApplication的setServicesProvider:方法,并传递nil参数。

So, do what you did in applicationDidFinishLaunching: , except pass nil instead of self . 因此,执行您在applicationDidFinishLaunching:所做的操作,除了通过nil而不是self (I don't know whether you'll need to do the NSUpdateDynamicServices step.) (我不知道您是否需要执行NSUpdateDynamicServices步骤。)

If you, for example, have an uninstaller application, you can remove your menu item from Services menu in such way: 例如,如果您拥有卸载程序,则可以通过以下方式从“服务”菜单中删除菜单项:

  1. Remove the application, that provides the service (the one, in which you called [NSApp setServicesProvider:self] ) 删除提供服务的应用程序(您在其中调用[NSApp setServicesProvider:self]
  2. Call NSUpdateDynamicServices() 呼叫NSUpdateDynamicServices()

It rebuilds the database and checks the availability of applications, registered to provide a service. 它重建数据库并检查已注册以提供服务的应用程序的可用性。

If it doesn't help, dump the database in Terminal with /System/Library/CoreServices/pbs -dump_pboard and look for your service and the url, that is listed for it. 如果没有帮助,请使用/System/Library/CoreServices/pbs -dump_pboard在Terminal中转储数据库,并查找列出的服务和URL。 There could be another copy of the application elsewhere, and the clever system found it. 可能在其他地方有该应用程序的另一个副本,而聪明的系统找到了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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