×

Translate OmniGraffle Shape Text

Here’s a simple but effective example of how well Omni Automation and the Shortcuts app work together. Text in a selected shape in an OmniGraffle document can be translated into a language chosen by the user by simply selecting the shape containing the text and launching an action from the OmniGraffle Automation menu.

IMPORTANT: The OmniGraffle app and the Shortcuts app must both be active and showing, by using either the split-view or slide-over iOS multitasking mode.

The Workflow

The workflow begins with the Shortcuts action of Translate Text, whose parameters are set for detecting the language of the input text, and for displaying a list of destination languages to the user during the running of the workflow.

The resulting translated text will automatically be returned to the Omni Automation action when the workflow is done.

translate-text-workflow

Install the workflow.

The OmniGraffle Action

This is the Omni Automation action for OmniGraffle that will performing the following tasks:

And here’s the action code:

Translate Shape Text Plug-In
 

/*{ "type": "action", "targets": ["omnigraffle"], "identifier": "com.omni-automation.og.translate-shape-text", "author": "Otto Automator", "description": "Translates the text in the selected shape using an workflow from the Shortcuts app.", "version": "1.2", "label": "Translate Text in Shape", "shortLabel": "Translate Text in Shape" }*/ (() => { const action = new PlugIn.Action(function(selection, sender){ // action code // selection options: canvas, document, graphics, lines, solids, view var textShape = selection.solids[0] var textInput = encodeURIComponent(textShape.text) var workflowName = encodeURIComponent("Translate Text") var urlStr = "shortcuts://run-workflow?name=" + workflowName + "&input=text&text=" + textInput URL.fromString(urlStr).call(function(reply){ // the result is a JavaScript object: {"result":"<-- translated text -->"} var translatedString = reply["result"] var textShape = document.windows[0].selection.solids[0] textShape.text = translatedString }) }); action.validate = function(selection, sender){ // validation code // selection options: canvas, document, graphics, lines, solids, view return (selection.solids.length === 1 && selection.solids[0].text !== "") }; return action; })();

 01-08  The metadata for the action file.

 23-31  The validation function determines whether or not to enable the action in the Automation menu.

 26-30  If a single shape containing text is selected, then enable the menu by returning a value of true, otherwise return a value of false.

 10-21  The main function for the action is performed if the validation routined has a positive result.

 13  Store a reference to the selected shape.

 14  Percent-encode the text from the shape in preparation for inclusion in the URL that will be sent to the Shortcuts app.

 15  Percent-encode the name of the installed Workflow file to run. In this example, the workflow file is named: “Translate Text”

 16  Create the url string for the Shortcuts app by concatenating the encoded data to the workflow url schema. More information about Workflow URLs is available here.

 17-24  Use the fromString() method of the URL class to convert the url string into a URL object, and then use the call() method to execute the url, triggering Workflow and waiting for the response.

 21  The reply of the call(…) function is a JavaScript object with the translated string accessed through the key: result

 23  Set the text of the selected shape to the translation returned as the result of the workflow execution.