Setting a Tag to new Task

Busy learning Automation and JScript having spent many happy hour with VBA!

Starting with a sample action I have the code creating multiple tasks to a new project but am unable to work how to add a Tag to each task.

                  todoItems = ["Champ Man","TC","Doc SEM","Doc Event"]
		
		todoItems.forEach((todoItem)=>{
		
		action = project.taskNamed(todoItem) || new Task(todoItem, project)
			action.dueDate = StartDate
			action.note = "Now"

This works fine and adds date and note to eash task. Could someone help me with how to add a Tag to each task?

PRH

Post a full working example.

/{
“type”: “action”,
“targets”: [“omnifocus”],
“author”: “Otto Automator”,
“identifier”: “com.omni-automation.event-check-list-sem”,
“version”: “1.02”,
“description”: “Check list for SEM events.”,
“label”: “Event Check List SEM”,
“paletteLabel”: “Check List”
}
/

var _ = function(){

var action = new PlugIn.Action(function(selection, sender) {

	var inputForm = new Form()
	var dateFormatter = Formatter.Date.withStyle(Formatter.Date.Style.Short, Formatter.Date.Style.Short)

	taskNameField = new Form.Field.String(
		"eventTitle",
		"Title",
		"Event"
	)
	startDateField = new Form.Field.Date(
		"startDate",
		"Monday",
		null,
		dateFormatter
	)
	endDateField = new Form.Field.Date(
		"endDate",
		"Sunday",
		null,
		dateFormatter
	)
	
	inputForm.addField(taskNameField)
	inputForm.addField(startDateField)
	inputForm.addField(endDateField)

	formPromise = inputForm.show("Enter the event name dates:","Continue")
	
	inputForm.validate = function(formObject){
		currentDateTime = new Date()
		startDateObject = formObject.values["startDate"]
		startDateStatus = (startDateObject && startDateObject > currentDateTime) ? true:false
		endDateObject = formObject.values["endDate"]
		endDateStatus = (endDateObject && endDateObject > startDateObject) ? true:false
		textValue = formObject.values["eventTitle"]
		textStatus = (textValue && textValue.length > 0) ? true:false
		validation = (textStatus && startDateStatus && endDateStatus) ? true:false
		return validation
	}
	
	formPromise.then(function(formObject){		
		eventTitle = formObject.values['eventTitle']
		var StartDate = formObject.values['startDate']
		var EndDate = formObject.values['endDate']
		var tripDuration = (Date.UTC(EndDate.getFullYear(), EndDate.getMonth(), EndDate.getDate()) - Date.UTC(StartDate.getFullYear(), StartDate.getMonth(), StartDate.getDate())) / 86400000;
		
		
		const displayStart = new Date(StartDate).toDateString();
		const displayEnd = new Date(EndDate).toDateString();
		projectName = eventTitle + " (" + displayStart + " - " + displayEnd + ")"
		var project = projectNamed(projectName) || new Project(projectName) 
		project.status = Project.Status.Active
		project.containsSingletonActions = true

		
		
		todoItems = ["TC","Doc SEM","Travel Doc SEM","Hotel Doc SEM","Paid Doc SEM","Invoice SEM"]
		
		todoItems.forEach((todoItem)=>{
		
		action = project.taskNamed(todoItem) || new Task(todoItem, project)
			action.dueDate = StartDate
			action.note = "Now"
			
			
			
		
		})
		
		document.windows[0].perspective = Perspective.BuiltIn.Projects
		document.windows[0].selectObjects([project])
	})
	
	formPromise.catch(function(err){
		console.log("form cancelled", err.message)
	})
});

action.validate = function(selection, sender) {
	// validation code
	// selection options: tasks, projects, folders, tags
	return true
};

return action;

}();
_;

Please enclose plug-in code in triple backticks. From the beginning till the end. Like this:

/{
“type”: “action”,
“targets”: [“omnifocus”],
“author”: “Otto Automator”,
“identifier”: “com.omni-automation.event-check-list-sem”,
“version”: “1.02”,
“description”: “Check list for SEM events.”,
“label”: “Event Check List SEM”,
“paletteLabel”: “Check List”
}/

var _ = function(){
var action = new PlugIn.Action(function(selection, sender) {

	var inputForm = new Form()
	var dateFormatter = Formatter.Date.withStyle(Formatter.Date.Style.Short, Formatter.Date.Style.Short)

	taskNameField = new Form.Field.String(
		"eventTitle",
		"Title",
		"Event"
	)
	startDateField = new Form.Field.Date(
		"startDate",
		"Monday",
		null,
		dateFormatter
	)
	endDateField = new Form.Field.Date(
		"endDate",
		"Sunday",
		null,
		dateFormatter
	)
	
	inputForm.addField(taskNameField)
	inputForm.addField(startDateField)
	inputForm.addField(endDateField)

	formPromise = inputForm.show("Enter the event name dates:","Continue")
	
	inputForm.validate = function(formObject){
		currentDateTime = new Date()
		startDateObject = formObject.values["startDate"]
		startDateStatus = (startDateObject && startDateObject > currentDateTime) ? true:false
		endDateObject = formObject.values["endDate"]
		endDateStatus = (endDateObject && endDateObject > startDateObject) ? true:false
		textValue = formObject.values["eventTitle"]
		textStatus = (textValue && textValue.length > 0) ? true:false
		validation = (textStatus && startDateStatus && endDateStatus) ? true:false
		return validation
	}
	
	formPromise.then(function(formObject){		
		eventTitle = formObject.values['eventTitle']
		var StartDate = formObject.values['startDate']
		var EndDate = formObject.values['endDate']
		var tripDuration = (Date.UTC(EndDate.getFullYear(), EndDate.getMonth(), EndDate.getDate()) - Date.UTC(StartDate.getFullYear(), StartDate.getMonth(), StartDate.getDate())) / 86400000;
		
		
		const displayStart = new Date(StartDate).toDateString();
		const displayEnd = new Date(EndDate).toDateString();
		projectName = eventTitle + " (" + displayStart + " - " + displayEnd + ")"
		var project = projectNamed(projectName) || new Project(projectName) 
		project.status = Project.Status.Active
		project.containsSingletonActions = true

		
		
		todoItems = ["TC","Doc SEM","Travel Doc SEM","Hotel Doc SEM","Paid Doc SEM","Invoice SEM"]
		
		todoItems.forEach((todoItem)=>{
		
		action = project.taskNamed(todoItem) || new Task(todoItem, project)
			action.dueDate = StartDate
			action.note = "Now"
			
			
			
		
		})
		
		document.windows[0].perspective = Perspective.BuiltIn.Projects
		document.windows[0].selectObjects([project])
	})
	
	formPromise.catch(function(err){
		console.log("form cancelled", err.message)
	})
});

action.validate = function(selection, sender) {
	// validation code
	// selection options: tasks, projects, folders, tags
	return true
};

return action;
}();
_;

I’ll look into it.

Thank you, noted for future.

You should find the relevant Tag object,

Class Functions

function byIdentifier(identifier: String) → Tag or null

Returns the Tag with the specified identifier, or null if no such tag exists.

and then use .addTag method on an instance of a Task object:

function addTag(tag: Tag, location: Task.TagInsertionLocation or null)

Adds a Tag to this task at the specified location relative to its other tags, or at the end if no location is specified. If the tag is already present, no change is made. The Database function moveTags can be used to control the ordering of tags within the task.

Feel free to ask for clarification.

1 Like