App to App
Using Omni Automation, the Omni suite of applications have the ability to transfer data to and from other Omni applications. In addition, using the tellFunction(…) and call(…) functions of the URL class, Omni Automation scripts can be sent from one Omni application to be executed by another.
Use of the these specialized functions is detailed in the Omni App-to-App section of the Omni Automation Actions documentation.
Copy Selected OmniFocus Tasks to OmniPlan
In the following example, data is gathered about the selected OmniFocus tasks and then set to OmniPlan as a JSON array of Objects, to recreate the tasks in the current project. In this case, the argument for the passed function is a JavaScript array of JavaScript objects, with each object containing the specifics of a selected OmniFocus task. The transfered data is then processed in sequence to construct new tasks in OmniPlan.
(Requires OmniFocus 3.7 or newer)
Example Argument Data
[
{
"OFtaskTitle":"TASK ONE",
"OFtaskNote":"omnifocus:///task/gRAODj8WNwd",
"OFtaskDueDate":"2020-03-26T00:00:00.000Z"
},
{
"OFtaskTitle":"TASK TWO",
"OFtaskNote":"omnifocus:///task/iakN8cX6A5u",
"OFtaskDueDate":"2020-03-16T07:00:00.000Z"
},
{
"OFtaskTitle":"TASK THREE",
"OFtaskNote":"omnifocus:///task/hKtVOc-4SXV",
"OFtaskDueDate":"2020-03-16T07:00:00.000Z"
}
]
Here is the example plug-in:
Copy Selected OmniFocus Tasks to OmniPlan
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.op.copy-tasks-to-omniplan",
"version": "1.2",
"description": "This action will create copies of the selected tasks in the current OmniPlan project.",
"label": "Copy Selected Tasks to OmniPlan",
"shortLabel": "Copy to OmniPlan",
"paletteLabel": "Copy to OmniPlan"
"image": "doc.on.doc.fill"
}*/
(() => {
const action = new PlugIn.Action(async function(selection, sender){
now = new Date()
today = Calendar.current.startOfDay(now)
targetFunctionArgument = new Array()
selection.tasks.forEach(function(OFtask){
var OFtaskTitle = OFtask.name
var OFtaskID = OFtask.id.primaryKey
var OFtaskLink = "omnifocus:///task/" + OFtaskID
var OFtaskNote = OFtask.note
if (OFtaskNote){
OFtaskNote = (OFtaskNote.length > 0) ? OFtaskNote + "\n" + OFtaskLink : OFtaskLink
} else {
OFtaskNote = OFtaskLink
}
var OFtaskDueDate = OFtask.dueDate
OFtaskDueDate = (OFtaskDueDate != null) ? OFtaskDueDate : today
// CREATE TASK DATA OBJECT
var taskDataObj = {
"OFtaskTitle":OFtaskTitle,
"OFtaskNote":OFtaskNote,
"OFtaskDueDate":OFtaskDueDate
}
targetFunctionArgument.push(taskDataObj)
})
console.log(JSON.stringify(targetFunctionArgument))
function targetAppFunction(arg){
try {
try{document.name}catch(err){throw "No project open."}
var OPTaskLinks = new Array()
arg.forEach(taskDataObj => {
OPtask = actual.rootTask.addSubtask()
OPtaskLink = "omniplan:///task/" + OPtask.uniqueID
OPtask.title = taskDataObj["OFtaskTitle"]
OPtask.note = taskDataObj["OFtaskNote"]
OPtask.endNoLaterThanDate = new Date(taskDataObj["OFtaskDueDate"])
OPTaskLinks.push(OPtaskLink)
})
return OPTaskLinks
}
catch(error){
console.log("An error occurred.")
throw error
}
}
scriptURL = URL.tellFunction(
"omniplan",
targetAppFunction,
targetFunctionArgument
)
scriptURL.call(function(result){
// PROCESS RESULTS OF SCRIPT
console.log("targetAppFunction result: ", result)
var selectedTasks = selection.tasks
result.forEach((OPTaskLink, index) => {
var OFTask = selectedTasks[index]
var OFTaskNote = OFTask.note
if (OFTaskNote){
if(OFTaskNote.length > 0){
OFTask.note = OFTaskNote + "\n" + OPTaskLink
} else {
OFTask.note = OPTaskLink
}
} else {
OFTask.note = OPTaskLink
}
})
}, function(err){
// PROCESS SCRIPT ERROR
new Alert("SCRIPT ERROR", err.errorMessage).show()
console.error(err)
})
});
action.validate = function(selection, sender){
return (selection.tasks.length > 0)
};
return action;
})();