Get the page # of a canvas in AppleScript - How to?

I’ve been having a devil of a time with a TOC script. The TOC is set to count canvases, but some of my canvases actually contain multiple pages (on the same canvas).

Is there something I can call in AppleScript that will give me the page # of the canvas. Similar to how I have <%#%> on the page and it tells me the page number. I’ve hunted around and haven’t been able to find anything.

Seems so obvious that name returns the name of the canvas. But for the page number… I’m totally lost.

I assume that by “page # of the canvas,” you mean the page number of the first page in the canvas. That would make sense for a ToC.

Two things help here:

  1. A canvas has horizontal pages and vertical pages properties; these give its width and height in page count. The product of those two is the number of pages in the canvas,
  2. The order of canvases in a document is reflected in the order of canvases in the document’s canvas list.

This means that you can loop through a document’s canvas list, keeping track of the total number of pages, until you get to a specific canvas. Add one to the number of pages before it and you get its page number.

Here’s a short demo script (no error handling):

tell application id "com.omnigroup.OmniGraffle6"
tell first document
     display dialog "Find the page number of canvas number:" default answer "1"
     set itemNumber to text returned of result as integer
     set theCanvas to a reference to item itemNumber of canvases
     set pageCount to 0
     repeat with currentCanvas in canvases
        if currentCanvas is theCanvas then
           return pageCount + 1
        else
           set pageCount to pageCount + (vertical pages of currentCanvas) * (horizontal pages of currentCanvas)
        end if
     end repeat
  end tell
  end tell