Scripting: Remove tag when task is completed

Hello all! New user and new to Automation for OmniFocus. I’m trying to create a script that will remove the tag “Next” when the task is complete. I found another post here in the forum that is similar but I have no idea how to properly add it. I selected Automation --> show console --> add action and entered the code below but the action is greyed out and I have errors in the console. Any help would be greatly appreciated! -Tom

/{
“author”: “Author Name”,
“targets”: [“omnifocus”],
“type”: “action”,
“identifier”: “com.mycompany.Remove Next tag when complete”,
“version”: “0.1”,
“description”: “A plug-in that…”,
“label”: “Remove Next tag when complete”,
“mediumLabel”: “Remove Next tag when complete”,
“paletteLabel”: “Remove Next tag when complete”,
}
/
(() => {
var action = new PlugIn.Action(function(selection) {
// Add code to run when the action is invoked

tell application “OmniFocus”
activate
delay 0.2
tell front window
set selectedTrees to selected trees of content
set selectedTasks to every item of selectedTrees
end tell

tell front document
	repeat with i from 1 to count of selectedTasks
		set theTask to the value of item i of selectedTasks
		set currTags to name of tags of theTask
		
		repeat with x from 1 to count of currTags
			set currTag to item x of currTags
			if currTag = "Next" then
				set currTag to (first flattened tag whose name is item x of currTags)
				remove currTag from tags of theTask
			end if
		end repeat
		
		mark complete theTask
		
	end repeat
end tell

end tell
*/

return action;

})();

1 Like

Yeah, you’ve got a mixup of JavaScript and Applescript here. Try this maybe? (Saved as a .omnijs file). This does assume “Next” is a root level tag. If it isn’t, let me know, I can provide an edit.

/*{
	"type": "action",
	"targets": ["omnifocus"],
	"description": "This action will remove a given tag and mark the task complete.",
	"label": "Complete and Remove Tag",
}*/
(() => {
	const action = new PlugIn.Action(function(selection, sender){
	    const selectedTag = tagNamed('Next');
	    
		selection.tasks.forEach((task) => {
			task.removeTag(selectedTag);
			task.markComplete();
		});
	});

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

Thank you for your reply! I ended up figuring out my error and compiled it as an AppleScript… Is there a way to have these scripts active in iOS or is it just available on the Mac? Thanks again!

No problem! Unfortunately, AppleScript is Mac-only, but JavaScript plug-ins can run on iOS.

Will that javascript example you wrote above work on iOS?

Yep! Though I noticed an issue just now and posted an edit.

1 Like

Cool. I’ll see if I can get it to work. Thank you!

Works great!!! Now is there a way to add it to the quick menu in iOS? From what I can see the only way to run it is to use the Share menu…

…and THANK YOU soooo much for your help with this!

1 Like

I think the Share menu and the Automation button are the only ways to run plugins, and the latter isn’t great for selections. It would certainly be nice if you could add at least some to the menu directly.

It occurred to me that something like this might be a preferable approach: This script runs through your entire database and removes the Next tag from any completed or dropped tasks:

/*{
	"type": "action",
	"targets": ["omnifocus"],
	"description": "This action will remove a given tag from completed tasks.",
	"label": "Remove Tag from Completed",
}*/
(() => {
	const action = new PlugIn.Action(function(){
	    const selectedTag = tagNamed('Next');
	    
		flattenedTasks.forEach((task) => {
		    if (task.taskStatus === Task.Status.Completed || task.taskStatus === Task.Status.Dropped) {
		        task.removeTag(selectedTag);
		    }
		});
	});

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

Awesome! Super helpful. I’ve sent the OmniFocus people an email suggesting custom quick menu additions. Hopefully in a future update… Thanks again! I owe ya a beer.

1 Like