Changing Canvas by script

Is there a way to switch canvas by script - either AppleScript or JavaScript?

Yes! While I believe it is possible in AppleScript as well, for JavaScript we have a Canvas Tools plug-in that may suit your needs.

See https://omni-automation.com/omnigraffle/canvas-00.html for more information on using Canvases in JavaScript.

Thanks for the swift reply! However looking at the list of functions, I canā€™t see how Iā€™d do this? I can see functions that change the order of canvases, and a function to get the current canvas - but how do I set the current canvas, that is get OG to switch to showing that canvas?

TIA,

You want to refer to the canvas by array index as described in https://omni-automation.com/omnigraffle/document-01.html.

For example, the first canvas is:

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

The second canvas down from the top is:

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

If you want to iterate over the canvases, you use logic like you would iterate over any array to get all of the canvases and act on them. The script Rename in Current Order in the Canvas Tools plug-in has a nice example.

Thanks for continuing to assist - I think that Iā€™m not being very clear.

For example, the Actions property of an object include ā€œSwitch to the next canvasā€. Iā€™m not trying to change the properties of any of the canvases, or their order in the document; I just want OG to switch to showing a different canvas (especially in Presentation mode) just as it does when I use the up or down arrows, or one of those Actions.

Is that possible?

Many thanks,

You can add an action that calls a script if you like. There is a built in ā€œJump To Canvasā€ action in OmniGraffle, so you donā€™t need a script to jump between canvases unless you want something more specific than the 6 options we provide. Select the shape, in the Properties Inspector under Action, set ā€œJump to Canvasā€ from the Drop Down menu. Then you pick which canvas to jump to. To do the jumping, use the Action tool or go into presentation mode

If you select an object in OmniGraffle Pro, in the Properties inspector you can set an action to AppleScript or JavaScript, and then change to that canvas by clicking on that object with the action tool (Or in presentation mode). If you do need a custom scripting action, try setting the Current View to the canvas that you want in your script.

Aha! Thanks Lanette - thatā€™s exactly what I wanted. Sorry to be so dense, I didnā€™t realise the distinction between the order of canvases in the document, and the order of canvases in the window.

(What Iā€™m trying to do is have an object with an action that includes first jumping to another canvas, and then hiding and showing layers on that canvas - I can do either with the built-in actions, but to do both requires a script.)

Thanks for persisting when I didnā€™t get it!

Hi, just getting familiar with automation in omni and wondered if thereā€™s any reason the above wouldnā€™t work anymore? The majority of the examples as well as sals tools/og tools - on omni-automation.com donā€™t work and typically fall over when referencing the canvas. Returns an ā€˜undefinedā€™ error. What am I doing wrong?

ā€˜copy asā€™ code from a shape works, just not the above. All Iā€™m trying to do is loop through the canvas to copy text.

See https://omni-automation.com/omnigraffle/big-picture.html for the examples of the correct and incorrect way to reference a canvas. Keep in mind, this changes the canvas showing in the view (center), but it may work so fast that you canā€™t see it. Undefined isnā€™t actually neccisarily an error. Unless you define a value for your variable or function to return, undefined is the default. The script below will go through each canvas leaving you viewing the last canvas in the view of the frontmost window.

canvases.forEach(function(cs){
   document.windows[0].selection.view.canvas = cs;
})

Thanks,
Lanette

It seems that referencing the canvas is the problem. I get the following error ā€˜ReferenceError: Canā€™t find variable: canvases undefined:1ā€™ whenever I try to reference something, even when copying the code above.

Do you have a document open with multiple canvases when running this? Are you running it from the console inside OmniGraffle? Can you check which version of macOS (Apple menu->About This Mac) and OmniGraffle (OmniGraffle>About OmniGraffle) you are running so that I can make sure I am testing the same thing that you are? When I run the script above inside the OmniGraffle Automation Console, I see page 2 of the frontmost document and if I add a graphic, it is added to the last canvas in the document. Iā€™ve tested this in multiple documents in OmniGraffle Pro 7.9.4 with both macOS 10.13.6 and 10.14 including the most recent beta. If these versions match what you are using, please contact support so that we can troubleshoot this with you. Please email us at omnigraffle@omnigroup.com.

Looks like it hasnā€™t been updated since 7.7 and ā€˜check for updatesā€™ was turned off. It works now! Thanks

Awesome! We keep adding more OmniJS support in most versions, so you may have been missing many updates. Glad you solved the mystery.

Hi All,

Not sure this is the correct place for this but here goes. Please advise if there is a more appropriate place.

I have been able to create reasonably complex diagrams with Omnigraffle. Love it. Have somewhat antiquated programming knowledge. Have been able to use the automation console to create a multi-canvas document. Wanted to create functions because much of the diagrams are just reiterating the same graphic several times on multiple canvases. Started to create canvas borders on each canvas for document title, info, etc. Each of the canvases are created but when I go to create the specific page format graphics, they all appear on the canvases[0] canvas.

The `canvas indexing thing isnā€™t working for me.
var cnvs = document.windows[0].selection.canvas
var cnvs = document.windows[1].selection.canvas

Looking for guidance on reference manuals to understand whatā€™s a available so that I can start to build an understanding of how to create docs with Omni Automation. Forums are great but Iā€™m having trouble finding insights specifically for what Iā€™m trying to do.

Thoughts / input?

Thank you in advance for reading through this. Cool stuff!!! Thanks.

Iā€™m afraid thereā€™s not much too much reference available. Itā€™s kinda lame thereā€™s no visual representation of the document model to be found anywhere.

What youā€™re looking for is probably

var cnvs = document.portfolio.canvases[0]

Hey bboc,

Thank you very much. That did the trickā€¦ very much appreciated.

Take care,
Greg

Hi Greg,

There is a visual representation of the document model at https://omni-automation.com/omnigraffle/big-picture.html and more details there that may help with scripting OmniGraffle.

Thanks,
Lanette

1 Like

Thank you Lanette. The internal constructs are a bit elusive for me now having little experience. Drawing stuff and copy as javascript are helping to see some of the internal constructs and reading through the forums are helping but havenā€™t stumbled onto a place where all of the internal constructs are defined has happened for me yet. Is there a good place for me to start that you would recommend? Thank you.

Iā€™d recommend starting with the OmniGraffle Tutorial and using the API references which is either in the Automation menu, or at https://omni-automation.com/omnigraffle/og-oa.html. I like the online version because itā€™s easy to check out while Iā€™m using the console.

We have a growing community of JavaScript users automating in OmniGraffle now, so if you run into a problem, try posting in the forums to see if any of us can help.

Thanks,
Lanette