如何通过辅助功能 API 获取当前所选文本的全局屏幕坐标。
我需要帮助来了解字典应用程序如何在任何应用程序上按 CMD+CTRL+D 时显示所选文本的以下弹出对话框。我想实施 我的可可应用程序具有相同的功能,我的应用程序将在后台运行,并显示有关所选文本的一些热键的建议。
我已经实现了热键捕获,我只需要有代码来获取矩形区域在屏幕上选择文本,这样我就可以像字典应用程序一样显示对话框。
谢谢
I need help to find out, how Dictionary app showing following popup dialog for selected text on pressing CMD+CTRL+D on any application. I want to implement
the same kind of functionality for my cocoa app, where my app will run in background and showing suggestions on some hot key press for the selected text.
I have already implemented hot key capturing, i just need to have code to get the rectangle area of selected text on screen, so i can show the dialog like dictionary app.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为此使用辅助功能 API。确保选中“启用辅助设备访问”设置(在系统偏好设置/通用访问中)。
以下代码片段将确定大多数应用程序中所选文本的边界(以屏幕坐标表示)。不幸的是,它在 Mail 和 Safari 中不起作用,因为它们使用私有辅助功能属性。也许也可以让它在那里工作,但它需要更多的工作,并且可能需要私有 API 调用。
You can use the accessibility APIs for that. Make sure that the "Enable access for assistive devices" setting is checked (in System Preferences / Universal Access).
The following code snippet will determine the bounds (in screen coordinates) of the selected text in most applications. Unfortunately, it doesn't work in Mail and Safari, because they use private accessibility attributes. It's probably possible to get it to work there as well, but it requires more work and possibly private API calls.
在这里你可以用 Swift 获取 @omz 答案
Here you go for @omz answer in Swift
您正在寻找的是服务。
通过服务,您的应用程序甚至不必运行或捕获全局热键。
例如,您描述的词典应用程序的功能实际上是一项服务,可以在“服务”菜单中观察到。
Apple 的 服务实现指南可能是有关服务的最佳信息。
What you're looking for is a Service.
With services, your app doesn't even have to be running or capture global hotkeys.
For example, the functionality of the dictionary app you described is actually a service, observable in the Services menu.
Apple's Service Implementation Guide is probably the best info on services out there.