如何从另一个应用程序获取选定的文本? [英] How to obtain the selected text from another application?

查看:301
本文介绍了如何从另一个应用程序获取选定的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将很快在一个应用程序上工作,该应用程序需要在最前面的应用程序窗口中获取当前选定的文本,包括Safari,Pages,TextEdit,Word等,并对该文本执行某些操作。

I will soon be working on an application which needs to get the currently selected text in the frontmost application window, be it Safari, Pages, TextEdit, Word, etc., and do something with that text.

我的目标是找到一个解决方案,尽可能多的应用程序。到目前为止,我想到使用 AppleScript ,但这将限制可以与我的服务一起使用的应用程序的数量。至少这些常见的应用程序必须支持:Safari,Firefox(没有AppleScript?),Word,Pages,Excel,TextEdit,...

My goal is to find a solution that works with as much applications as possible. So far I thought about using AppleScript, but that would limit the amount of applications which could be used with my service. At least these common applications must be supported: Safari, Firefox (no AppleScript?), Word, Pages, Excel, TextEdit, ...

剪贴板的内容放在一个临时变量中,然后模拟文本复制操作(Cmd-C),获取文本,然后将原始内容重新插入。这可能会突出显示编辑菜单项操作模拟,似乎有点怪我。 IMO这个解决方案似乎不够好的商业产品。

I also thought about keeping the clipboard's content in a temporary variable then simulating a text copy operation (Cmd-C), getting the text and then put the original content back in. This would probably highlight the Edit menu item when the copy operation is simulated and seems a bit hacky to me. IMO this solution doesn't seem good enough for a commercial product.

我也希望得到更多的选择(ie:页面的完整内容在Safari或Word等)在未来添加一些额外的功能。

I am also looking to get more than the selection (i.e: the complete contents of the page in Safari or Word, etc.) to add some additional features in the future.

有关如何实现此行为的任何想法/细节?

Any ideas/details on how to implement this behavior?

提前感谢任何提示!

注意:我需要支持至少10.4及以上,但最好还是早于10.4。

N.B: I need to support at least 10.4 and up, but ideally older than 10.4 too.

更新:

我选择的解决方案:使用责任链设计模式

The solution I've opted for: Using the "Chain of Responsibility" design pattern (GOF) to combine 3 different input methods (Pasteboard, AppleScript and Accessibility), using the best available input source automatically.

请注意,当使用NSAppleScript的executeAndReturnError:方法时,它会返回一个NSAppleEventDescriptor(让我们来看一下这个方法)例如描述符实例),对于[描述符stringValue]方法返回的东西,在你的AppleScript中必须使用return someStringOUTSIDE的告诉块否则不会返回任何东西。

Note that when using NSAppleScript's executeAndReturnError: method which returns an NSAppleEventDescriptor (let's say a "descriptor" instance), for the [descriptor stringValue] method to return something, in your AppleScript you must use "return someString" OUTSIDE of a "tell" block else nothing will be returned.

推荐答案

辅助功能将有效,但前提是辅助设备的访问权限已启用。

Accessibility will work, but only if access for assistive devices is on.

以获取当前应用程序,然后获取其关注的UI元素,然后获取其所选文本范围及其值(整个文本)和所选文本范围。

You'll need to get the current application, then get its focused UI element, then get its selected text ranges and its value (whole text) and selected text ranges. You could just get its selected text, but that would either concatenate or ignore multiple selections.

准备好任何步骤失败:应用程序可能没有任何窗口则可能没有具有焦点的UI元素,所关注的UI元素可以不具有文本,并且所关注的UI元素可以仅具有空的所选文本范围。

Be prepared for any of those steps to fail: The app may not have any windows up, there may be no UI element with focus, the focused UI element may have no text, and the focused UI element may have only an empty selected text range.

这篇关于如何从另一个应用程序获取选定的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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