Export all Canvases in OmniGraffle

Over the last few years I’ve created a number of useful diagrams in OmniGraffle that describe some technical architectures and requirements for some of the software that I work with. Whenever there is a new release I update the diagrams and then export each in turn as an image file. Because of the number of diagrams involved this became a rather laborious process … so as usual I decided to make things a bit more automated.

I have my diagrams organised such that related diagrams are held within a single OmniGraffle file but each is assigned to its own canvas (basically a page) within the file. Unfortunately in OmniGraffle (I’m using OmniGraffle Pro 5 on OS X) there is no option to export all canvases, but OmniGraffle is Applescript compatible so automating the generation of image files was actually relatively straight forward.

Once you have saved the Applescript into your script library, simply open the file you wish to export in OmniGraffle and run the script.

The script begins by having the user select a directory to output the image files to. If the user cancels the folder selection at this point, the script will terminate and no export will take place.

Note that the application specified in the script below is “OmniGraffle Professional 5”. If you are using a different version of OmniGraffle, you would need to amend this accordingly.

Once a folder has been selected, the script identifies the front most document open in OmniGraffle, fetches the name of the file and removes the file name extension (e.g. “.graffle”) from it to give just the name of the file. This will form the first part of our name for the exported images. The routine used to strip the file name extension is not one I created, but in fact from macosxautomation.com - it does the job admirably.

The script then loops through each canvas in the document. It gets the name of the canvas and then build the file path for the export of that diagram as “{output folder}/{file name (with no file extension)} - {canvas name}.png”. This ensures that each exported image is uniquely and meaningfully named; assuming of course your original file name and canvas names are also meaningful.

The canvas is then exported and a dialog box is then displayed showing the number of images exported.

That’s all there is to it. This saves me a significant amount of time especially since we generally have a few review iterations each time we do an update. Here’s hoping that it can do the same for you.

--Choose the output folder.  Script ends if cancelled
set strOutputDirectory to choose folder

--Get the name of the current file
tell application "OmniGraffle Professional 5"
    set objOmniGraffleFile to front document
    set strOmniGraffleFileName to name of objOmniGraffleFile as string
end tell

--Drop the file extension
set strOmniGraffleFileNameNoExtension to remove_extension(strOmniGraffleFileName)

--Export each canvas
tell application "OmniGraffle Professional 5"
    set objOmniGraffleFile to front document
    set canvas of front window to canvas 1 of objOmniGraffleFile
    set intCanvasCount to count of canvases of objOmniGraffleFile
    --Iterate through each canvas and export it to the specified folder
    --File name will be the file name of the file with a hyphen and then the name of the canvas
    --Files saved in PNG format
    repeat with intCurrentCanvasID from 1 to intCanvasCount
        set canvas of front window to canvas intCurrentCanvasID of objOmniGraffleFile
        set currentCanvas to canvas of front window
        set strOutputFilePath to strOutputDirectory & strOmniGraffleFileNameNoExtension & " - " & name of currentCanvas & ".png" as string
        save objOmniGraffleFile as "png" in (strOutputFilePath)
    end repeat
end tell

--Display a final message about the exports
display dialog "Exported " & intCanvasCount & " files to
" & strOutputDirectory with title "Export Complete"

-- Remove file extension (from http://www.macosxautomation.com/applescript/sbrt/)
on remove_extension(this_name)
    if this_name contains "." then
        set this_name to ?          (the reverse of every character of this_name) as string
        set x to the offset of "." in this_name
        set this_name to (text (x + 1) thru -1 of this_name)
        set this_name to (the reverse of every character of this_name) as string
    end if
    return this_name
end remove_extension
Author: Stephen Millard
Tags: | applescript |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read