There is no great way of doing this, because OSX has no general API for managing text with precision.
So, you’re left with brute-forcing the UI.
An alternative to your method:
Type the ⌥⇧I Keystroke
Type the ⌘Left Arrow Keystroke
Type the ⇧⌘Right Arrow Keystroke
Type the ⌘X Keystroke
Search and Replace Clipboard With String Matching (ignoring case)
Search for “ˆ”
Replace with “\n”
Type the ⌘V Keystroke
Type the Up Arrow Keystroke
Type the ⌃E Keystroke
Type the Forward Delete Keystroke
* Option-Shift-i to get the mini-caret character: “ˆ”
BTW. You don’t mention what app (or apps) you’re working it. Sometimes that makes all the difference in what’s possible.
Thanks, Chris. I presume the mini caret is just a rarely used character here working as a marker? Because Opt-Shift-i produces a U with a caret on top with my QWERTZ-keyboard.
I am using Textmate, but will probably switch to BBEdit.
BBEdit is very scriptable, so it's easy to get the text of the line where the cursor is:
tell application "BBEdit"
tell front text window
set _win to it
tell selection
if (get its length) = 0 then
set lineText to contents of line (get startLine) of _win
end if
end tell
end tell
end tell