The properties of the Selection class provide scripting access to the objects that are currently selected in the application interface. These objects include the following:
columns (Array of Columnsr/o) An array of references to the selected columns
document (OutlineDocument or nilr/o) A reference to the parent outline document
editor (Editor or nilr/o) The Editor that contains the selection, or null.
items (Array of Itemr/o) An array of references to the selected items (rows)
nodes (Array of ItemTreeNoder/o) The ItemTreeNode instances for the selected rows.
outline (Outline or nilr/o) The Outline that contains the selection, or null.
styles (Array of Stylesr/o) An array of the styles used by the selected objects.
Accessing the Selection
In OmniGraffle, the selection is a property of the Window class. In OmniOutliner, the selection is a property of the Editor class, and so must be accessed accordingly, appeneded to a statement ending with an instance of the Editor class, usually the first or current editor:
Getting the Selection
01
document.editors[0].selection
An instance of the Selection class contains references to the objects listed above, which are accessed by appending them to the selection instance:
The Selection Properties
01
document.editors[0].selection.columns
02
document.editors[0].selection.document
03
document.editors[0].selection.editor
04
document.editors[0].selection.items
05
document.editors[0].selection.nodes
06
document.editors[0].selection.outline
07
document.editors[0].selection.styles
Using these selection objects, scripts can provide functionality targeted at the objects selected by the user.
Examples
Here’s the basic script for processing selected items (rows):
var items = document.editors.selection.items
items.forEach(function(item){
// processing statements go here
})
Process Selected Items
01
varitems = document.editors.selection.items
02
items.forEach(function(item){
03
// processing statements go here
04
})
and here’s an expanded version of the previous script that converts the notes of selected items into follow-on rows (siblings):
var items = document.editors[0].selection.items
items.forEach(function(item){
var itemNote = item.note
var itemParent = item.parent
itemParent.addChild(
item.after,
function(row){
row.topic = itemNote
}
)
item.note = ""
})
The following script uses the columns property of the selection instance to adjust the widths of the selected columns to be the arithmetic mean (average) of their widths:
var editor = document.editors[0]
var selectedColumns = editor.selection.columns
var columnCount = selectedColumns.length
if (columnCount === 1){throw new Error('single column')}
var widths = selectedColumns.map(function(col){
return editor.widthForColumn(col)
})
var total = widths.reduce(
function(total,num){return total + num},
0)
var avgWidth = total/columnCount
avgWidth = Number(avgWidth.toFixed(0))
selectedColumns.forEach(
function(col){editor.setWidthForColumn(col,avgWidth)}
)
/*{
"type": "action",
"targets": ["omnioutliner"],
"author": "Otto Automator",
"identifier": "com.omni-automation.oo.balance-columns",
"version": "1.0",
"description": "Adjust the width of selected columns to be the same.",
"label": "Balance Widths of Selected Columns",
"shortLabel": "Balance Column Widths"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender) {
// action code
// selection options: columns, document, editor, items, nodes, styles
var selectedColumns = selection.columns
var editor = selection.editor
var columnCount = selectedColumns.length
var widths = selectedColumns.map(col => {
return editor.widthForColumn(col)
})
var total = widths.reduce(function(total,num){return total + num},0)
var avgWidth = total/columnCount
avgWidth = Number(avgWidth.toFixed(0))
selectedColumns.forEach(
function(col){editor.setWidthForColumn(col,avgWidth)}
)
});
action.validate = function(selection, sender) {
// validation code
// selection options: columns, document, editor, items, nodes, styles
return (selection.columns.length > 1)
};
return action;
})();