OmniFocus: Get Selected Project (template)
The actions of the following shortcut can be used to return an object reference to the currently selected project. These actions can be copied and pasted into your shortcut.
Get Selected Project
try {
var sel = selection.projects
if(sel.length === 1){
var selectedItem = sel[0]
} else {
throw {
name: "Selection Issue",
message: "Please select a single project."
}
}
var itemProperties = new Object()
itemProperties["primaryKey"] = selectedItem.id.primaryKey
itemProperties["type"] = "project"
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.projects
if(sel.length === 1){
var selectedItem = sel[0]
} else {
throw {
name: "Selection Issue",
message: "Please select a single project."
}
}
// 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}`
}
})();