Last active
March 1, 2020 14:33
-
-
Save deaghean/df97bc106a31545bc4837c041034303b to your computer and use it in GitHub Desktop.
Distribute review dates to projects over a given interval
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
/*{ | |
"type": "action", | |
"targets": ["omnifocus"], | |
"author": "Josh Hughes", | |
"identifier": "com.josh-hughes.SetReviewDateByInterval", | |
"version": "1.0", | |
"description": "Distribute review dates to projects over a given interval", | |
"label": "Set Review Date Interval", | |
"paletteLabel": "Set Review Date Interval" | |
}*/ | |
// 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 defaultInterval = '1'; | |
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 interval = new Form.Field.String('interval', 'Over Number of Days', defaultInterval); | |
reviewForm.addField(datePicker); | |
reviewForm.addField(interval); | |
let formPromise = reviewForm.show('Distribute Review Dates', 'Select Date and Interval'); | |
formPromise.then(function(form) { | |
let newReviewDate = form.values.reviewDate; | |
let interval = parseInt(form.values.interval); | |
if (!isNaN(interval)) { | |
const activeProjects = getUnfinishedProjects(); | |
if (activeProjects.length > 0) { | |
const groupSize = Math.ceil((activeProjects.length/interval)); | |
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 number of days 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