OmniGraffle: Windows

The window class has a few properties:

And two instance functions:

Window: zoom

The percentage of the window display is indicated as a number with 1 representing 100%. For example: 0.75 = 75%, 0.25 = 25%, 2.1 = 210%

document.windows[0].zoom = 1.0
omnigraffle://localhost/omnijs-run?script=try%7Bdocument%2Ewindows%5B0%5D%2Ezoom%20%3D%201%2E0%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

TIP: to see how to both zoom and center the current canvas to fit in the window, see the example script at the bottom of this page

Window: centerVisiblePoint

The value of the centerVisiblePoint property is the coordinates of the center point of the window view. Keeping in mind that the center point of a shape is represented as a Rect, the window’s centerVisiblePoint property be used to center the current canvas (background shape) in the window:

cnvs = document.windows[0].selection.canvas if(cnvs.canvasSizingMode = CanvasSizingMode.Fixed){ document.windows[0].centerVisiblePoint = cnvs.background.geometry.center }
omnigraffle:///omnijs-run?script=cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Adocument%2Ewindows%5B0%5D%2EcenterVisiblePoint%20%3D%20cnvs%2Ebackground%2Egeometry%2Ecenter

NOTE: the previous script requires the canvas be set to a fixed size.

And here’s how to center the selected shape in the window:

graphic = document.windows[0].selection.solids[0] document.windows[0].centerVisiblePoint = graphic.geometry.center
omnigraffle://localhost/omnijs-run?script=graphic%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Esolids%5B0%5D%0Adocument%2Ewindows%5B0%5D%2EcenterVisiblePoint%20%3D%20graphic%2Egeometry%2Ecenter

OmniGraffle: Document Selection

Working with items selected by the user is a common task performed by scripts. In OmniGraffle, selected document items are identified as elements of the selection property of the frontmost instance of the window class.

var sel = document.windows[0].selection

The value of the selection property is a selection object that contains references to important elements of the document. The following examples show how to access references to these items.

Document of the Selection

A reference to the parent document of the selection can be accessed as a property of the selection object:

var doc = document.windows[0].selection.document

The Current Canvas

In an OmniGraffle document, there must always be at least one selected canvas. The topmost selected canvas is always considered the current canvas, even if multiple canvases are selected in the window.

var cnvs = document.windows[0].selection.canvas

The Current View

The value of the view property of the selection object is an instance of the GraphicView class representing the display of the current canvas of the front window.

var vw = document.windows[0].selection.view

The value of the visibleRect property of the canvas view is a Rect defining the view of the canvas in an OmniGraffle window.

document.windows[0].selection.view.visibleRect

The GraphicView class supports the select function for selecting graphics on the current canvas. Change the selection to a new array of graphics. If the extending parameter is true, then the previous selection is retained as well.

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.

document.windows[0].selection.view.deselect([arrayOfGraphics])

The edit function is used to select the text of solid, initiating the editing mode for the indicated solid.

document.windows[0].selection.view.edit(solidToEdit)

And here is another example of referencing the GraphicView class to select items, in this case, all graphics in the current canvas:

vw = document.windows[0].selection.view cnvs = document.windows[0].selection.canvas vw.select(cnvs.graphics)
omnigraffle://localhost/omnijs-run?script=try%7Bvw%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Eview%0Acnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Avw%2Eselect%28cnvs%2Egraphics%29%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D

Selected Objects

The value of the view property of the the selection object contains arrays of references to these selected objects: graphics, solids, and lines

Selected graphics:

var grphs = document.windows[0].selection.graphics
var grphs = document.windows[0].selection.graphics for(i = 0; i < grphs.length; i++){ grphs[i]. }

Selected lines:

var lns = document.windows[0].selection.lines
var lns = document.windows[0].selection.lines for(i = 0; i < lns.length; i++){ lns[i]. }

Selected solids:

var slds = document.windows[0].selection.solids
var slds = document.windows[0].selection.solids for(i = 0; i < slds.length; i++){ slds[i]. }

Window: setViewForCanvas(…) function and zoom property

Here’s a script that uses the zoom property and setViewForCanvas(…) method of the window class, with the visibleRect property of the view object of the selection class, to zoom the current canvas to fit and center in the current visible window view.

Optionally, you can install it as a plugin.

w = document.windows[0] w.zoom = 1 // set zoom to 100% viewSize = w.selection.view.visibleRect.size cnvs = w.selection.canvas cnvsSize = cnvs.size if (cnvs.size.width > cnvs.size.height){ // canvas is landscape if (viewSize.width > viewSize.height){ zoomFactor = viewSize.width / cnvs.size.width } else { zoomFactor = viewSize.width / cnvs.size.width } } else { // canvas is portrait if (viewSize.height > viewSize.width){ zoomFactor = viewSize.width / cnvs.size.width } else { zoomFactor = viewSize.height / cnvs.size.height } } zoomFactor = zoomFactor * 0.95 // reduce 5% w.setViewForCanvas(cnvs, zoomFactor, cnvs.background.geometry.center) w.centerVisiblePoint = cnvs.background.geometry.center
omnigraffle://localhost/omnijs-run?script=try%7Bw%20%3D%20document%2Ewindows%5B0%5D%0Aw%2Ezoom%20%3D%201%20%2F%2F%20set%20zoom%20to%20100%25%0AviewSize%20%3D%20w%2Eselection%2Eview%2EvisibleRect%2Esize%0Acnvs%20%3D%20w%2Eselection%2Ecanvas%0AcnvsSize%20%3D%20cnvs%2Esize%0Aif%20%28cnvs%2Esize%2Ewidth%20%3E%20cnvs%2Esize%2Eheight%29%7B%20%2F%2F%20canvas%20is%20landscape%0A%09if%20%28viewSize%2Ewidth%20%3E%20viewSize%2Eheight%29%7B%0A%09%09zoomFactor%20%3D%20viewSize%2Ewidth%20%2F%20cnvs%2Esize%2Ewidth%0A%09%7D%20else%20%7B%0A%09%09zoomFactor%20%3D%20viewSize%2Ewidth%20%2F%20cnvs%2Esize%2Ewidth%0A%09%7D%0A%7D%20else%20%7B%20%2F%2F%20canvas%20is%20portrait%0A%09if%20%28viewSize%2Eheight%20%3E%20viewSize%2Ewidth%29%7B%0A%09%09zoomFactor%20%3D%20viewSize%2Ewidth%20%2F%20cnvs%2Esize%2Ewidth%0A%09%7D%20else%20%7B%0A%09%09zoomFactor%20%3D%20viewSize%2Eheight%20%2F%20cnvs%2Esize%2Eheight%0A%09%7D%0A%7D%0AzoomFactor%20%3D%20zoomFactor%20%2A%200%2E95%20%2F%2F%20reduce%205%25%0Aw%2EsetViewForCanvas%28cnvs%2C%20zoomFactor%2C%20cnvs%2Ebackground%2Egeometry%2Ecenter%29%0Aw%2EcenterVisiblePoint%20%3D%20cnvs%2Ebackground%2Egeometry%2Ecenter%7Dcatch%28err%29%7Bconsole%2Elog%28err%29%7D
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