Voice control custom command with variable

catalinavoice-control

I'd like to be able to declaim "Keyword foo" and have voice control, on hearing this, type :foo for me.

Many of the built-in commands take 'parameters' (i.e., some following speech), so if it were possible to make a user defined command as such it would look like keyword <phrase>, i.e., a 'variable' goes into the command (that variable being "phrase").

Is there a way to make a user-defined command which takes a variable as input, as many of the built-in commands do? That would be super useful.

I'm looking into whether it would be feasible to write code in an IDE (in my case, Clojure & Intellij) using only voice control (and perhaps an on screen control pad for help where needed e.g. that provided by the wacom cintiq), thus, keyboard free! – it would be amazing if this were possible.

I haven't yet explored what can be done with "run workflow".
(I don't see any indication as yet that those can take variables, though).

I've found the location of the default workflows:
/System/Library/PrivateFrameworks/SpeechObjects.framework/Versions/A/Frameworks/DictationServices.framework/Versions/A/Resources/Workflows

mbp:Workflows m$ ll
total 0
drwxr-xr-x  12 root  wheel   384B 14 Dec  2019 ./
drwxr-xr-x  65 root  wheel   2.0K 12 Oct 16:30 ../
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 New Audio Recording.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 New Screen Recording.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 New Video Recording.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Apple Website.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Top Albums.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Top Family Movies.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Top Movies.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Top Music Videos.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Show Me Top Songs.workflow/
drwxr-xr-x   3 root  wheel    96B 14 Dec  2019 Take My Picture.workflow/

Update: realised I may be able to get half way to this by defining a command "To keyword", which could issue Alt+<- , press ':', then issue Alt+->.
But that's not just a 'keyboard shortcut', it's three presses. I haven't used automator yet but guess that might be able to do this.

Best Answer

I've got something workable by using "Run Workflow" and the following Apple Script":

on run {input, parameters}
    activate application "IntelliJ IDEA CE"
    tell application "System Events" to key code 123 using {option down} -- left arrow for back word
    tell application "System Events" to keystroke ":"
    tell application "System Events" to key code 124 using {option down} -- right arrow for fwd word
    -- https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
    return input
end run

I just have to say the word "foo" as normal then speak a subsequent command "to keyword", which converts the previous word to a keyword using a sequence of three key presses as above.

Related Question