Automation script to defer projects not task?

Hi,

I have an old script that allows me to select one more tasks and update the defer date to tomorrow.

However, it doesn’t work on projects and I was wondering if anyone knows how to tweak it so that it work on projects?

(() => {
	var action = new PlugIn.Action(function(selection, sender){
        
        var d = new Date();
        var formatter = Formatter.Date.withStyle(Formatter.Date.Style.Short)
        var deferToday = formatter.dateFromString('today @ 9AM'); /* This sets the defer date to today 9am */
        var dueToday = formatter.dateFromString('today @ 12AM'); /* This is the empty due date starting at 12am */
        
		selection.tasks.forEach(task => {
		task.deferDate = deferToday;
            if(task.dueDate !== null){
                var oldDue = task.dueDate;
                var newDue = new Date(dueToday.getFullYear(), dueToday.getMonth(), dueToday.getDate(), oldDue.getHours(), oldDue.getMinutes());
                task.dueDate = newDue;
            }
		})
	});

	action.validate = function(selection, sender){
		return (selection.tasks.length > 0 )
	};
	
	return action;
})();

If you change selection.tasks in both the validation and the action sections to be something like [...selection.tasks, ...selection.projects] that should do what you need. :-)

(I haven’t tested, so if it doesn’t work, you might need to try [...selection.tasks, ...selection.projects.map(p => p.task)] instead. :-))

Hi,

I’m not sure I understand how to replace that…

Original code:

action.validate = function(selection, sender){
		return (selection.tasks.length > 0 )
	};

New code?

action.validate = function(selection, sender){
		return ([...selection.tasks, ...selection.projects].length > 0 )
	};

I figured it out in the end, just posting my solution here in case anyone else faces the same issue.

I removed the validate and instead used it in the if/else statement. I checked whether the selection was a task or a project and updated the relevant defer/due date accordingly.

(() => {
	var action = new PlugIn.Action(function(selection, sender){
        
        var d = new Date();
        var formatter = Formatter.Date.withStyle(Formatter.Date.Style.Short)
        var deferToday = formatter.dateFromString('today @ 9am'); /* This sets the defer date to today at 9am */
        var dueToday = formatter.dateFromString('today'); /* This is the due date starting at 12am */
        
        if (selection.tasks.length > 0){
                selection.tasks.forEach(task => {
                    task.deferDate = deferToday;
                    if(task.dueDate !== null){
                        var oldDue = task.dueDate;
                        var newDue = new Date(dueToday.getFullYear(),
                                              dueToday.getMonth(), dueToday.getDate(), oldDue.getHours(), oldDue.getMinutes());
                        task.dueDate = newDue;
                    }
                })            
        }
        else {
            selection.projects.forEach(project =>{
                project.deferDate = deferToday;
                if(project.dueDate !==null){
                    var oldDue = project.dueDate;
                    var newDue = new Date(dueMon.getFullYear(),
                                          dueMon.getMonth(), dueMon.getDate(), oldDue.getHours(), oldDue.getMinutes());
                    project.dueDate = newDue;
                }
            })
                            
        }

	});
	
	return action;
})();

edit: updated the code to handle multiple selections of projects and tasks

I recommend checking out the Task Date Controls plug-in. It was developed by Omni Support Human, Christian, and works very well in my experience. On the Mac and iPad, I assigned keyboard shortcuts for the two features I use most: “Defer +1 Day” and “Defer +1 Week”.

1 Like

Sorry, yes, that’s exactly what I meant!

But your solution looks like a good one too. :-)