Copy whole line and restore cursor position

Hi,

I needed a macro that saves the content of the current line in a text editor while restoring the cursor position before running the macro.

It basically copies the content before the cursor position and the content after the position, and then concatenates these two parts to a variable.

It works for me, but I am not satisfied and I am looking forward to a better solution.

Copy whole line and restore cursor position afterwards.kmmacros (4.3 KB)

1 Like

Hey Ben,

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.


Best Regards,
Chris

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.

Yes.

-ccs

1 Like

Hey Ben,

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

-Chris

2 Likes