Type number higher than highlighted number (of possible...?)

Suppose that I have a highlighted number in a text doc. Say, "10". is there any sort of macro or command that will type in the next following number proceeding the highlighted number, which, in this case would be "11"?

Conversely, this would ideally apply to any highlighted number. Meaning, if a number 05 is highlighted, this macro would change it to a 06. Makes sense? Is this a thing?

I'm doubting that something like this is possible, but thought that I would throw it out there…

It's possible, and not overly difficult. If you didn't have to pad the leading zeros, it's really easy. But this macro handles the leading zeros, too:

increment number.kmmacros (8.3 KB)

It copies the selection to the clipboard, puts that in a variable and increments it, then reads through the initial number, adding a leading zero to the new number for each leading zero in the original, then pastes it back when done.

I wrote this pretty quickly, but it seems to work well testing with TextEdit.

-rob.

2 Likes

Wow! Thanks, Rob! Super cool!

There seems to be a slight glitch, however. While it IS populating the next number, it seems to be adding that number next to the original as opposed to replacing it. For instance, if I have 4 highlighted, it is simply adding a 5 next to the 4 (to the right of it) as opposed to replacing the original 4.

Sorry, I thought that's what you wanted it to do :). Take out these two actions:

  • Unselect text so it doesn't get replaced
  • Type the Space keystroke

It should then replace-in-place.

-rob.

2 Likes

Got it! Wow, Rob - you saved the day! Brilliant!

No problem; glad it works for you.

-rob.

There is one thing that will trip up the macro, depending on how you want the leading zeros to behave.

When you go from n digits to n+1 digits, you will still get a leading zero if there was one before. So 09 will become 010, and 099 will become 0100. If you wanted it to be 100 instead of 0100, more logic would be needed. Basically, the macro would have to check the length of the new number (with its leading zeros) and the old number, and if they aren't the same, then remove leading zeros until they are ... or something close to that.

I could probably figure this out, but won't do anything unless you say it's something you'd really like it to do.

-rob.