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.

2 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