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 Items to New Project
/*{
"type": "action",
"targets": ["omnioutliner"],
"author": "Otto Automator",
"identifier": "com.omni-automation.oo.focused-to-omnifocus",
"version": "1.7",
"description": "Create a new OmniFocus project containing the focused items as tasks.",
"label": "Focused Items to New Project",
"paletteLabel": "Focused To New Project"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender){
// action code
// selection options: columns, document, editor, items, nodes, outline, styles
var items = selection.editor.focusedItems.map(item => {
return item.topic
})
var textInputField = new Form.Field.String(
"textInput",
"Project Title",
null
)
var menuItems = ["Parallel", "Sequential", "Single Actions"]
var menuIndexes = [0,1,2]
var menuElement = new Form.Field.Option(
"menuElement",
"Project Type",
menuIndexes,
menuItems,
0
)
var completedByChildrenCheckbox = new Form.Field.Checkbox(
"shouldBeCompletedByChildren",
"Complete with last action",
null
)
var inputForm = new Form()
inputForm.addField(textInputField)
inputForm.addField(menuElement)
inputForm.addField(completedByChildrenCheckbox)
var formPrompt = "Title and settings for new project:"
var buttonTitle = "Continue"
formPromise = inputForm.show(formPrompt,buttonTitle)
inputForm.validate = function(formObject){
var inputText = formObject.values['textInput']
return ((!inputText)?false:true)
}
formPromise.then(function(formObject){
var textValue = formObject.values['textInput']
var projectTypeIndex = formObject.values['menuElement']
var shouldBeCompletedByChildren = formObject.values['shouldBeCompletedByChildren']
// THE OPTIONAL ARGUMENT MAY BE: string, number, date, array, or object
var targetFunctionArgument = {
"items": items,
"title": textValue,
"projectTypeIndex": projectTypeIndex,
"shouldBeCompletedByChildren": shouldBeCompletedByChildren
}
var targetAppName = "omnifocus"
// THE FUNCTION TO BE EXECUTED ON THE TARGET APP
function targetAppFunction(targetFunctionArgument){
try {
var itemTopics = targetFunctionArgument["items"]
var projectTitle = targetFunctionArgument["title"]
var projectTypeIndex = targetFunctionArgument["projectTypeIndex"]
var shouldBeCompletedByChildren = targetFunctionArgument["shouldBeCompletedByChildren"]
var 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)
})
var projectID = project.id.primaryKey
var 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
var 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)
})
})
formPromise.catch(function(err){
console.error("form cancelled", err.message)
})
});
action.validate = function(selection, sender){
// validation code
// selection options: columns, document, editor, items, nodes, outline, styles
return (selection.editor.focusedItems.length > 0)
};
return action;
})();