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
convertTasksToProjects(tasks: Array of Task, position: Folder or Folder.ChildInsertionLocation) → (Array of Project) • Converts tasks to new projects at the specified location.
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:
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.1",
"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",
"paletteLabel": "Tasks to Projects",
"image": "archivebox"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
defaultFolderName = "Converted Tasks"
menuItems = ["Beginning of Library", "End of Library", "New Beginning Folder", "New Ending Folder"]
menuIndexes = [0,1,2,3]
insertionLocationMenu = new Form.Field.Option(
"insertionLocation",
null,
menuIndexes,
menuItems,
0
)
inputForm = new Form()
inputForm.addField(insertionLocationMenu)
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
}
formPrompt = "Choose the destination:"
buttonTitle = "Continue"
formObject = await inputForm.show(formPrompt,buttonTitle)
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
}
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()
});
action.validate = function(selection, sender){
return (selection.tasks.length > 0)
};
return action;
})();