Increment xx.xx or xx.xx.xx for Renaming

I have a macro that uses javascript to try to increment a variable and add it to a copied name. Another macro sets the variable "FMScriptNameStart" So I have a bunch of scripts in FileMaker (not important) that I want to prefix correctly. I will set something like 10.01 in "FMScriptNameStart" and I will be on a script named something like "My First Script" and I want to result to be "10.01 My First Script" and then move down one script. I then can hit it again and get something like "10.02 My Second Script" when the next one was named "My Second Script"


Right now it is working except it is not incrementing. What am I missing?

In case you cant read the code or if you want to fix it I will include the javascript here:

//--- GET A REFERENCE TO THE KM ENGINE ---
var kme = Application("Keyboard Maestro Engine");

//--- GET A KM VARIABLE ---
var scriptStart = kme.getvariable('FMScriptNameStart');
var scriptName = kme.getvariable('scriptName');

//--- SET A KM VARIABLE ---
//    - Using explicit text
//kme.setvariable('My Other KM Var', { to: "Set by JXA" });

//    - Using a JavaScript Variable
var numbs = scriptStart.split(".");
if ( numbs.includes(3) ){
  // Set each piece of the xx.xx.xx to have zero padding
    result = numbs[0] + "." + numbs[1] + "." + numbs[2] + "." + (parseInt(numbs[3]) + 1).padStart(2, '0');
}else if( numbs.includes(2) ){
    result = numbs[0] + "." + numbs[1] + "." + (parseInt(numbs[2]) + 1).padStart(2, '0');
}else if( numbs.includes(1) ){
    result = numbs[0] + "." + (parseInt(numbs[1]) + 1).padStart(2, '0');
}else{
    result = (parseInt(numbs[0]) + "." + "01" );
}

scriptName = result + " " + scriptName;

kme.setvariable('scriptName', { to: scriptName });

What is the context of this ?

IP scanning ?

Is it really a KM-specific question ?

Just use a four-digit number in a KM variable, increment it by 1 each loop, and when it comes to renaming the file use two "Get Substring" actions:

Substrings Demo.kmmacros (4.6 KB)

Image

Edit to add:

And for the regex fans:

Substrings Regex Demo.kmmacros (3.9 KB)

Image

1 Like

Here is the working javascript

//--- GET A REFERENCE TO THE KM ENGINE ---
var kme = Application("Keyboard Maestro Engine");

//--- GET A KM VARIABLE ---
var scriptStart = kme.getvariable('FMScriptNameStart');
var scriptName = kme.getvariable('scriptName');

var rgx = /^(\d{3})(\.\d{2})+ /i;
scriptName = scriptName.replace(rgx, '');

//--- SET A KM VARIABLE ---
//    - Using explicit text
//kme.setvariable('My Other KM Var', { to: "Set by JXA" });

//    - Using a JavaScript Variable
var numbs = scriptStart.split(".");
if ( numbs.length == 4 ) {
  // Set each piece of the xx.xx.xx to have zero padding
    result = numbs[0] + "." + numbs[1] + "." + numbs[2] + "." + String(parseInt(numbs[3]) + 1).padStart(2, '0');
}else if( numbs.length == 3 ) {
    result = numbs[0] + "." + numbs[1] + "." + String(parseInt(numbs[2]) + 1).padStart(2, '0');
}else if( numbs.length == 2 ) {
    result = numbs[0] + "." + String(parseInt(numbs[1]) + 1).padStart(2, '0');
}else if( numbs.length == 1 ) {
    result = String(parseInt(numbs[0]) + "." + "01" );
}
var scriptStart = kme.setvariable('FMScriptNameStart',{ to: result });

scriptName = result + " " + scriptName;
kme.setvariable('scriptName', { to: scriptName });

In case anyone else wants something like this