OmniGraffle: Windows
The window class has a few properties:
selection • An instance of the Selection class, representing the items selected in the document
centerVisiblePoint • The center point of the window document display area
zoom • A number indicating the percentage of the magnification of the canvas display. 1 is equivalent to 100%, 2 is equivalent to 200%, 0.75 is equivalent to 75%.
And two instance functions:
close() • When called, this function will close the targeted window.
setViewForCanvas(canvas:Canvas, zoom:Number, center:Point) • Change the saved position for a canvas, so that this will be the view position and scale shown when that canvas is next selected.
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%
Set the Window’s Display Percentage to 100% | ||
01 | document.windows[0].zoom = 1.0 |
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:
Center Canvas in Window | ||
01 | cnvs = document.windows[0].selection.canvas | |
02 | if(cnvs.canvasSizingMode = CanvasSizingMode.Fixed){ | |
03 | document.windows[0].centerVisiblePoint = cnvs.background.geometry.center | |
04 | } |
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:
Center Display of Selected Graphic | ||
01 | graphic = document.windows[0].selection.solids[0] | |
02 | document.windows[0].centerVisiblePoint = graphic.geometry.center |
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.
Get the Selection | ||
01 | var sel = document.windows[0].selection | |
02 | ==> [object 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:
Document of the Selection | ||
01 | 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.
Current Canvas | ||
01 | 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.
View of the Selection | ||
01 | 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.
The Visible Rect of the View | ||
01 | 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.
Select View Graphics | ||
01 | 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 | ||
01 | 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.
Edit Text of Solid | ||
01 | 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:
Select All Graphics on the Current Canvas | ||
01 | vw = document.windows[0].selection.view | |
02 | cnvs = document.windows[0].selection.canvas | |
03 | vw.select(cnvs.graphics) |
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:
Selected Graphics | ||
01 | var grphs = document.windows[0].selection.graphics |
Loop Selected Graphics | ||
01 | var grphs = document.windows[0].selection.graphics | |
02 | for(i = 0; i < grphs.length; i++){ | |
03 | grphs[i]. | |
04 | } |
Selected lines:
Selected Lines | ||
01 | var lns = document.windows[0].selection.lines |
Loop Selected Lines | ||
01 | var lns = document.windows[0].selection.lines | |
02 | for(i = 0; i < lns.length; i++){ | |
03 | lns[i]. | |
04 | } |
Selected solids:
Selected Solids | ||
01 | var slds = document.windows[0].selection.solids |
Loop Selected Solids | ||
01 | var slds = document.windows[0].selection.solids | |
02 | for(i = 0; i < slds.length; i++){ | |
03 | slds[i]. | |
04 | } |
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.
Zoom Canvas to Fit | ||
01 | w = document.windows[0] | |
02 | w.zoom = 1 // set zoom to 100% | |
03 | viewSize = w.selection.view.visibleRect.size | |
04 | cnvs = w.selection.canvas | |
05 | cnvsSize = cnvs.size | |
06 | if (cnvs.size.width > cnvs.size.height){ // canvas is landscape | |
07 | if (viewSize.width > viewSize.height){ | |
08 | zoomFactor = viewSize.width / cnvs.size.width | |
09 | } else { | |
10 | zoomFactor = viewSize.width / cnvs.size.width | |
11 | } | |
12 | } else { // canvas is portrait | |
13 | if (viewSize.height > viewSize.width){ | |
14 | zoomFactor = viewSize.width / cnvs.size.width | |
15 | } else { | |
16 | zoomFactor = viewSize.height / cnvs.size.height | |
17 | } | |
18 | } | |
19 | zoomFactor = zoomFactor * 0.95 // reduce 5% | |
20 | w.setViewForCanvas(cnvs, zoomFactor, cnvs.background.geometry.center) | |
21 | w.centerVisiblePoint = cnvs.background.geometry.center |
This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.