To access particular elements of the list, see token:JSONValue [Keyboard Maestro Wiki]
JSON Array of (UUID- Name) pairs for all macros in selected groups.kmmacros (4.7 KB)
Expand disclosure triangle to view JS source
(() => {
"use strict";
// JSON list of (UUID, Name) pairs
// for all macros in selected groups.
// Rob Trew @2024
const main = () => {
const km = Application("Keyboard Maestro");
return sortBy(
comparing(snd)
)(
km.selectedMacroGroups().flatMap(group => {
const macros = group.macros;
return zip(
macros.id()
)(
macros.name()
);
})
);
};
// --------------------- GENERIC ---------------------
// comparing :: Ord a => (b -> a) -> b -> b -> Ordering
const comparing = f =>
// The ordering of f(x) and f(y) as a value
// drawn from {-1, 0, 1}, representing {LT, EQ, GT}.
x => y => {
const
a = f(x),
b = f(y);
return a < b
? -1
: a > b
? 1
: 0;
};
// snd :: (a, b) -> b
const snd = tpl =>
// Second member of a pair.
tpl[1];
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
const sortBy = f =>
// A copy of xs sorted by the comparator function f.
xs => xs.slice()
.sort((a, b) => f(a)(b));
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
// MAIN --
return JSON.stringify(main(), null, 2);
})();