1

Because I have limited use of my hands, I use Voice Control to dictate most of my text. (I'm using it to speak this question.) Voice Control offers the ability to add custom commands, including those that will run an Automator workflow. Automator, in turn, can execute arbitrary JavaScript or AppleScript. (I prefer JavaScript). What I want to do is to create a script that takes the currently selected text in whatever application is active, manipulates it, and then replaces the selected text in place. For example, imagine creating a "hyphenate this" command that converts selected text like this—"Mac Voice Control scripting"—and changes it to this—"Mac-Voice-Control-scripting". Writing the JavaScript or AppleScript to transform the string is the easy part. The problem is how to get the currently selected text for the current application, whatever it may be.

In Automator, you can create a "Quick Action" workflow that is supposed to pass the currently selected text and enable you to replace it. In practice, however, in Big Sur (at least) I could not get this workflow to receive text from any application. Given a standard run() function, the input array was always of length 0.

How do I create a workflow that may be executed by a custom command in voice control that receives, processes, and replaces the currently selected text in the current application?

1

Sorry I have zero knowledge of JavaScript but I do have somewhat of a solution for you using only AppleScript without the need for Automator.

This following AppleScript code will take the selected text in any application, replace the spaces with hyphens then will replace your original selected text with the new hyphenated text.

-- Copies Selected Text To The Clipboard
tell application "System Events" to keystroke "c" using {command down}
delay 0.1

-- Uses The Text From The Clipboard And Replaces Space Characters With A Hyphen
set hyphenatedText to hyphenateText(the clipboard, " ", "-")
delay 0.1

-- Inserts The New Hyphenated Text Replacing The Original Selected Text
tell application "System Events" to keystroke hyphenatedText

(* 
someText is the selected text containing the characters to be replaced
replaceTheseListItems are characters from original text which you want replaced
replacementListItems replacement text characters 
*)
on hyphenateText(someText, replaceTheseListItems, replacementListItems)
    set originalText to someText
    set AppleScript's text item delimiters to replaceTheseListItems
    set tempText to text items of originalText
    set text item delimiters to replacementListItems
    set cleanedText to tempText as text
end hyphenateText

Save the AppleScript code as a .scpt file (Hyphenate Selected Text.scpt). Then open Finder and select your new script file. Once the file is selected, and with voice control activated, simply say the command “Make This Speakable”. When the new dictation command dialog box opens, Assign it a new voice command

enter image description here

For demonstration purposes I added this script file to the script menu and ran it from there

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .