Following some discussion in another thread here is an illustrative macro, showing one way of using
- Execute JavaScript in Safari or Chrome, and then
- Execute JavaScript for Automation
To list a subset of metadata from the front page in Safari or Chrome, choosing and copying one or more items to the clipboard:
Safari or Chrome front page meta data - choosing and copying.kmmacros (26.8 KB)
Dependent Cartesian Closed Categories
Yamada, Norihiro
2017/04/16
The North Korea-Trump Nightmare
Nicholas Kristof
April 20, 2017
Some pages have more meta-data than we are likely to be interested in, so this macro shows a way of specifying sub-strings to look for in Meta tag names.
It also shows some basics of post-processing whatever is selected, using JavaScript for Automation.
Safari or Chrome JavaScript:
(() => {
'use strict';
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
const strKeys = document.kmvar.metaKeys || 'title author published date';
return show(
Array.from(document.querySelectorAll(
strKeys.split(/\s+/)
.map(k => 'meta[name*="' + k + '" i]')
.join(', ')
))
.reduce((a, x) => {
const
k = x.name,
prev = a[k],
v = x.content;
return (a[k] = (prev ? prev + ', ' + v : v), a);
}, {})
);
})();
JavaScript for Automation:
(function () {
'use strict';
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
var kme = Application('Keyboard Maestro Engine'),
dct = JSON.parse(kme.getvariable('metaJSON')),
dks = Object.keys(dct),
// keyValue pairs in the 'metaKeys' KM variable order
lstKV = kme.getvariable('metaKeys')
.split(/\s+/)
.reduce((a, k) => {
const lk = k.toLowerCase();
return a.concat(
dks.reduce((b, x) =>
x.toLowerCase()
.indexOf(lk) !== -1 ? b.concat(
[
[x, dct[x]]
]
) : b, [])
)
}, []);
return lstKV.length === 0 ? '' : function () {
var a = Application.currentApplication(),
se = Application("System Events"),
sa = (se.includeStandardAdditions = true, se),
delim = ' -> ',
kvs = lstKV.map(function (kv) {
return kv.join(delim);
}),
varChoice = (sa.activate(), se.chooseFromList(kvs, {
withTitle: kme.getvariable('browserName') +
' page metadata',
withPrompt: 'Cmd-click for multiple selections',
defaultItems: [kvs[0]],
okButtonName: 'OK',
cancelButtonName: 'Cancel',
multipleSelectionsAllowed: true,
emptySelectionAllowed: true
}));
return (
a.activate(),
varChoice !== false ? varChoice.map(function (s) {
return s.split(delim)[1];
})
.join('\n') : ''
);
}();
})();