Skip to content

Instantly share code, notes, and snippets.

@deaghean
Last active March 1, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deaghean/7bd310160122bd8fa5635fc50b78f986 to your computer and use it in GitHub Desktop.
Save deaghean/7bd310160122bd8fa5635fc50b78f986 to your computer and use it in GitHub Desktop.
Distribute review dates to projects with a max number of reviews per day.
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Josh Hughes",
"identifier": "com.josh-hughes.SetReviewDateWithMaxNumber",
"version": "1.0",
"description": "Distribute review dates to projects with a max number of reviews per day.",
"label": "Set Review Date With Max Number",
"paletteLabel": "Set Review Date With Max Number"
}*/
// To enable Omni JS, go to this link in your browser:
// omnifocus:///change-preference?OJSEnabled=1
var _ = function() {
function getUnfinishedProjects() {
const activeProjects = [];
library.apply((project) => {
if (project.status && (project.status === Project.Status.Active || project.status === Project.Status.OnHold)) {
activeProjects.push(project);
}
});
return activeProjects;
}
var action = new PlugIn.Action(function() {
const defaultGroupSize = '10';
let firstReviewDate = new Date();
firstReviewDate.setHours(0,0,0,0); // Reset time to midnight
let reviewForm = new Form();
let datePicker = new Form.Field.Date('reviewDate', 'First Review Date', firstReviewDate);
let groupSize = new Form.Field.String('groupSize', 'Max Number of Reviews', defaultGroupSize);
reviewForm.addField(datePicker);
reviewForm.addField(groupSize);
let formPromise = reviewForm.show('Distribute Review Dates', 'Select Date and Max Reviews');
formPromise.then(function(form) {
let newReviewDate = form.values.reviewDate;
let groupSize = parseInt(form.values.groupSize);
if (!isNaN(groupSize)) {
const activeProjects = getUnfinishedProjects();
if (activeProjects.length > 0) {
let index = 0;
activeProjects.forEach((project) => {
if (index >= groupSize) {
newReviewDate.setDate(newReviewDate.getDate() + 1);
index = 0;
}
project.nextReviewDate = newReviewDate;
index++;
});
}
} else {
let error = new Alert('Error', 'The max number of reviews value must be a number.').show();
}
}).catch(function () {});
});
action.validate = function(){
let activeProjects = getUnfinishedProjects();
return (activeProjects.length > 0);
};
return action;
}();
_;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment