Manipulating text fields

Before I start thinking about some things to do, In terms of manipulating text inside a message window or a text field, I first need to copy the text into a variable, clipboard, file, etc, correct? I cannot directly access the text without using ⌘-c or AppleScript or perhaps an image match followed by keypresses?¹

And there is no condition for “There’s an insertion point active” that I have missed, correct?

¹ (I saw someone using an image match for ‘http’ to find a URL, too then click and drag and ⌘-C to get the URL with KM actions.)

1 Like

Apple Mail and a new outgoing message? A reply? Something else?

A new outgoing message will always have the insertion point in the "To" field -- you can tab-key into the message body (4-6 Tabs, depending on BCC field and signature status). A reply will always have the insertion point in the message body, in a consistent position (mine is probably different to yours because I use the QuoteFix plugin).

Or you can use AppleScript -- again, how will depend on what you are trying to do. For a new outgoing message:

set msgContent to "Hello World!"

tell application "Mail"
	activate
	set newMsg to make outgoing message with properties {sender:"email@example.com", subject:"Test Message", visible:true, content:msgContent}
	tell newMsg
		make new recipient at end of to recipients with properties {address:("test@example.com")}
	end tell
end tell

In general, yes, that is correct.

There is no general way to tell:

  1. whether or not their is currently a text insertion point active
  2. the text contents of the currently selected text field (or even if there is one)
  3. the selection or position of the insertion point in the current text field (or even if there is one)
  4. the location on the screen of the currently text insertion point (or even if there is one)

You can, in some cases, use AppleScript, or the Accessibility API (via AppleScript) to determine some things in some cases, but it does not work in general. You could, in theory record the screen looking for the flashing cursor, but that is not really a practical solution, and would not work if there was a selection.

It would be great to be able to answer all of those questions but macOS simply has no way to answer them reliably in the general case.