How to obtain the selected text from another application?

前端 未结 2 1708
粉色の甜心
粉色の甜心 2020-12-10 08:25

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 d

2条回答
  •  伪装坚强ぢ
    2020-12-10 09:04

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

    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.

    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.

提交回复
热议问题