“Omni Automation Script” selection Parameter
Omni Automation plug-ins use the special parameter selection for representing the current document or application selection. This parameter is automatically passed into the plug-in action and validation handlers.
The various app-specific versions of the “Omni Automation Script” action employ a similar construct to provide access to the current Selection object to hosted script.
In the action’s script, the selection parameter can be used one or more times, anywhere within the script, and usually is appended with other terms to specify the type of objects to target. For example:
- OmniFocus: selection.projects • The currently selected projects.
- OmniGraffle: selection.canvas • The currently selected canvas.
- OmniOutliner: selection.items • The currently selected outline rows.
- OmniPlan: selection.resources • The currently selected project resources.
OmniGraffle • Set Fill Color of Selected Shapes
When used in an Omni Automation script targeting the OmniGraffle application, the selection parameter represents the Selection object generated by the script statement: document.windows[0].selection
In OmniGraffle, the types that can be appended to the selection parameter are: document, canvas, view, graphics, solids, and lines
Change Fill Color of Selected Solids
try {
var selectedItems = selection.solids
if(selectedItems.length === 0){
throw {
name: "Selection Issue",
message: "Please select one or more shapes."
}
}
var R = argument.input.red
var G = argument.input.green
var B = argument.input.blue
var A = argument.input.alpha
var color = Color.RGB(R,G, B, A)
selectedItems.forEach(item => item.fillColor = color)
}
catch(err){
var alertPromise = new Alert(err.name, err.message).show()
alertPromise.then(result => {throw new Error(-128)})
}
OmniOutliner • Mark Selected Rows as DRAFT
When used in an Omni Automation script targeting the OmniOutliner application, the selection parameter represents the Selection object generated by the script statement: document.editors[0].selection
In OmniOutliner, the types that can be appended to the selection parameter are: allObjects, columns, document, editor, items, nodes, outline, and styles
Mark Selected Rows as DRAFT
try {
selectedItems = selection.items
if(selectedItems.length === 0){
throw {
name: "Selection Issue",
message: "Please select one or more rows."
}
}
IDs = selectedItems.map(item => {
item.topic = argument.input + " • " + item.topic
return item.identifier
})
}
catch(err){
var alertPromise = new Alert(err.name, err.message).show()
alertPromise.then(result => {throw new Error(-128)})
}
OmniFocus • Add Task to Selected Project
When used in an Omni Automation script targeting the OmniFocus application, the selection parameter represents the Selection object generated by the script statement: document.windows[0].selection
In OmniFocus, the types that can be appended to the selection parameter are: tasks, projects, folders, tags, database, window, databaseObjects, and allObjects
Pass Reference to 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)})
}
And here’s a script that passes an array of IDs of the selected tasks:
Pass IDs of Selected Tasks
try {
var sel = selection.tasks
if(sel.length === 0){
throw {
name: "Selection Issue",
message: "Please select one or more tasks."
}
}
IDs = sel.map(item => item.id.primaryKey)
JSON.stringify(IDs)
}
catch(err){
var alertPromise = new Alert(err.name, err.message).show()
alertPromise.then(result => {throw new Error(-128)})
}
OmniPlan • Add Staff Resource to Selected Task from Contacts
When used in an Omni Automation script targeting the OmniGraffle application, the selection parameter represents the Selection object generated by the script statement: document.windows[0].selection
In OmniPlan, the types that can be appended to the selection parameter are: document, project, resources, tasks, and window
Here’s the script code used in the example shortcut:
Assign/Create Resource from Passed Dictionary
try {
var selectedItems = selection.tasks
if(selectedItems.length !== 1){
throw {
name: "Selection Issue",
message: "Please select a single task."
}
}
var fullname = argument.input.fullname
var email = argument.input.email
var rsc = actual.resourceNamed(fullname)
if(!rsc){
var rsc = actual.rootResource.addMember()
rsc.type = ResourceType.staff
rsc.name = fullname
rsc.email = email.toString()
}
selectedItems[0].addAssignment(rsc)
}
catch(err){
var alertPromise = new Alert(err.name, err.message).show()
alertPromise.then(result => {throw new Error(-128)})
}
Next…
The next section of this documentation demonstrates how to add file references to the “Omni Automation Script” action via the action’s Associated Files control.