New UUID in JavaScript for Automation Action

For example:

Generate give number of UUIDs in JavaScript for Automation context.kmmacros (2.6 KB)


Expand disclosure triangle to view JS source
(() => {
    "use strict";

    const main = () =>
        enumFromTo(1)(
            Number(
                Application("Keyboard Maestro Engine")
                .getvariable("uuidCount")
            )
        )
        .map(newUUID)
        .join("\n");


    // --------------------- GENERIC ---------------------

    // enumFromTo :: Int -> Int -> [Int]
    const enumFromTo = m =>
        n => Array.from({
            length: 1 + n - m
        }, (_, i) => m + i);


    // newUUID :: () -> IO UUID String
    const newUUID = () =>
        ObjC.unwrap($.NSUUID.UUID.UUIDString);

    // MAIN ---
    return main();
})();


JavaScript is an embedded scripting language, and the libraries available to a JS interpreter vary with the context is in which it is embedded.

For example:

  • A JS interpreter embedded in a browser has access, in the global context, to a library exposing an instance of the Document Object Model
  • The JXA embedding provides no DOM library – there is no web-page in sight – but does expose, in the global context, an Application library object with various methods and properties that are useful for synchronous scripting and access to ObjC libraries.
  • Neither of these has any connection to the Node JS embedding (of a V8 JS interpreter) in a non-browser local run-time context, for which the uuidv4 module is written.
3 Likes