Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created February 20, 2018 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/9dff16b7f2ac23d79d9758dfb1717289 to your computer and use it in GitHub Desktop.
Save ttscoff/9dff16b7f2ac23d79d9758dfb1717289 to your computer and use it in GitHub Desktop.
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