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.
Install the workflow.
The OmniGraffle Action
This is the Omni Automation action for OmniGraffle that will performing the following tasks:
- Detect if a single shape containing text is selected
- Extract the text from the selected shape
- Encode the extracted text and create a URL for targeting the Shortcuts app
- Execute the url and wait for the response from the Shortcuts app
- Replace the text in the selected shape with the translated text
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;
})();
|