Changing Canvas by script


#1

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


#2

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.


#3

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,


#4

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.


#5

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,


#6

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.


#7

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!


#8

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.


#9

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


#10

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.


#11

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.


#12

Looks like it hasn’t been updated since 7.7 and ‘check for updates’ was turned off. It works now! Thanks


#13

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