如何右键单击文本和选择服务工作? [英] How does right-clicking text and selecting a service work?

查看:194
本文介绍了如何右键单击文本和选择服务工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[1]当我选择并右键点击任何应用程序中的任何文本,我得到一个服务菜单,其中我可以点击调用任何服务,如使用Google搜索



点击后,会自动发送一个 NSPasteboard



[2]第三方应用程序可以使用 BOOL NSPerformService(NSString * itemName,NSPasteboard * pboard)函数来编程调用任何服务。



这里的应用程序必须设置 NSPasteboard ,发送到服务。






有没有办法以编程方式调用任何服务, c $ c> NSPasteboard 对象( NSPasteboard 对象应该自动包含所选文本,如[1])?



如果 NSPasteboard 对象包含由OS X在[1]中发送到服务的选定文本?是否有一些私有API?

解决方案

这在服务实施指南,特别是使用服务一章。



首先,用户打开服务菜单,Cocoa会在响应链中的对象上调用 -validRequestorForSendType:returnType:,以确定哪些对象可以处理发送类型和返回类型的哪些组合,如果有的话,基于其当前状态(例如,当前选择)。

当用户选择服务菜单项时,Cocoa会创建一个粘贴板并调用 -writeSelectionToPasteboard:types: -validRequestorForSendType:returnType:返回的请求者对象上的。该对象应该将选择数据放在所提供的粘贴板上,不管它支持哪种类型。



Cocoa然后将粘贴板传递到系统。系统在服务提供者进程中与Cocoa通信。 Cocoa在该进程中调用服务提供程序对象的 - < messageName>:userData:error:,其中< messageName> 来自该应用程序的Info.plist文件中的服务描述。服务提供程序对象是使用 NSRegisterServicesProvider()向Cocoa注册的应用程序。



服务提供程序处理输入



提供程序完成后,Cocoa在原始应用程序中调用 -readSelectionFromPasteboard:。同样,该方法提供了粘贴板。


[1] When I select and right-click any text in any app, I get a Services menu where I can click to invoke any service like Search With Google.

After clicking, a NSPasteboard object is automatically sent to the service containing the selected text.

[2] 3rd party apps can use the BOOL NSPerformService ( NSString *itemName, NSPasteboard *pboard ) function to programatically invoke any service.

Here the app has to set the NSPasteboard object, which is sent to the service.


Is there a way to programatically invoke any service, but without setting the NSPasteboard object (the NSPasteboard object should automatically contain the selected text like in [1]) ?

How is the NSPasteboard object containing selected text sent to the service by OS X in [1]? Is there some private API?

解决方案

This is documented in the Services Implementation Guide, in particular the Using Services chapter.

First, when the user opens the Services menu, Cocoa calls -validRequestorForSendType:returnType: on the objects in the responder chain to determine which combinations of send type and return type can be handled by which object, if any, based on its current state (e.g. the current selection). That controls which Services menu items are enabled.

When the user selects a service menu item, Cocoa creates a pasteboard and calls -writeSelectionToPasteboard:types: on the requestor object returned by -validRequestorForSendType:returnType:. That object should put the selection data on the provided pasteboard in whichever of the types it supports.

Cocoa then passes the pasteboard off to the system. The system communicates with Cocoa in the service provider process. Cocoa invokes -<messageName>:userData:error: on the service provider object in that process, where <messageName> comes from the description of the service in that app's Info.plist file. The service provider object is whatever the app registered with Cocoa using NSRegisterServicesProvider().

The service provider processes the input data, if any, from the pasteboard and stores the return data, if any, to it.

After the provider is done, Cocoa in the original app calls -readSelectionFromPasteboard: on the requestor object. Again, the pasteboard is provided to that method.

这篇关于如何右键单击文本和选择服务工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆