Test for Whether or Not Cursor Is at New Line

I keep a markdown-style task list in a plain text file. I use a macro to quick-enter new tasks without having to open up the document, basically prompting me for task name and then appending to the end of the task list file while I get on with other things.

If the last character of the file is at the end of the (previously last) task, the new task gets appended on to that existing line (which I don't want). But if the last character is at the beginning of a new line, after that last task, then the new task is inserted on that new line (which I DO want).

But if I force a new line using Append Text, then sometimes I'll end up with blank lines between tasks, which I don't want.

I want to force my new task to the beginning of a new line, while not having any blank (empty) lines between my new task and my previous task. I don't know how to test for whether I'm at the beginning of a new line, so that I can craft the appropriate Append Text.

The thread at Get Text Cursor Position seems to indicate that I can't go to CMD-DownArrow and then test for position index = 0.

Is there any other good way of figuring out, when I move to the end of a file, if my cursor is at a fresh new empty line or not?

Thank you all, please forgive my clumsy descriptions, it's my first forum post and I'm still learning.

Rennan

Here's my simple macro.

03)Text to ToDo.kmmacros (3.0 KB)
image

Hey Rennan,

Use BBEdit, and you can actually test for such things.

(The commercial version reverts to a freeware β€œlite” version after a set demo period, but the lite version is still way powerful and scriptable.)

BBEdit runs 24/7 on my systems for this sort of task and many others.

With a normal app you'd have to do something like this:

  • Set a variable to the CLIPBOARDSEED() function.
  • Ctrl-E
  • Shift-Left-Arrow.
  • Copy.
  • Test for CLIPBOARDSEED() = OLD-CLIPBOARDSEED

This will tell you if any text is behind the cursor position.

You can play with variations of that, and see if something works for you.

Or – you can get very precise with BBEdit.

-Chris

1 Like

What Chris said.

BBEdit is as indispensable to me as KM. While it's hugely useful even in its free from, it's worth every penny. I would almost feel guilty using the free version.

1 Like

Chris,
I just curious about BBEdit
"This will tell you if any text is behind the cursor position.

You can play with variations of that, and see if something works for you."

How does BBEdit work with KM to check for text behind the cursor position ?

Thank you all. I made it work with a modification of this advice:

but instead of testing CLIPBOARDSEED, I tested whether the copied character was a LineFeed. Then I realized I had to actually have the file open in a text editor, and I fixed that too.

I've never thought of using BBEdit as a utility like this. I currently have VSCode installed because I'm trying to have my text-writing environment identical to my obligate work Windows environment. I think I could do similar scripting there, but I've always had it in my mind to get the more... um... noble BBEdit.

Like @macdevign_mac, I'll like to see how BBEdit can work with KM. I'm starting to find a few examples online.

Again, thank you all. This community is so full of ingenuity, it's a happy discovery for me.

AppleScript can get a great deal of information about the selection (or character position) in BBEdit:

image

From there you can do all kinds of wondrous things...

A brief example:

tell application "BBEdit"
   tell front text window
      
      # return (get selection) --> uncomment to examine properties-of-selection.
      
      set charOffset to characterOffset of (get selection)
      
      set beforeCharOffset to (its characters (charOffset - 1) thru charOffset) as text
      set charOffsetContents to character charOffset as text
      set charactersAfterSelection to ((characters charOffset thru -1) as text)
      set charactersAfterSelectionLength to length of charactersAfterSelection

   end tell
end tell

So – rather than having to mimic user actions you can precisely script what you want to do.

Add some text to a document, and move the cursor to the end of it – then run this script:

property LF : linefeed

set itemName to text returned of (display dialog Β¬
   "Enter Markdown Item Name" buttons Β¬
   {"Cancel", "OK"} default button Β¬
   "OK" default answer "")

tell application "BBEdit"
   tell front text window
      set lastCharacter to its last character
      set charOffsetOfSelection to characterOffset of (get selection)
      if characterOffset of lastCharacter < charOffsetOfSelection then
         set selection to LF & "- [ ] " & itemName
         select insertion point after selection
      end if
   end tell
end tell

I like VSC very much and use it for a fair number of things, but BBEdit will always stay on my system when it can do things like this – and no other app can...

-Chris

Chris,
I see. So it is by using Applescript to accomplish that.
thank

1 Like