×

Plug-In: Tasks to Projects

On occasion, you may wish to convert an existing task into a project. A function of the Database class is provided for such a purpose.

The convertTasksToProjects() Function

For example, to convert each top-level inbox item into a new project at the end of your library and capture the resulting projects:

Convert Inbox Tasks to Projects


var newProjects = convertTasksToProjects(inbox, library.ending)

The Tasks to Projects Plug-In

The following plug-in uses the convertTasksToProjects() function of the Database class to convert the selected tasks into projects placed at either the beginning or ending of the library, or in a new folder placed at the beginning or ending of the library.

A form for selecting the destination for the new projects is displayed when the plug-in is executed:

tasks-to-projects tasks-to-projects-folder-name

Return to: OmniFocus Plug-In Collection

Tasks to Projects
 

/*{ "type": "action", "targets": ["omnifocus"], "author": "Otto Automator", "identifier": "com.omni-automation.of.tasks-to-projects", "version": "1.0", "description": "Converts the selected tasks to projects placed at either the beginning or ending of the library.", "label": "Convert Tasks to Projects", "shortLabel": "Tasks to Projects", "image": "archivebox" }*/ (() => { var action = new PlugIn.Action(function(selection, sender){ // action code // selection options: tasks, projects, folders, tags, allObjects var defaultFolderName = "Converted Tasks" var menuItems = ["Beginning of Library", "End of Library", "New Beginning Folder", "New Ending Folder"] var menuIndexes = [0,1,2,3] var insertionLocationMenu = new Form.Field.Option( "insertionLocation", null, menuIndexes, menuItems, 0 ) var inputForm = new Form() inputForm.addField(insertionLocationMenu) var formPrompt = "Choose the destination:" var buttonTitle = "Continue" formPromise = inputForm.show(formPrompt,buttonTitle) inputForm.validate = function(formObject){ console.log(formObject.fields.length) var insertionLocationIndex = formObject.values['insertionLocation'] if (insertionLocationIndex > 1 && formObject.fields.length === 1){ var textInputField = new Form.Field.String( "textInput", "Folder Title", null ) inputForm.addField(textInputField) } else if (insertionLocationIndex > 1 && formObject.fields.length === 2){ // do nothing } else if (insertionLocationIndex < 2 && formObject.fields.length > 1){ inputForm.removeField(inputForm.fields[1]) } return true } formPromise.then(formObject => { var insertionLocationIndex = formObject.values['insertionLocation'] if ([0,1].includes(insertionLocationIndex)){ var folderName = defaultFolderName } else { var folderName = formObject.values['textInput'] if (!folderName || folderName === ""){folderName = defaultFolderName} } if (insertionLocationIndex === 0){ var insertionLocation = library.beginning } else if (insertionLocationIndex === 1){ var insertionLocation = library.ending } else if (insertionLocationIndex === 2){ var newFolder = new Folder(folderName, library.beginning) var insertionLocation = newFolder } else if (insertionLocationIndex === 3){ var newFolder = new Folder(folderName, library.ending) var insertionLocation = newFolder } var newProjects = convertTasksToProjects(selection.tasks, insertionLocation) if ([2,3].includes(insertionLocationIndex)){ var urlStr = "omnifocus:///folder/" + newFolder.id.primaryKey } else { var itemIDs = newProjects.map(item => item.id.primaryKey) var urlStr = "omnifocus:///task/" + itemIDs.join(',') } URL.fromString(urlStr).open() }) formPromise.catch(function(err){ console.error("form cancelled", err.message) }) }); action.validate = function(selection, sender){ // validation code // selection options: tasks, projects, folders, tags, allObjects return (selection.tasks.length > 0) }; return action; })();