Selecting the Number Under the Cursor

I've successfully selected - with two keystrokes - the word under the cursor in an entry field. For an integer this is all I need.

But my use case is font point sizes. Which I expect to handle complete with the fractional part. Word selection doesn't do it - as the bit before the decimal point is not the same word as the bit after.

So any ideas how - in Keyboard Maestro - to select a decimal number properly? I have to handle both the integer and non integer cases.

For context, the idea is to take the number under the cursor and paste it back incremented by 0.5. Likewise decrement - in a companion macro. Both macros would share the "select number under cursor" macro.

And this would be driven by a knob in a StreamDeck Plus.

What app are you working in?

Your average Mac app or macOS field will select the entire number on both sides of a decimal point.

BBEdit is my prime use case. And I say "prime" because I'd like it to work anywhere I have a text field.

Consider the string <!-- md2pptx compactTables: 16.5 --> . I'd like to be able to twiddle a knob with the cursor at that number and have that go to 16 or 17. Twiddling knobs to kick off a Keyboard Maestro macro (in fact two) is easy. It's the number selection to copy, increment, paste that's the problem.

In BBEdit I'd probably use an AppleScripted regex search.

Here's a script you can adapt. It should work for your specific purpose but is designed to be more general.

You can start off with the cursor within the number to be selected or on either side of it.

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# Task: Select Word
# dMod: 2014/09/16 16:59
--------------------------------------------------------

tell application "BBEdit"
   
   tell front text window's text
      set startSel to characterOffset of found object of (find "\\s|^" options {search mode:grep, backwards:true})
      set endSel to characterOffset of found object of (find "\\s|$" options {search mode:grep, backwards:false})
      
      if (get contents of character startSel) is in {" ", " ", "  ", return} then
         set startSel to startSel + 1
      end if
      
      select (characters startSel thru (endSel - 1))
   end tell
end tell

--------------------------------------------------------

Thank you. I can live with it just being BBEdit - as I rarely use anything else.

What if there were two or more numbers on the line? My guess is this will get the right one but I'm not sure.

Actually it worked very nicely. So thanks!

The script doesn't depend upon numbers – it depends upon boundary space.

The cursor can be within the number (or word) or adjacent to it on either the left or the right.

It's not working properly for words/numbers at the beginning of a line (at least in BBEdit 14.1.2), because Bare Bones broke beginning-of-line when finding backwards – and I think that's the only shortcoming of the script.

This bug may or may not have been fixed in later versions of BBEdit. I'm stuck with this one, because I'm still using Mojave...

1 Like

Right. Reviewing it I can see nothing about numeric digits. In my use case - which I won't be supporting other users with - I can be mindful of the fact it's a space-delimited block of text, not a number.

I'll check later today if the issue is fixed in the latest Keyboard Maestro.

But, again, thanks for the help. I have a nice solution. I'm wondering if it's worth writing up.

You can play around with AppleScript and the regex to have more precise control, but things can get pretty complicated pretty fast.

1 Like

So I tested on 10.2 and the problem persists. In fact the problem is also there at the end of the line. To counteract the latter my actions move the cursor 1 character left after incrementing or decrementing the value. If I position my cursor a little carefully I can avoid both these problems.

Another approach I thought of was to actually test individual characters to the left and right of the cursor. But I don't know how to get the character to the left or right of the cursor - other than doing single character selection with the clipboard.

In any case I have a workable solution for a number anywhere on the line so long as I don't have the cursor at either end. A small niggle but one where I don't feel it's good enough to write up.

BUT the idea of using a Stream Deck (or Xencelabs or home-grown) dial to adjust a text value seems a reasonably useful one. I'll try to find a different use case. Perhaps a text case adjuster.

To be clear -- the problem is with BBEdit, not KM.

@ccstone -- does changing the "find backwards" pattern to "\\s|^." fix it for you, Chris?

It seems to for me, but I'm on an older version here so may not be seeing the exact same issue in the first place!

2 Likes

Good thought.

That helps, but there're still problems when at the start of a document, line or at the end of the document.

What I'm going to suggest is to use BBEdit's select-word menu item which will always do the right thing according to BBEdit's rules for what a word is.

Then check to see what your selection's boundaries are:

tell application "BBEdit"
   tell front text window
      set characterBefore to (character before selection) as text
      set characterAfter to (character after selection) as text
   end tell
end tell

From there make a decision on how to handle the the rest of the selection.

BOF and EOF will have to be handled, as they will throw out-of-range errors, but I think the only other thing Martin needs to handle is the decimal character.

1 Like