I am working on a macro that uses a JavaScript script to generate a UUID. I am using this code:
When I run the UUID node.JS module in Terminal, it works fine. However, when I run this script in Keyboard Maestro, it doesn't appear to be importing the node.js module properly.
Alternatively, I realized I can run the UUID as a shell script. See screenshot. This macro works well. However, what is the best way to generate multiple UUIDs based on a user input. For example, a user requests 150 UUIDs and the shell script generates 150 and puts them in the clipboard.
(() => {
"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.