OmniGraffle: Stencils

A stencil is a reusable shape that can be dragged to the canvas from the Stencils window. Stencils can be as simple as a square or triangle, or as complex as a multilayered and meticulously designed illustration. Each object or object group in an OmniGraffle stencil file becomes an individual stencil.

TIP: As a service to OmniGraffle users, the Omni Group hosts STENCILTOWN, a website from which you can download and install stencil files containing very useful collections of stencil graphics.

Stencil Properties

The Stencil Object (stencil file)

In scripting parlance, a stencil refers to a stencil file, and it is an element of the application class. A stencil is a container holding a defined set of graphics you can add to your OmniGraffle document.

In Omni Automation, the OmniGraffle stencil class has a property (name) and elements (graphics).

Determining how many stencils (stencil files) are installed:

Getting the name of a stencil specified by index:

Call the function below to retrieve the names of all installed stencils:

function getStencilNames(){ var stencilNames = [] app.stencils.forEach(function(aStencil){ stencilNames.push(aStencil.name) }) return stencilNames }
omnigraffle:///omnijs-run?script=function%20getStencilNames()%7B%0A%09var%20stencilNames%20%3D%20%5B%5D%0A%09app.stencils.forEach(function(aStencil)%7B%0A%09%09stencilNames.push(aStencil.name)%0A%09%7D)%0A%09return%20stencilNames%0A%7D%0AgetStencilNames()

The result of calling the function above in the OmniGraffle console window (see below) will be a list of the names of the installed stencils:

“Recents”, “Connections”, “Shapes”, “UX Kits Colors”, “Solarized Dark”, “Solarized Light”, “UX Kits UML”, “UX Kits Connections”, “Square Foot Gardening”, “Modern Landscape”, “Flowers”, “Reference”, “Africa”, “Colors”, “Fills”, “USA”, “Periodic Table”, “Rounded Corners”, “Mac OS X Wireframes”, “Magnetized”, “FlowChart”, “Furniture”, “Konigi Wireframes”, “UML-State”, “Antarctica”, “Asia Pacific”, “Russia”, “Middle East”, “Plumbing Fixtures”, “Walls, Windows & Doors”, “North America”, “Boolean Gates”, “Circuit Engineering”, “Connections”, “Appliances”, “UML-UseCase”, “UML-General”, “Fonts”, “South America”, “UML-Sequence”, “Europe”, “Turkey”, “Russia”, “Ukraine”, “Garrett IA”, “Three-Dimensional”, “ERD”, “iOS Wireframes”, “Shapes”, “Apple watchOS Suite”, “The iOS 9 Suite”, “UX Kits UML”, “Google Material Design Icons”, “UX Kits Hardware”

change-color-of-selection

Another way to access the names of the installed stencils, is to use the JavaScript map method. This method is particularly useful and efficient in retrieving object properties:

stencilNames = app.stencils.map(function(aStencil){return aStencil.name})

The Stencil Object

Call either of the example functions below to retrieve the object reference for a stencil identified by name:

function getStencilObjectByName(stencilName){ var targetStencil app.stencils.forEach(function(aStencil){ if (aStencil.name.localeCompare(stencilName) == 0){ targetStencil = aStencil } }) if (targetStencil instanceof Stencil){ return targetStencil } else { errorString = "There is no stencil named: " + stencilName new Alert('MISSING OBJECT', errorString).show(function(result){}) } }
// returns a stencil object whose name matches the passed value function getStencilObjectByName(stencilName){ for(i = 0; i < app.stencils.length; i++){ if (app.stencils[i].name.localeCompare(stencilName) == 0){ return app.stencils[i] } } errorString = "There is no stencil named: " + stencilName new Alert('ERROR', errorString).show(function(result){}) throw new Error(errorString) }

To enable visual error reporting, uncomment the line containing the alert command (09) to have the function display a dialog if no matching stencil can be located:

alert-dialog-01

Example Script

Here is an example script that will create a new text shape containing the names of the installed stencils. The script uses the JavaScript map(), join(), and sort() methods, as well as the Omni Automation newShape() method.

names = app.stencils.map(function(aStencil){return aStencil.name}) names = names.sort() cnvs = document.windows[0].selection.canvas var shape = cnvs.newShape() shape.autosizing = TextAutosizing.Full shape.textHorizontalAlignment = HorizontalTextAlignment.Left shape.text = names.join('\n')
omnigraffle:///omnijs-run?script=names%20%3D%20app%2Estencils%2Emap%28function%28aStencil%29%7Breturn%20aStencil%2Ename%7D%29%0Anames%20%3D%20names%2Esort%28%29%0Acnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Avar%20shape%20%3D%20cnvs%2EnewShape%28%29%0Ashape%2Eautosizing%20%3D%20TextAutosizing%2EFull%0Ashape%2EtextHorizontalAlignment%20%3D%20HorizontalTextAlignment%2ELeft%0Ashape%2Etext%20%3D%20names%2Ejoin%28%27%5Cn%27%29
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER