Draft Contents to Selected Graphic
Drafts → OmniGraffle
1Writer → OmniGraffle
Synopsis: A Drafts action that transfers the text of the draft document to the selected OmniGraffle graphic.

Since JavaScript Core is the native scripting language for the Drafts application, implementing and executing Omni Automation script URLs within Drafts actions is easy to do.

For example, let’s say you want to create a Drafts action for exporting the contents of the current draft to a selected graphic in OmniGraffle. Here is a simplified Omni Automation script for setting the contents of the single selected graphic.

if (document.windows[0].selection.graphics.length != 1){ title = "SELECTION ERROR"; message = "Please select a single graphic."; new Alert(title, message).show(function(result){}); } else { cnvs = document.windows[0].selection.canvas graphic = document.windows[0].selection.graphics[0] currentTextSize = graphic.textSize; graphic.text = 'XXXXX'; graphic.textSize = currentTextSize; }
omnigraffle://localhost/omnijs-run?script=if%20%28document%2Ewindows%5B0%5D%2Eselection%2Egraphics%2Elength%20%21%3D%201%29%7B%0A%09title%20%3D%20%22SELECTION%20ERROR%22%3B%0A%09message%20%3D%20%22Please%20select%20a%20single%20graphic%2E%22%3B%0A%09new%20Alert%28title%2C%20message%29%2Eshow%28function%28result%29%7B%7D%29%3B%0A%7D%20else%20%7B%0A%09cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0A%09graphic%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Egraphics%5B0%5D%0A%09currentTextSize%20%3D%20graphic%2EtextSize%3B%0A%09graphic%2Etext%20%3D%20%27XXXXX%27%3B%0A%09graphic%2EtextSize%20%3D%20currentTextSize%3B%0A%7D

To include the Omni Automation script within Drafts action JavaScript, transform the script into a JavaScript function that accepts the new graphic text as its input parameter, and place the function within the Drafts action code:

(() => { // wrapping the action script in a self-invoking anonymous arrow function (() => {})(); // prevents possible conflicts between script actions that may use the same variable names // OmniGraffle JavaScript Context // Omni Automation script as a function with text input function setTextForSelectedGraphic(textInput){ if (document.windows[0].selection.graphics.length != 1){ title = "SELECTION ERROR"; message = "Please select a single graphic."; new Alert(title, message).show(function(result){}); } else { cnvs = document.windows[0].selection.canvas graphic = document.windows[0].selection.graphics[0] currentTextSize = graphic.textSize; graphic.text = textInput; graphic.textSize = currentTextSize; } }; // Host Application JavaScript Context (1Writer, Drafts, etc.) const strText = editor.getText(); // create and execute an Omni Automation script URL app.openURL( 'omnigraffle://localhost/omnijs-run?script=' + encodeURIComponent( '(' + setTextForSelectedGraphic + ')' + '(' + JSON.stringify(strText) + ')' ) ); })();

* code based upon examples and suggestions from draft8

 01-29  Wrapping the action script in a self-invoking anonymous arrow function (() => {//code})(); prevents possible conflicts between script actions executed in sequence that may use the same variables or objects.

 04-16  The function setTextForSelectedGraphic() is created to hold the Omni Automation script for setting the contents of the current selected graphic in OmniGraffle.

 05-15  The Omni Automation script for setting the contents of the current selected graphic in OmniGraffle.

 20-28  Drafts app JavaScript action code.

 20  Retrieve the text contents of the draft and store it in the variable: strText

 22-28  Execute the generated Omni Automation script URL

 23-27  Generate an Omni Automation script URL that will contain the percent encoded script and passed text from the draft.

 23  The beginning of an Omni Automation script URL begins with the name of the targeted Omni app, followed by URL identifier and the script argument and value (encoded script).

 24-27  the JavaScript encodeURIComponent() method is used to percent encode both the Omni Automation function code as well as the text from the Drafts document.

 25-26  Encasing the encoded object within parens enables the function to execute using the passed text as the input argument: (function)(arguments)

In the Drafts app, the code is entered as the single action step:

IMG_46C36C7B54EE-1
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