Script for Canvas Size and Fit in Window

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!

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

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.

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

I was wrong on this. The CanvasSizingMode API is read only, but there is a canvasSizingMode function in the Canvas API that we can use to set.

The easiest way to do what you want is to download the canvas tools plug-in and use the Copy Sizing Parameters to All when you have an infinite canvas selected.

There is a script that does something close to what you are looking for at https://omni-automation.com/plugins/docs/canvas-tools.html if you take the setting from the CanvasLib.js and under the function to //SET SIZING MODE TO FIXED.

Also, here’s a script that seems pretty close to doing what you asked for, but feel free to refactor it and share with us improvements you made.

w = document.windows[0]
w.zoom = 1 // set zoom to 100%
viewSize = w.selection.view.visibleRect.size
cnvs = w.selection.canvas
cnvsSize = cnvs.size
canvases.forEach(function (cnvs){
    cnvs.canvasSizingMode = CanvasSizingMode.Infinite;
    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
})

Hope this helps! Sorry I said it wasn’t possible before. It wasn’t possible for me until I found an example that showed me what I was doing wrong.

Thanks,
Lanette

1 Like

Thank you for your guidance on this! I ran into this error when trying to run the code you listed above:

I have installed the plugin you listed.

Did you edit the action, or just install it and try to run it? Sounds like one of the actions in your Plug-Ins folder doesn’t have the required metadata. Check what is in Automation>Plug-ins. You may need to Show Package Contents to see what is going on in there.

Check https://omni-automation.com/actions/forms-01.html and see if you can identify which one. Is there a Untitled Action-1 inside of the CanvasTools package? That will need to have the required metadata in order to work from the Automation menu.

The script I pasted above isn’t part of the CanvasTools library, so it would need to be made into an action when finished to run it from the toolbar. I only tested it from the Automation Console here.

Maybe I am approaching this wrong. I am doing the following steps:

  1. Automation > Show Console
  2. Add Action
  3. Give it a name, click ‘Add’, open in Xcode
  4. Pasting the code
  5. Close Xcode
  6. Click on ‘Untitled Action’

That is not enough to make the script an action. Use the action generator at https://omni-automation.com/ogac/ to add the needed metadata, and that should get things working for you.

Again, thank you. So I followed the instructions, watched the video, here is what I came up with. The video doesn’t specify what ‘Action Identifier’ is, So I just put our company name.

/*{
	"type": "action",
	"targets": ["omnigraffle"],
	"author": "Mr Smith",
	"identifier": "com.vmware.actionName",
	"description": "This script will move through each canvas and resize the canvas to 'fit in window'.",
	"label": "VMware Custom Scripts",
	"paletteLabel": "Resize All Canvas"
}*/
var _ = function(){
	var action = new PlugIn.Action(function(selection, sender){
		// action code
		// selection options: canvas, document, graphics, lines, solids, view
		shape = selection.solids[0]
        w = document.windows[0]
        w.zoom = 1 // set zoom to 100%
        viewSize = w.selection.view.visibleRect.size
        cnvs = w.selection.canvas
        cnvsSize = cnvs.size
        canvases.forEach(function (cnvs){
            cnvs.canvasSizingMode = CanvasSizingMode.Infinite;
            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
        })
	});

	action.validate = function(selection, sender){
		// validation code
		// selection options: canvas, document, graphics, lines, solids, view
		if(selection.solids.length === 1){return true} else {return false}
	};
	
	return action;
}();
_;

This menu option is greyed out in OmniGraffle, the file name is vmware_omni_scripts.omnijs. I noticed the other plugins end with ‘omnigrafflejs’. I don’t know how to get it into that format. I tried renaming and it errors.

I see the issue. You don’t need this part:

 action.validate = function(selection, sender){
    		// validation code
    		// selection options: canvas, document, graphics, lines, solids, view
    		if(selection.solids.length === 1){return true} else {return false}
    	};

Try instead

action.validate = function(selection, sender){
		return true
	};

I didn’t realize this, but the scripting actions won’t enable unless you have a document open, so you don’t need to manually verify. What you specified in that action was it would only work if solids were selected on canvas, but what you want is for the action to return true anytime a document is open, which it will do with that change.

Thanks,
Lanette

Here is what I came up with using the template. But my menu option isn’t showing for some reason still.

I basically cloned changeColorOfSelectedGraphics.strings and changeColorOfSelectedGraphics.js and adapted it, i think i might be on the right path? lol

I replaced the code with the new code you supplied, it kind of looks like its working. This video might give you an idea.

I think in my example I have used the cnvs variable for multiple things, and maybe that isn’t our intention.

Consider refactoring my example by splitting the reference to cnvs into 2 variables, one for zooming the canvas and the other for resizing it. Might be worth some thought. I’m out of time for now, but I hope this idea can help. I believe if you look at the console you’ll see an error about the use of the cnvs variable which I think is not what we intended.

Ok, thanks again! I’ll play around more with it.