Layers

A canvas contains one or more layers, upon which are located the canvas’ elements, such as graphics or images.

Layers are created using the newLayer() instance method of the Canvas class.

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Alayer%20%3D%20cnvs%2EnewLayer%28%29%0Alayer%2Ename%20%3D%20%22NEW%20LAYER%22
Add a New Layer
 

cnvs = document.windows[0].selection.canvas layer = cnvs.newLayer() layer.name = "NEW LAYER"

By default, new layers are created at the top of the stack of existing layers.

Here’s a script that demonstrates the creation of a canvas, layers, and shapes:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20addCanvas%28%29%0Acnvs%2EcanvasSizingMode%20%3D%20CanvasSizingMode%2EFixed%0Acnvs%2Esize%20%3D%20new%20Size%28288%2C%20288%29%0A%0Alayer%20%3D%20cnvs%2Elayers%5B0%5D%0Agraphic%20%3D%20layer%2EaddShape%28%27Rectangle%27%2C%20new%20Rect%280%2C%200%2C%20144%2C%20144%29%29%0Agraphic%2EfillColor%20%3D%20Color%2ERGB%280%2E5%2C%200%2E5%2C%201%2C%201%29%0Agraphic%2EshadowColor%20%3D%20null%0Agraphic%2Ename%20%3D%20%27blue%27%0A%0Alayer%20%3D%20cnvs%2EnewLayer%28%29%0Agraphic%20%3D%20layer%2EaddShape%28%27Rectangle%27%2C%20new%20Rect%28144%2C%200%2C%20144%2C%20144%29%29%0Agraphic%2EfillColor%20%3D%20Color%2ERGB%281%2C%200%2E5%2C%200%2E5%2C%201%29%0Agraphic%2EshadowColor%20%3D%20null%0Agraphic%2Ename%20%3D%20%27red%27%0A%0Alayer%20%3D%20cnvs%2EnewLayer%28%29%0Agraphic%20%3D%20layer%2EaddShape%28%27Rectangle%27%2C%20new%20Rect%28144%2C%20144%2C%20144%2C%20144%29%29%0Agraphic%2EfillColor%20%3D%20Color%2ERGB%281%2C%200%2E5%2C%201%2C%201%29%0Agraphic%2EshadowColor%20%3D%20null%0Agraphic%2Ename%20%3D%20%27magenta%27%0A%0Alayer%20%3D%20cnvs%2EnewLayer%28%29%0Agraphic%20%3D%20layer%2EaddShape%28%27Rectangle%27%2C%20new%20Rect%280%2C%20144%2C%20144%2C%20144%29%29%0Agraphic%2EfillColor%20%3D%20Color%2ERGB%280%2E5%2C%201%2C%200%2E5%2C%201%29%0Agraphic%2EshadowColor%20%3D%20null%0Agraphic%2Ename%20%3D%20%27green%27
Create Canvas with Layers and Shapes
 

cnvs = addCanvas() cnvs.canvasSizingMode = CanvasSizingMode.Fixed cnvs.size = new Size(288, 288) layr = cnvs.layers[0] graphk = layr.addShape('Rectangle', new Rect(0, 0, 144, 144)) graphk.fillColor = Color.RGB(0.5, 0.5, 1, 1) graphk.shadowColor = null graphk.name = 'blue' layr = cnvs.newLayer() graphk = layr.addShape('Rectangle', new Rect(144, 0, 144, 144)) graphk.fillColor = Color.RGB(1, 0.5, 0.5, 1) graphk.shadowColor = null graphk.name = 'red' layr = cnvs.newLayer() graphk = layr.addShape('Rectangle', new Rect(144, 144, 144, 144)) graphk.fillColor = Color.RGB(1, 0.5, 1, 1) graphk.shadowColor = null graphk.name = 'magenta' layr = cnvs.newLayer() graphk = layr.addShape('Rectangle', new Rect(0, 144, 144, 144)) graphk.fillColor = Color.RGB(0.5, 1, 0.5, 1) graphk.shadowColor = null graphk.name = 'green'

Note the use of the variable names: cnvs, layr, graphk, and graphx that represent a Canvas object, a Layer object, a Graphic object, and multiple Graphic objects.. This variable naming convention is used throughout this website.

New canvas with layers cna color squares

Instance Properties

Here are the properties of a layer object:

Working with Properties

Script examples demonstrating the use of layer properties:

Names of Canvas Layers


cnvs = document.windows[0].selection.canvas layerNames = cnvs.layers.map(layr => layr.name)

And here’s a script that will hide all layers of the current canvas that do not contain at least one selected graphic:

omnigraffle://localhost/omnijs-run?script=sel%20%3D%20document%2Ewindows%5B0%5D%2Eselection%0Agraphx%20%3D%20sel%2Egraphics%0Aif%20%28graphx%2Elength%20%3E%200%29%7B%0A%09cnvs%20%3D%20sel%2Ecanvas%0A%09%2F%2F%20hide%20all%20layers%0A%09cnvs%2Elayers%2EforEach%28layer%20%3D%3E%20%7Blayer%2Evisible%20%3D%20false%7D%29%0A%09%2F%2F%20show%20layers%20that%20contain%20a%20selected%20graphic%0A%09graphx%2EforEach%28graphk%20%3D%3E%20%7Bgraphk%2Elayer%2Evisible%20%3D%20true%7D%29%0A%7D
Show Only Layers with Selected Graphics
   

sel = document.windows[0].selection graphx = sel.graphics if (graphx.length > 0){ cnvs = sel.canvas // hide all layers cnvs.layers.forEach(layr => {layr.visible = false}) // show layers that contain a selected graphic graphx.forEach(graphk => {graphk.layer.visible = true}) }

And here’s a script for making sure that all layers of the current canvas are visible:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Acnvs%2Elayers%2EforEach%28layr%20%3D%3E%20%7Blayr%2Evisible%20%3D%20true%7D%29
Show All Layers
   

cnvs = document.windows[0].selection.canvas cnvs.layers.forEach(layr => {layr.visible = true})
Video 1: Working with Layer Properties
Using plug-ins to hide and show layers based upon their selected graphics.

Instance Functions

The instance functions for a layer object include the ability to reorder or delete layers:

Performing Tasks with Layers

Here are script outlines for performing operations with each of the layers of the current canvas:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Afor%20%28layr%20of%20cnvs%2Elayers%29%7B%0A%09%2F%2Fprocessing%20code%0A%09console%2Elog%28layr%2Ename%29%0A%7D
Using for…of Loop
 

cnvs = document.windows[0].selection.canvas for (layr of cnvs.layers){ //processing code console.log(layr.name) }
omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Acnvs%2Elayers%2EforEach%28layr%20%3D%3E%20%7B%0A%09%2F%2Fprocessing%20code%0A%09console%2Elog%28layr%2Ename%29%0A%7D%29
Using forEach() Function
 

cnvs = document.windows[0].selection.canvas cnvs.layers.forEach(layr => { //processing code console.log(layr.name) })

Using the script outline above to create a script that shows hidden layers of the current canvas:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Afor%20%28layr%20of%20cnvs%2Elayers%29%7B%0A%09if%20%28layr%2Evisible%20%3D%3D%20false%29%7Blayr%2Evisible%20%3D%20true%7D%0A%7D
Show Hidden Layers
 

cnvs = document.windows[0].selection.canvas for (layr of cnvs.layers){ if (layr.visible === false){layr.visible = true} }

Here’s a script that deletes the hidden layers of the current canvas. NOTE: The remove() function deletes layers regardless of the status of their locked property.

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Afor%20%28layr%20of%20cnvs%2Elayers%29%7B%0A%09if%20%28layr%2Evisible%20%3D%3D%3D%20false%29%7Blayr%2Eremove%28%29%7D%0A%7D
Delete Hidden Layers
 

cnvs = document.windows[0].selection.canvas for (layr of cnvs.layers){ if (layr.visible === false){layr.remove()} }

Getting the object reference to a layer by using the layer’s name:

Get Layer Reference by Name


function getLayerObjectOfCurrentCanvasByName(layerName){ var targetLayer cnvs = document.windows[0].selection.canvas for (layr of cnvs.layers){ if (layr.name.localeCompare(layerName) === 0){ targetLayer = layr break } } if (targetLayer){ return targetLayer } else { errStr = `There is no layer named “${layerName}” in the current canvas.` new Alert('MISSING OBJECT', errStr).show() return null } } getLayerObjectOfCurrentCanvasByName(layerName)

This script reorders the layers by name:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Alayrz%20%3D%20cnvs%2Elayers%0Alayrz%2Esort%28%28a%2C%20b%29%20%3D%3E%20%7B%0A%20%20var%20x%20%3D%20a%2Ename%3B%0A%20%20var%20y%20%3D%20b%2Ename%3B%0A%20%20if%20%28x%20%3C%20y%29%20%7Breturn%20%2D1%3B%7D%0A%20%20if%20%28x%20%3E%20y%29%20%7Breturn%201%3B%7D%0A%20%20return%200%3B%0A%7D%29%0Alayrz%2Ereverse%28%29%2EforEach%28layr%20%3D%3E%20%7B%0A%09topLayr%20%3D%20cnvs%2Elayers%5B0%5D%0A%09if%28layr%20%21%3D%3D%20topLayr%29%7B%0A%09%09layr%2EorderAbove%28topLayr%29%20%0A%09%7D%0A%7D%29
Reorder Layers by Name
 

cnvs = document.windows[0].selection.canvas layrz = cnvs.layers layrz.sort((a, b) => { var x = a.name; var y = b.name; if (x < y) {return -1;} if (x > y) {return 1;} return 0; }) layrz.reverse().forEach(layr => { topLayr = cnvs.layers[0] if(layr !== topLayr){ layr.orderAbove(topLayr) } })

NOTE: the document window view may need to be refreshed in order to display the reordered canvases correctly.

Reordered Layers

Moving Objects to Other Layers

Currently, the scripting implementation in OmniGraffle does not offer commands for merging layers or for moving objects between layers. However, because references ot objects are based on the unique value of their id property, you can use the orderAbove() and orderBelow() methods from the Graphic class to move objects between the layers of a canvas.

For example, in the OmniGraffle document pictured below, there are three objects, each on its own layer. You can use the orderAbove() method to move them to the top layer while maintaining their stacking relationship.

Mutlple layered document
Moving Objects Between Layers: orderAbove()


cnvs = document.windows[0].selection.canvas g0 = cnvs.layers[0].graphics[0] // diamond g1 = cnvs.layers[1].graphics[0] // circle g2 = cnvs.layers[2].graphics[0] // square g2.orderAbove(g0) // move square above diamond g1.orderAbove(g2) // move circle above square g0.orderAbove(g1) // move diamond above circle
Mutlple layered document

Alternatively, you can use the orderBelow() method to move the three objects to the bottom layer while maintaining their stacking relationship.

Moving Objects Between Layers: orderBelow()


cnvs = document.windows[0].selection.canvas g0 = cnvs.layers[0].graphics[0] // diamond g1 = cnvs.layers[1].graphics[0] // circle g2 = cnvs.layers[2].graphics[0] // square g1.orderBelow(g2) // move circle below square g0.orderBelow(g2) // move diamond below sqaure g2.orderBelow(g1) // move square below circle
Mutlple layered document