OmniFocus: Get Selected Task (template)
The actions of the following shortcut can be used to return an object reference to the currently selected task. These actions can be copied and pasted into your shortcut.
Get Selected Task
try {
var sel = selection.tasks
if(sel.length === 1){
var selectedItem = sel[0]
} else {
throw {
name: "Selection Issue",
message: "Please select a single task."
}
}
var itemProperties = new Object()
itemProperties["primaryKey"] = selectedItem.id.primaryKey
itemProperties["type"] = "task"
itemProperties["version"] = 1.0
JSON.stringify(itemProperties)
}
catch(err){
var alertPromise = new Alert(err.name, err.message).show()
alertPromise.then(result => {throw new Error(-128)})
}
Asynchronous Wrapper
The following version wraps the script in an asynchronous function that simplifies the display of forms and alerts.
Asynchronous Wrapper
(async () => {
try {
sel = selection.tasks
if(sel.length === 1){
var selectedItem = sel[0]
} else {
throw {
name: "Selection Issue",
message: "Please select a single task."
}
}
// SCRIPT STATEMENTS GO HERE
}
catch(err){
if(!err.message.includes("cancelled")){
await new Alert(err.name, err.message).show()
}
throw `${err.name}\n${err.message}`
}
})();