Here is one scripted approach:
Variable matches in two lists.kmmacros (22.0 KB)
Variable matches in two lists.kmmacros (22.0 KB)
JS source
(() => {
'use strict';
const main = () => {
const
// Any number of KM variable names ...
kxs = ['name1', 'name2', 'name3'],
// A second list of any no. of KM variable names ...
kys = ['name4', 'name5', 'name6'];
// MAIN -------------------------------------------------
const main = () =>
matchPairs(kxs, kys);
// VARIABLES WITH MATCHING VALUES ----------------------
// matchPairs :: [String] -> [String] ->
// [((String, String), (String, String))]
const matchPairs = (kmVarNameList1, kmVarNameList2) =>
concatMap(
xy => snd(fst(xy)) === snd(snd(xy)) ? (
[xy]
) : [],
cartesianProduct(
map(keyVal, kmVarNameList1),
map(keyVal, kmVarNameList2)
)
);
const
kme = Application('Keyboard Maestro Engine'),
keyVal = k => [k, kme.getvariable(k)];
// TEST
return JSON.stringify(
main()
);
};
// GENERIC FUNCTIONS ----------------------------
// https://github.com/RobTrew/prelude-jxa
// Tuple (,) :: a -> b -> (a, b)
const Tuple = (a, b) => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
// e.g. [(*2),(/2), sqrt] <*> [1,2,3]
// --> ap([dbl, hlf, root], [1, 2, 3])
// --> [2,4,6,0.5,1,1.5,1,1.4142135623730951,1.7320508075688772]
// Each member of a list of functions applied to each
// of a list of arguments, deriving a list of new values.
// apList (<*>) :: [(a -> b)] -> [a] -> [b]
const apList = (fs, xs) => //
fs.reduce((a, f) => a.concat(
xs.reduce((a, x) => a.concat([f(x)]), [])
), []);
// cartesianProduct :: [a] -> [b] -> [(a, b)]
const cartesianProduct = (xs, ys) =>
apList(xs.map(x => y => [x, y]), ys);
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) =>
xs.reduce((a, x) => a.concat(f(x)), []);
// fst :: (a, b) -> a
const fst = tpl => tpl[0];
// listFromTuple :: (a, a ...) -> [a]
const listFromTuple = tpl =>
Array.from(tpl);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
// MAIN ---
return main();
})();