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.


#14

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.


#15

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]

#16

Hey bboc,

Thank you very much. That did the trick… very much appreciated.

Take care,
Greg


#17

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


#18

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.


#19

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