Graphic

A graphic is an individual graphic element on a canvas. Graphic is an abstract superclass (that is, no actual Graphic objects exist, only more specialized sub-types of Graphic such as Solid and Line).

A Solid graphic is one that potentially has a fill, image, and text - as opposed to a Line, which has only a stroke. Almost all Solid graphics will actually be the subclass Shape, but a canvas background is a Solid without being a Shape.

A Shape is a “Solid” graphic which has a particular shape, either one of the built-in shapes (such as Circle), or a custom bezier shape.

A Line is a graphic which is a vector, potentially connecting two other graphics at its head and tail ends.

So to summarize: an instance of the Graphic class can be a Solid or a Line.

NOTE: The Graphic class also includes specialized sub-classes of Group, Subgraph, and Table, all of which will be covered in the Graphic section of the website.

Getting Graphics

The graphics property of the Canvas class has a value that is an array of references to every instance of the Graphic class on the canvas.

Here is a script for creating an array of references to all graphics on the current canvas:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Agraphx%20%3D%20cnvs%2Egraphics%0Aconsole%2Elog%28graphx%29
Get All Graphics on Current Canvas
 

cnvs = document.windows[0].selection.canvas graphx = cnvs.graphics console.log(graphx) //--> [object Shape: blue],[object Shape: red],[object Shape: magenta],[object Shape: green]

Graphic Instance Functions

All graphics share common functions for duplicating, deleting, and reordering their position in the stack of graphics on a canvas. Here are script examples of each function:

Scripts using the ordering functions:

omnigraffle://localhost/omnijs-run?script=graphx%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Egraphics%0Aif%28graphx%2Elength%20%3E%201%29%7B%0A%09bottomGraphic%20%3D%20graphx%5Bgraphx%2Elength%2D1%5D%0A%09bottomGraphic%2EorderAbove%28graphx%5B0%5D%29%0A%7D
Move Bottom Graphic to the Top
 

graphx = document.windows[0].selection.canvas.graphics if(graphx.length > 1){ bottomGraphic = graphx[graphx.length-1] bottomGraphic.orderAbove(graphx[0]) }

NOTE: The orderAbove() and orderBelow() methods will move graphics between layers. See the section on Layers for more information.

omnigraffle://localhost/omnijs-run?script=graphx%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Egraphics%0Aif%28graphx%2Elength%20%3E%201%29%7B%0A%09bottomGraphic%20%3D%20graphx%5Bgraphx%2Elength%2D1%5D%0A%09graphx%5B0%5D%2EorderBelow%28bottomGraphic%29%0A%7D
Move Top Graphic to the Bottom
 

graphx = document.windows[0].selection.canvas.graphics if(graphx.length > 1){ bottomGraphic = graphx[graphx.length-1] graphx[0].orderBelow(bottomGraphic) }

Using the remove() function:

omnigraffle://localhost/omnijs-run?script=graphx%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Egraphics%0Agraphx%20%3D%20graphx%2Ereverse%28%29%0Afor%28i%20%3D%200%3B%20i%20%3C%20graphx%2Elength%20%2D%201%3B%20i%2B%2B%29%7B%0A%09graphx%5Bi%5D%2Eremove%28%29%0A%7D
Delete all but the Top Graphic
 

graphx = document.windows[0].selection.canvas.graphics graphx = graphx.reverse() for(i = 0; i < graphx.length - 1; i++){ graphx[i].remove() }

Using the duplicateTo(…) function:

omnigraffle://localhost/omnijs-run?script=if%20%28document%2Ewindows%5B0%5D%2Eselection%2Egraphics%2Elength%20%3D%3D%3D%201%29%7B%0A%09graphk%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Egraphics%5B0%5D%0A%09geo%20%3D%20graphk%2Egeometry%0A%09graphk%2EduplicateTo%28new%20Point%28geo%2EmaxX%2C%20geo%2EminY%29%29%0A%7D
Duplicate Selected Graphic to the Right
 

if (document.windows[0].selection.graphics.length === 1){ graphk = document.windows[0].selection.graphics[0] geo = graphk.geometry graphk.duplicateTo(new Point(geo.maxX, geo.minY)) }

To duplicate to the same layer as the copied graphic, use the orderBelow(…) function:

omnigraffle://localhost/omnijs-run?script=if%20%28document%2Ewindows%5B0%5D%2Eselection%2Egraphics%2Elength%20%3D%3D%3D%201%29%7B%0A%09graphk%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Egraphics%5B0%5D%0A%09geo%20%3D%20graphk%2Egeometry%0A%09newGraphk%20%3D%20graphk%2EduplicateTo%28new%20Point%28geo%2EmaxX%2C%20geo%2EminY%29%29%0A%09newGraphk%2EorderBelow%28graphk%29%0A%7D
Duplicate Selected Graphic to the Right (Same Layer)
 

if (document.windows[0].selection.graphics.length === 1){ graphk = document.windows[0].selection.graphics[0] geo = graphk.geometry newGraphk = graphk.duplicateTo(new Point(geo.maxX, geo.minY)) newGraphk.orderBelow(graphk) }

Ordering graphics by name:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Agraphx%20%3D%20cnvs%2Egraphics%0Agraphx%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%0Agraphx%2Ereverse%28%29%2EforEach%28graphk%20%3D%3E%20%7B%0A%09topGraphk%20%3D%20cnvs%2Egraphics%5B0%5D%0A%09if%28graphk%20%21%3D%3D%20topGraphk%29%7B%0A%09%09graphk%2EorderAbove%28topGraphk%29%20%0A%09%7D%0A%7D%29
Reorder Canvas Graphics by Name
 

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

To reorder only the graphics in a specific layer (like the top layer) use this technique:

omnigraffle://localhost/omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Alayr%20%3D%20cnvs%2Elayers%5B0%5D%0Agraphx%20%3D%20layr%2Egraphics%0Agraphx%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%0Agraphx%2Ereverse%28%29%2EforEach%28graphk%20%3D%3E%20%7B%0A%09topGraphk%20%3D%20layr%2Egraphics%5B0%5D%0A%09if%28graphk%20%21%3D%3D%20topGraphk%29%7B%0A%09%09graphk%2EorderAbove%28topGraphk%29%20%0A%09%7D%0A%7D%29
Reorder Graphics in the Topmost Layer by Name
 

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

Select and Deselect Graphics

The select(…) function changes the selection to a new array of graphics. If the extending parameter is true, then the previous selection is retained as well.

Select View Graphics


document.windows[0].selection.view.select([arrayOfGraphics], false)

The deselect(…) function removes any of the passed graphics from the current selection, leaving any other currently selected graphics still selected.

Deselect View Graphics


document.windows[0].selection.view.select([arrayOfGraphics], false)

To deselect all graphics, pass in an empty array as the value for the select(…) function:

omnigraffle://localhost/omnijs-run?script=document%2Ewindows%5B0%5D%2Eselection%2Eview%2Eselect%28%5B%5D%2C%20false%29
Deselect All Graphics
 

document.windows[0].selection.view.select([], false)

Checking for Single Selected Graphic

It is common to create scripts that require the user to have already selected a single graphic. Here is the outline of such a script:

omnigraffle://localhost/omnijs-run?script=%28async%20%28%29%20%3D%3E%20%7B%0A%09try%20%7B%0A%09%09graphx%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Egraphics%0A%09%09if%28graphx%2Elength%20%21%3D%3D%201%29%7B%0A%09%09%09throw%20%7B%0A%09%09%09%09name%3A%20%22Selection%20Issue%22%2C%0A%09%09%09%09message%3A%20%22Please%20select%20a%20single%20graphic%2E%22%0A%09%09%09%7D%0A%09%09%7D%0A%09%09graphk%20%3D%20graphx%5B0%5D%0A%0A%09%09%2F%2F%20STATEMENTS%20GO%20HERE%0A%0A%09%7D%0A%09catch%28err%29%7B%0A%09%09new%20Alert%28err%2Ename%2C%20err%2Emessage%29%2Eshow%28%29%0A%09%7D%0A%7D%29%28%29%3B
Check for Single Selected Graphic
 

(async () => { try { graphx = document.windows[0].selection.graphics if(graphx.length !== 1){ throw { name: "Selection Issue", message: "Please select a single graphic." } } graphk = graphx[0] // STATEMENTS GO HERE } catch(err){ new Alert(err.name, err.message).show() } })();