Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Keyboard Maestro macro for natural language dates in TaskPaper 3
(function() {
'use strict'
function TPContextExpandDates(editor, options) {
function toRx(string) {
return '(' + string.split(/[ ,]+/).join('|') + ')';
}
var selection = editor.selection,
selectedItems = selection.selectedItems,
isoVal = null;
editor.outline.groupUndoAndChanges(function() {
selectedItems.forEach(function(task) {
for (var prop in task.attributes) {
var tag = prop.match(/^data-(?!type)(.*)/);
if (tag && tag[0].match(toRx(options.dateTags))) {
// item.setAttribute(tag[0], );
var tagValue = task.getAttribute(tag[0]);
console.log(tagValue);
tagValue = isNaN(tagValue) ? tagValue : tagValue + 'd';
isoVal = tagValue ? DateTime.format(tagValue) : null;
if (isoVal && isoVal !== 'invalid date') {
isoVal = isoVal.replace(/([\d\-]+)( .*?)$/,'$1');
task.setAttribute(tag[0], isoVal);
}
}
}
});
});
editor.moveSelectionToItems(selection);
// return rx;
}
function appIsInstalled(strBundleId) {
ObjC.import('AppKit');
return ObjC.unwrap(
$.NSWorkspace.sharedWorkspace
.URLForApplicationWithBundleIdentifier(
strBundleId
)
.fileSystemRepresentation
) !== undefined;
}
var strKMEid = "com.stairways.keyboardmaestro.engine",
kmVars = appIsInstalled(strKMEid) ? Application(strKMEid).variables : {},
tagRX = (kmVars.dateTags && kmVars.dateTags.value() || 'start due');
var tp = Application("TaskPaper"),
docs = tp.documents;
if (docs.length) {
return docs[0].evaluate({
script: TPContextExpandDates.toString(),
withOptions: {
dateTags: tagRX
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment