Plug-In: Focused Outline Items to New Project
Create a new OmniFocus project containing the focused OmniOutliner items as tasks.
Return to: OmniFocus Plug-In Collection
Focused OmniOutliner Items to New Project
/*{
"type": "action",
"targets": ["omnioutliner"],
"author": "Otto Automator",
"identifier": "com.omni-automation.oo.focused-to-omnifocus",
"version": "1.9",
"description": "Create a new OmniFocus project containing the focused items as tasks.",
"label": "Focused Items to New Project",
"paletteLabel": "Focused To New Project",
"image": "rectangle.stack.fill.badge.plus"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
items = selection.editor.focusedItems.map(item => item.topic)
textInputField = new Form.Field.String(
"textInput",
"Project Title",
null
)
menuItems = ["Parallel", "Sequential", "Single Actions"]
menuIndexes = [0,1,2]
menuElement = new Form.Field.Option(
"menuElement",
"Project Type",
menuIndexes,
menuItems,
0
)
completedByChildrenCheckbox = new Form.Field.Checkbox(
"shouldBeCompletedByChildren",
"Complete with last action",
null
)
inputForm = new Form()
inputForm.addField(textInputField)
inputForm.addField(menuElement)
inputForm.addField(completedByChildrenCheckbox)
inputForm.validate = function(formObject){
var inputText = formObject.values['textInput']
return (!inputText) ? false:true
}
formPrompt = "Title and settings for new project:"
buttonTitle = "Continue"
formObject = await inputForm.show(formPrompt, buttonTitle)
textValue = formObject.values['textInput']
projectTypeIndex = formObject.values['menuElement']
shouldBeCompletedByChildren = formObject.values['shouldBeCompletedByChildren']
// THE OPTIONAL ARGUMENT MAY BE: string, number, date, array, or object
targetFunctionArgument = {
"items": items,
"title": textValue,
"projectTypeIndex": projectTypeIndex,
"shouldBeCompletedByChildren": shouldBeCompletedByChildren
}
targetAppName = "omnifocus"
// THE FUNCTION TO BE EXECUTED ON THE TARGET APP
function targetAppFunction(targetFunctionArgument){
try {
itemTopics = targetFunctionArgument["items"]
projectTitle = targetFunctionArgument["title"]
projectTypeIndex = targetFunctionArgument["projectTypeIndex"]
shouldBeCompletedByChildren = targetFunctionArgument["shouldBeCompletedByChildren"]
project = new Project(projectTitle)
project.status = Project.Status.Active
if (projectTypeIndex === 1){
project.sequential = true
} else if (projectTypeIndex === 2){
project.containsSingletonActions = true
}
project.completedByChildren = shouldBeCompletedByChildren
itemTopics.forEach(item => {
new Task(item, project.ending)
})
projectID = project.id.primaryKey
url = "omnifocus:///task/" + projectID
console.log("Passed function has been executed")
return url
}
catch(error){
console.error("An error occurred.")
throw error
}
}
// CREATE SCRIPT URL WITH FUNCTION
scriptURL = URL.tellFunction(
targetAppName,
targetAppFunction,
targetFunctionArgument
)
// CALL THE SCRIPT URL, PROCESS RESULTS OR ERROR
scriptURL.call(function(reply){
// PROCESS RESULTS OF SCRIPT
if (typeof reply === "object"){
console.log(reply["result"])
} else {
console.log(reply)
// open returned link to show new project
URL.fromString(reply).open()
}
}, function(error){
// PROCESS SCRIPT ERROR
new Alert("SCRIPT ERROR", error.errorMessage).show()
console.error(error)
})
});
action.validate = function(selection, sender){
return (selection.editor.focusedItems.length > 0)
};
return action;
})();