×

Console Functions

In addition to providing a means for running Omni Automation scripts, an Omni application’s console window allows scripts to “log” debugging, warning, or error information where it can be viewed in the system console or in the console output area. A single instance of the Console class is available to scripts as the console global variable.

Here are the functions supported by the Console:

console.log()

A simple logging of a text string:

console-00

An example using the (optional) “additional” parameter to include the shape of the item being processed in the log statement:

console-01

Another example passing in an object rather than a string:

console-08

console.error()

console-02

Using the (optional) “additional” parameter to include unique identifying information about the object causing the error:

console-03

console.info()

An informational example that creates and displays an informational data dictionary for each processed object:

console-04

console.warn()

Posting a warning about graphic names containing spaces:

console-05

console.clear()

Placing the console.clear() function at the start of the script will remove previous messaging from the console window and only display the results of the current script:

console-06 console-06A

Graphic Summary

Here’s an example of using console logging to review the status of the graphics in the current canvas. Run the script with the OmniGraffle console window open to view the results.

Note the use of JavaScript Ternary operators in lines 8 and 10:

NOTE: This page contains interactive script examples that are executed only with your approval. See the section on Script Security for details about allowing the execution of remote scripts.
omnigraffle://localhost/omnijs-run?script=console%2Eclear%28%29%0Agraphics%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%2Egraphics%0Agraphics%2EforEach%28function%28graphic%2C%20index%29%7B%0A%09obj%20%3D%20new%20Object%28%29%0A%09obj%2ElayerName%20%3D%20graphic%2Elayer%2Ename%0A%09obj%2Ename%20%3D%20graphic%2Ename%0A%09obj%2Eid%20%3D%20graphic%2Eid%0A%09obj%2Etype%20%3D%20graphic%20instanceof%20Line%20%3F%20%22Line%22%20%3A%20graphic%2Eshape%0A%09if%20%28obj%2Etype%20%21%3D%20%22Line%22%29%7B%0A%09%09obj%2Eimage%20%3D%20graphic%2Eimage%20instanceof%20ImageReference%20%3F%20true%20%3A%20false%0A%09%7D%20else%20%7Bobj%2Eimage%20%3D%20false%7D%0A%09obj%2Elocked%20%3D%20graphic%2Elocked%0A%09console%2Elog%28%22Graphic%20%22%20%2B%20String%28index%29%2CJSON%2Estringify%28obj%29%29%0A%7D%29
Graphics Summary
 

console.clear() var graphics = document.windows[0].selection.canvas.graphics graphics.forEach(function(graphic, index){ var obj = new Object() obj.layerName = graphic.layer.name obj.name = graphic.name obj.id = graphic.id obj.type = graphic instanceof Line ? "Line" : graphic.shape if (obj.type != "Line"){ obj.image = graphic.image instanceof ImageReference ? true : false } else {obj.image = false} obj.locked = graphic.locked console.log("Graphic " + String(index),JSON.stringify(obj)) })
console-076

“Pre-Flight” Documents

Here’s an example of using the console to Pre-flight a document prior to saving, printing, or processing. If installed as an action, the results will log to the application’s console window when the action is selected from the Automation menu.

This example script checks for graphics that are unnamed or are set to not print. You can edit the script to check for the document or graphic properties you require.

omnigraffle://localhost/omnijs-run?script=console%2Eclear%28%29%0Avar%20cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0Avar%20cnvsName%20%3D%20cnvs%2Ename%0Acnvs%2Egraphics%2EforEach%28function%28graphic%2C%20index%29%7B%0A%09var%20obj%20%3D%20new%20Object%28%29%0A%09obj%2EcanvasName%20%3D%20cnvsName%0A%09obj%2ElayerName%20%3D%20graphic%2Elayer%2Ename%0A%09obj%2Ename%20%3D%20graphic%2Ename%0A%09obj%2Eid%20%3D%20graphic%2Eid%0A%09obj%2Etype%20%3D%20graphic%20instanceof%20Line%20%3F%20%22Line%22%20%3A%20graphic%2Eshape%0A%09if%20%28obj%2Etype%20%21%3D%20%22Line%22%29%7B%0A%09%09obj%2Eimage%20%3D%20graphic%2Eimage%20instanceof%20ImageReference%20%3F%20true%20%3A%20false%0A%09%7D%20else%20%7B%0A%09%09obj%2Eimage%20%3D%20false%0A%09%7D%0A%09obj%2Elocked%20%3D%20graphic%2Elocked%0A%09obj%2Eprints%20%3D%20graphic%2Elayer%2Eprints%0A%09var%20objStr%20%3D%20JSON%2Estringify%28obj%29%0A%09if%20%28obj%2Eprints%20%3D%3D%3D%20false%29%7B%0A%09%09console%2Ewarn%28%22Non%2DPrinting%20Graphic%20%22%20%2B%20String%28index%29%2CobjStr%29%0A%09%7D%20else%20if%20%28obj%2Ename%20%3D%3D%3D%20null%29%7B%0A%09%09console%2Ewarn%28%22Unnamed%20Graphic%20%22%20%2B%20String%28index%29%2CobjStr%29%0A%09%7D%20else%20%7B%0A%09%09console%2Einfo%28%22Graphic%20%22%20%2B%20String%28index%29%2CobjStr%29%0A%09%7D%0A%7D%29
Flight-Check Canvas
 

console.clear() var cnvs = document.windows[0].selection.canvas var cnvsName = cnvs.name cnvs.graphics.forEach(function(graphic, index){ var obj = new Object() obj.canvasName = cnvsName obj.layerName = graphic.layer.name obj.name = graphic.name obj.id = graphic.id obj.type = graphic instanceof Line ? "Line" : graphic.shape if (obj.type != "Line"){ obj.image = graphic.image instanceof ImageReference ? true : false } else { obj.image = false } obj.locked = graphic.locked obj.prints = graphic.layer.prints var objStr = JSON.stringify(obj) if (obj.prints === false){ console.warn("Non-Printing Graphic " + String(index),objStr) } else if (obj.name === null){ console.warn("Unnamed Graphic " + String(index),objStr) } else { console.info("Graphic " + String(index),objStr) } })

MORE: See the next page about the Console Toolbar for more detail about the pre-flight example.