Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created February 20, 2018 18:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/73ed9f6e0848e4c76d928467c4282692 to your computer and use it in GitHub Desktop.
Save ttscoff/73ed9f6e0848e4c76d928467c4282692 to your computer and use it in GitHub Desktop.
Keyboard Maestro macro for cycling numeric tag values in TaskPaper 3
// Cycle forward/back through numeric tags
// Brett Terpstra <http://brettterpstra.com>
// Credit: Rob Trew @complexpoint
(function () {
'use strict';
function TaskPaperContext(editor, options) {
var selection = editor.selection,
selectedItems = selection.selectedItems,
strAttrib = 'data-' + options.tag,
increment = options.offset,
max = options.maxVal,
min = options.minVal;
function cyclePriority(val,inc) {
var newVal;
if (inc > 0) {
if (val === 0) {
newVal = min;
} else if (val + inc > max) {
newVal = null;
} else {
newVal = val + inc;
}
} else {
if (val === 0) {
newVal = max;
} else if (val + inc < min) {
newVal = null;
} else {
newVal = val + inc;
}
}
return newVal;
}
editor.outline.groupUndoAndChanges(function () {
selectedItems.forEach(function (item) {
var currVal = item.getAttribute(strAttrib),
newVal = null;
if (currVal) {
currVal = parseInt(currVal,10);
} else {
currVal = 0;
}
item.setAttribute(strAttrib, cyclePriority(currVal,increment));
});
});
editor.moveSelectionToItems(selection);
}
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 : {},
varTag = kmVars.incTag,
varOffset = kmVars.incVal,
varMinVal = kmVars.minVal,
varMaxVal = kmVars.maxVal;
var ds = Application('TaskPaper').documents();
if (ds.length) {
return ds[0].evaluate({
script: TaskPaperContext.toString(),
withOptions: {
tag: (varTag && varTag.value()) || 'priority',
offset: (varOffset && parseInt(varOffset.value(),10)) || 1,
maxVal: (varMaxVal && parseInt(varMaxVal.value(),10)) || 5,
minVal: (varMinVal && parseInt(varMinVal.value(),10)) || 1
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment