Assigning project to task, if there's no existing project assigned

I have some automation set up to move tasks from Inbox to a generic Miscellaneous project.
I’ve specified in the code to only replace the project if there isn’t an existing project already asssigned (if(task.containingProject === null)), but OmniFocus overrides the project field regardless of whether it was empty or not.

Can someone check where I went wrong?

(() => {
	var action = new PlugIn.Action(function(selection, sender){
        
        var tagObj = flattenedTags.byName("Personal") || new Tag("Personal");
        var remTag = flattenedTags.byName("Work");
        var projName = "Miscellaneous";
        var projObj = flattenedProjects.byName(projName) || new Project(projName);

        selection.tasks.forEach(task => {
            task.removeTag(remTag)
            task.addTag(tagObj)
            var arrayTasks = [];

            if(task.containingProject === null){
                moveTasks(selection.tasks, projObj)
            }
            else {

            }

        })
        
        
	});
    
	return action;
})();

Try testing whether the assignedContainer is null rather than the containingProject. The containingProject for an item will be null as long as the item is still in the Inbox (i.e. until it is cleaned up and moved out of the Inbox).

I originally thought it worked, but then realised it didn’t.

The project is still being changed even though there’s an existing one.

ah, I see what I did. Sometimes I was clicking the button while I was in the Inbox, and sometimes I was doing it from within Projects view. As you said, containingProject is for whether something is still in inbox.

I’ve fixed my code so that it works whether the task has be moved from Inbox or not.

(() => {
	var action = new PlugIn.Action(function(selection, sender){
        
        var tagObj = flattenedTags.byName("Personal") || new Tag("Personal");
        var remTag = flattenedTags.byName("Work");
        var projName = "Miscellaneous";
        var projObj = flattenedProjects.byName(projName) || new Project(projName);

        selection.tasks.forEach(task => {
            task.removeTag(remTag);
            task.addTag(tagObj);
            var arrayTasks = [];

            if(task.containingProject === null){
                if(task.assignedContainer === null){
                    moveTasks(selection.tasks, projObj);
                }
                else {}
            }
            else {

            }

        });
        
        
	});
    
	return action;
})();