Created
February 20, 2018 18:16
-
-
Save ttscoff/9dff16b7f2ac23d79d9758dfb1717289 to your computer and use it in GitHub Desktop.
Keyboard Maestro macro for natural language dates in TaskPaper 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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