Script for Canvas Size and Fit in Window


#1

Hey all, can someone provide me guidance or a script that will cycle through all loaded Canvas’ and do the following to each:

  1. Set Canvas Size to Infinite
  2. Select ‘Fit in Window’

Thank you for any help!


#2

Check out the info at https://www.omni-automation.com/omnigraffle/canvas-00.html for a start and grab the Canvas Tools at https://www.omni-automation.com/plugins/docs/CanvasTools.omnigrafflejs.zip for some examples. setSizeModeOfEveryCanvasToFixed() will work for what you need with a few tweaks.

For Fit in Window, I’d set a shortcut in OmniGraffle>Keyboard Shortcuts and then use a System Event to press the key, & save the document at the end.

Hope this helps!

Thanks,

Lanette


#3

I know this is an older thread, but do you have some examples of how to use setSizeModeOfEveryCanvasToFixed() ? or specifically setSizeModeOfEveryCanvasToInfinite()? or is there a public API reference (PDF) or searchable? I tried using the API reference that is inside OmniGraffle but it doesn’t seem you can search for specific commands.


#4

Unfortunately, CanvasSizingMode is a read-only property, so you can’t set that via scripting. You can use canvases.ForEach to return an array showing you which canvases names are not in Infinite Canvas Mode, but you can’t set them all to what you want them to be in an existing document.

I don’t know why it isn’t possible to change the CanvasSizingMode properties currently. It seems like something reasonable to get as well as set. Please send us an email from Contact Omni in the Help menu and request it be editable and include the API reference request as a PDF. I’m able to search the API reference with Edit>Find when it is up. Is that not working for you? You can see an API reference in searchable HTML at https://omni-automation.com/omnigraffle/og-oa.html as well.

An example of how to iterate canvases is up at https://omni-automation.com/omnigraffle/canvas-00.html which is using the view:

var docView = document.windows[0].selection.view
canvases.forEach(function(cnvs){
	docView.canvas = cnvs
	console.log(cnvs.name)
})

Script to set the canvas zoom level to fit:

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

So you’ll want to loop the array of Canvases in each document, and then you can save them using document.save() and that should get you part of the way there. Sorry for the bad news about the CanvasSizingMode. You might consider using a template in order to work around that issue in new documents, but it will not help you with existing files unless we allow you to set that property in the scripting API.

Thanks,

Lanette