Increment Variable Maintain Padding

Is there a way to increment a variable with a calculation and maintain the original padding? I have a variable that's either going to be 001 or 01, and when I increment it, I'd like it to maintain that formatting. So far, all I've been able to do is set everything to a fixed padding with the Format option in the calculation action.

I can use Filter to get the character count, but I don't think I can use that with the calculation format, unless I'm missing something.

I can probably split out all the zeros and append them back to the variable after the calculation, but I figured there was probably a simpler way to do this that I'm missing.

Because of how KM handles variables and text (a good thing overall, in my mind), I don't think you can really maintain the leading zeros as you go. I always just do my calculations, then use a second variable to display the formatted results to whatever level I need to show them.

There's probably an easier/better way, though; I'm just not aware of it :).

-rob.

1 Like

Your solution suggestion, if modified a bit, would do the job perfectly, but I'm fairly confident someone will come up with a simple solution.

I'm working on a delightful, novel solution, but it may take longer than usual because I'm watching the US election results and making breakfast at the same time.

Here's what I'm doing for now. It's a little silly but it works.

Here's a simple way to do it, using some JavaScript:

I have a solution. I enjoyed the challenge. I made it into a subroutine macro. Basically, what I did was add up the digits "manually in my macro." If there were leading zeros, I kept the zeros. I'm sure there are other solutions, which I'm still pondering, because I like this puzzle.

Add2Numbers Macro (v11.0.3)

Add2Numbers.kmmacros (14 KB)

Ideally, you could do this:

image

But unfortunately, the Set Variable to Calculation action does not accept token text in the format field (changed for the next version).

So you can do that, but you need to generate the XML yourself and then execute it:

image

Increment Number.kmactions (2.7 KB)

1 Like

Or with .padStart:

Padded Integer Increment.kmmacros (2.1 KB)


Expand disclosure triangle to view JS source
const s = kmvar.local_N;

return isNaN(s)
    ? s
    : `${1 + Number(s)}`.padStart(s.length, "0");

I have a new way to solve this problem, without shell code, KM loops or AppleScript or Javascript!

I tested this on only one data point, not hundreds, as I did for my first solution.

I forgot to mention that for actions #2 and #4 above, you have to turn off token processing ("process nothing") in the cogwheel.

OK, here is a pure Keyboard Maestro version using the CalculateFormat token and the Process Tokens Filter action.

image

So convert the number to a format by replacing every digit with 0, Then create a CalculateFormat token with the format inserted (note the doubled percentage characters all through that text which are converted to single percent characters), and then process the tokens a final time to calculate the result and format it.

3 Likes

I can't be envious when The Architect does better than me.

3 Likes

Very nice. I have instantly adapted it into a subroutine after replacing the global variables with local variables :slight_smile:

1 Like

Wise. I tend to debug things with global variables because it is a bit easier if you need to look at their values.

3 Likes

When in doubt, apply brute force :wink:

Use as many 0s as seems reasonable... The "If" is there for when the padding is absent for some reason.

1 Like

Thanks, Peter, this works great! Adding token support to the format field will be a nice addition, but it's cool to see all the alternate solutions, I learned some new stuff here.

1 Like

Hi guys, i’m working on a project that need to create increment line number everytime i hit a hot key. Let say i bind cmd-L for a hot key. when i press cmd-L it writes down line0001, the second time i press cmd-L it write down line0002 and so on up to line 0999. I’ve been struggling If and else for 12 hours now and couldn’t make it work :frowning: Any help is appreciated

Here is my first attempt to implement a solution for you. Bear in mind that I'm only 50% sure I have understood your question. There are several things about your problem that just aren't clear to me. You will probably have to explain to me what's wrong with this macro, and then I will fix it. What this macro does is let you press a hotkey which will insert a new string, such as "line0001" in the place where the text cursor is, and if there is any text selected, it will replace the selected text with the next sequence number. I'm not sure that this is exactly what you want, but it's how I interpreted your request.

Increment and Replace Selected Text Macro (v11.0.4)

Increment and Replace Selected Text.kmmacros (3.5 KB)

1 Like

Looks good, but Insert Text by Pasting action would replace the last two actions and be clearer.

1 Like

Your method is simpler, but I'm not sure the word clearer works here, because your method hides the fact that the system clipboard is being changed. Some users may benefit from seeing that the clipboard is being changed.

1 Like