OmniGraffle: Document

A Document is the top-level object in OmniJS. For security reasons, scripts can only address the frontmost document, they cannot query for or address other documents that may be open. Unlike other classes of objects, such as graphics, that can be addressed by index, the “current document” is simply referenced as document instead of app.documents[0] in your scripts.

The following documentation describes how open, create, save, and close documents, as well as how to access a document’s properties.

Open Document

To open an OmniGraffle file, a file URL is passed to the openDocument application method. This handler attempts to open the specified document and to return a reference to it asynchronously. If the document is already open, the reference to the open document is passed along.

Note that due to platform sandboxing restrictions, opening the document may fail if the application doesn’t have currently permission to access the given file URL. The document, if any, that is associated with the calling script can be passed along to help grant permission to open the new document.

The passed in function will return two arguments: the first (result) will be either either the Document reference or an Error. On success, the second argument (wasOpen) is a Boolean value specifying whether the document was already open.

app.openDocument(document,fileURL,function(result,wasOpen){}

A an example, here is a script that uses the choose() method of the URL class to prompt the user to locate and select an OmniGraffle document (in either file or package format) and then open the chosen document:

fileURL = URL.choose(["com.omnigroup.omnigraffle.graffle","com.omnigroup.omnigraffle.graffle-package"]) if (fileURL == null){ throw new Error('user cancelled') } else { app.openDocument(document,fileURL,function(result,wasOpen){ if(result instanceof Document){ // processing code goes here cnvs = result.windows[0].selection.canvas console.log(cnvs.graphics.length) } else { throw new Error(result) } }) }
omnigraffle:///omnijs-run?script=fileURL%20%3D%20URL%2Echoose%28%5B%22com%2Eomnigroup%2Eomnigraffle%2Egraffle%22%2C%22com%2Eomnigroup%2Eomnigraffle%2Egraffle-package%22%5D%29%0Aif%20%28fileURL%20%3D%3D%20null%29%7B%0A%09throw%20new%20Error%28%27user%20cancelled%27%29%0A%7D%20else%20%7B%0A%09app%2EopenDocument%28document%2CfileURL%2Cfunction%28result%2CwasOpen%29%7B%0A%09%09if%28result%20instanceof%20Document%29%7B%0A%09%09%09%2F%2F%20processing%20code%20goes%20here%0A%09%09%09cnvs%20%3D%20result%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0A%09%09%09console%2Elog%28cnvs%2Egraphics%2Elength%29%0A%09%09%7D%20else%20%7B%0A%09%09%09throw%20new%20Error%28result%29%0A%09%09%7D%0A%09%7D%29%0A%7D

New Document

The makeNewAndShow() method is called on the Document class to create and setup an OmniGraffle document. This method includes as its parameter, a function that is called once the new document is created. A reference to the new document is passed into the function, and can be used to set the properties of the default canvas as well as be used to create new elements. Note the use of the canvasSizingMode property to indicate the method used to size the default canvas.

Document.makeNewAndShow(function(doc){ cnvs = doc.windows[0].selection.canvas cnvs.canvasSizingMode = CanvasSizingMode.Fixed cnvs.size = new Size(612,792) cnvs.name = "Letter to Sally" })
omnigraffle:///omnijs-run?script=Document%2EmakeNewAndShow%28function%28doc%29%7B%0A%09cnvs%20%3D%20doc%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0A%09cnvs%2EcanvasSizingMode%20%3D%20CanvasSizingMode%2EFixed%0A%09cnvs%2Esize%20%3D%20new%20Size%28612%2C792%29%0A%09cnvs%2Ename%20%3D%20%22Letter%20to%20Sally%22%0A%7D%29

Document Properties

The document class has a small set of properties:

Name

The read-only name property has a value that is the name of the OmniGraffle document as it appears in the title bar of the document window. On macOS, this value will reflect the value of the Finder’s file extension visibility setting, set in the file’s Information window.

Document Undo

If the value of the document’s canUndo property is true, then the undo() method can be used to undo the previous user or script action.

if(document.canUndo){document.undo()}
omnigraffle:///omnijs-run?script=if%28document%2EcanUndo%29%7Bdocument%2Eundo%28%29%7D

Document Redo

If the value of the document’s canRedo property is true, then the redo() method can be used to re-apply the previous user or script action.

if(document.canRedo){document.redo()}
omnigraffle:///omnijs-run?script=if%28document%2EcanRedo%29%7Bdocument%2Eredo%28%29%7D

Document Close

Close this document. This is not currently allowed if the current scripting environment is owned by the Document (you can close other documents, but not your own).

document.close()
omnigraffle:///omnijs-run?script=document%2Eclose%28%29
var oldDoc = document Document.makeNewAndShow(function(newDoc){ oldDoc.close() })
omnigraffle:///omnijs-run?script=var%20oldDoc%20%3D%20document%0ADocument%2EmakeNewAndShow%28function%28newDoc%29%7B%0A%09oldDoc%2Eclose%28%29%0A%7D%29

Document Save

The save() method performs the standard save action with the document.

document.save()
omnigraffle:///omnijs-run?script=document%2Esave%28%29
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER