Published on

Using Talon's mimic( ) function

Authors
  • avatar
    Name
    Trillium Smith
    Twitter

Talon Voice has an inbuilt mimic function that lets the user test commands. This is a feature I use fairly regularly when I'm in a space with too much background talking, or I just want to test something.

This code snippet creates two functions m and M (short for mimic).

function m() {
  command=""
  for word in "$@"; do
    if [[ "$word" == *, || "$word" == *. ]]; then
      word=$(echo "${word}" | tr -d ',.')
      command="$command $word"
      command="${command#"${command%%[![:space:]]*}"}"
      command="${command%"${command##*[![:space:]]}"}"
      echo "mimic(\"${command}\")"| $TALON_REPL_PATH > /dev/null
      echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
      command=""
    else
      command="$command $word"
    fi
  done
  echo "mimic(\"${command/ /}\")"| $TALON_REPL_PATH > /dev/null
}

function M() {
  echo "mimic(\"command tab\")"| $TALON_REPL_PATH > /dev/null
  echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
  m "$@"
  echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
  echo "mimic(\"command tab\")"| $TALON_REPL_PATH > /dev/null
}

In order to work, $TALON_REPL_PATH needs to be defined earlier else in your .bashrc

What does m do? (lowercase)

m is a function that pipes a sentence into mimic, splitting commands on period . and comma ,. It does this via looping over the stored arguments in $@, which will be every space separated word in the entered line.

The command m focus chrome, go tab one will split into the follow parts:

  • m: The function being executed
  • focus chrome,: The first phrase being examined by the script
  • go tab one: The second phrase being examined by the script

In the above example, focus chrome, has a trailing comma, this is used to separate commands. focus chrome, will be split into focus chrome and a short sleep. The mimic command only accepts strings of words Talon Voice understands, so for example mimic will fail with numbers e.g. 1 but would accept strings representing numbers e.g. one. Additional characters like punctuation tend to also break mimic, so when using this script I don't use them.

function m() {
  # begin an empty command
  command=""
  # loop over all arguments passed to the function
  for word in "$@"; do
    # If , or . are found in an argument
    if [[ "$word" == *, || "$word" == *. ]]; then
      # remove , or . from the argument
      word=$(echo "${word}" | tr -d ',.')
      # add the current argument to the command
      command="$command $word"
      # Remove leading whitespace
      command="${command#"${command%%[![:space:]]*}"}"
      # Remove trailing whitespace
      command="${command%"${command##*[![:space:]]}"}"
      # Pipe the command to the talon repl running mimic
      echo "mimic(\"${command}\")"| $TALON_REPL_PATH > /dev/null
      # Pipe a short sleep between each command
      echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
      command=""
    # if no , or . are found in the argument
    # append the argument to the command
    else
      command="$command $word"
    fi
  done
  # Run the last saved command
  echo "mimic(\"${command/ /}\")"| $TALON_REPL_PATH > /dev/null
}

What's with the > /dev/null

This just mutes return statements from the repl so that noise won't pollute the terminal as I'm working.

What does M do? (uppercase)

M is a helper function that automatically returns to the last used window. This makes it easy to run a short command e.g. tab search, word github, south, lap. In my repo that would execute in order:

  • command tab to switch windows. Chrome is the assumed window in this case
  • tab search to run the command to search open tabs
  • word github to type github into the search
  • south to press the down arrow
  • lap to press enter, and now we'd on a github tab
function M() {
  # Change to the last used window
  echo "mimic(\"command tab\")"| $TALON_REPL_PATH > /dev/null
  # Send a short sleep
  echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
  # Run commands after `M`
  m "$@"
  # Send a short sleep
  echo "actions.sleep(.05)"| $TALON_REPL_PATH > /dev/null
  # Restore back the original used window
  # (as long as no other window changing commands are used)
  echo "mimic(\"command tab\")"| $TALON_REPL_PATH > /dev/null
}

(Note: it's hard coded for mac at the moment but mimicking command tab so if that doesn't switch to your last window in your OS it will need updating).

When is this actually a terrible choice to test something

Funnily enough, I had thought that using mimic to test if pieces of my Talon setup were functioning properly was totally fine. Turns out no, mimic is not the same as actually saying a whole phrase in Talon. This is important when testing commands from other sources like talon_gaze_ocr, which depends on a phrase being said to generate timestamp data. mimic does not generate that data.