Sequential Numbering for beginners

Hi there.

I am trying to duplicate a file and the increase the numbering by 100.e.g.:

0825DUD_0100
0825DUD_0200
0825DUD_0300
etc

Creating a macro for the duplication is easy, but I am struggling adding the sequential numbering. to my macro.

Does anyone have an idea for my problem?
Thanks in advance

regex... but I am not an expert for that. Personally I use pathfinder for operations like that (and only for that :D)

With any luck, if we start by giving you a quick JavaScript solution, then others will follow it up with something more directly Maestronic:

Successor file name.kmmacros (19.8 KB)

nextFile
JS Source

(() => {
    const main = () => {
        const
            strFn = Application('Keyboard Maestro Engine')
            .getvariable('fileNameCurrent');

        return succFileName('_', 4, 100)(strFn)
    };

    // Separator -> num string width -> increment size
    //    -> filename -> filename

    // succFileName :: String -> Int -> Int -> String -> String
    const succFileName = (sep, width, delta) => fn => {
        const parts = splitOn(sep, fn);
        return 1 < parts.length ? (() => {
            const strN = parts[1];
            return parts[0] + sep + justifyRight(
                width,
                '0',
                (delta + (
                    isNaN(strN) ? (
                        0
                    ) : parseInt(strN, 10)
                )).toString()
            );
        })() : fn;
    };

    // GENERIC FUNCTIONS ----------------------------------

    // https://github.com/RobTrew/prelude-jxa

    // justifyRight :: Int -> Char -> String -> String
    const justifyRight = (n, cFiller, strText) =>
        n > strText.length ? (
            (cFiller.repeat(n) + strText)
            .slice(-n)
        ) : strText;

    // showJSON :: a -> String
    const showJSON = x => JSON.stringify(x, null, 2);

    // splitOn :: a -> [a] -> [[a]]
    // splitOn :: String -> String -> [String]
    const splitOn = (needle, haystack) =>
        haystack.split(needle);

    return main();
})();

That’s awesome!

Thanks

Thorsten

Here's something more directly Maestronic, as @ComplexPoint put it:

Duplicate File in Sequence.kmmacros (2.1 KB)

Results

03%20PM

Are you duplicating the file all at one time?

If so you can use a For Each action action with a Number Range collection and use the formatting of Set Variable to Calculation action to format it as 01, 02, etc.

Eg:

For Each.kmactions (1.2 KB)

2 Likes