Text substitutions for superscript and subscript

There is a way to individually create text substitutions on Mac for most applications, by going to System Preferences→Keyboard→Text and then creating text substitutions. For example, I've made the following ones for ₂, ₃, × and ²³. Text

If I wanted to, I could create more and more shortcuts like this, but that takes a lot of time. Is there a way to use Automator and/or Applescript and/or Terminal to make such substitutions for all such cases? For example, if I type _5002, it should come out looking like ₅₀₀₂.


Solution 1:

Here is one solution using the command line:

  1. Converting numbers to super/subscript

The sed utility allows you to use a translation table to convert characters to other characters:

$ echo "abc513" | sed -e 'y|0123456789|⁰¹²³⁴⁵⁶⁷⁸⁹|'
abc⁵¹³

$ echo "abc513" | sed -e 'y|0123456789|₀₁₂₃₄₅₆₇₈₉|'
abc₅₁₃

From there, we can write a tsubst script:

$ cat ./tsubst
#!/bin/bash

function superscript { echo "$1" | sed -e 'y|0123456789|⁰¹²³⁴⁵⁶⁷⁸⁹|'; }
function subscript   { echo "$1" | sed -e 'y|0123456789|₀₁₂₃₄₅₆₇₈₉|'; }

function phrase_from_shortcut_pattern {
  if [[ "$1" =~ ^\^(.+)$ ]]
  then
    # ^ -> superscript
    echo "$(superscript "${BASH_REMATCH[1]}")"
  elif [[ "$1" =~ ^_(.+)$ ]]
  then
    # _ -> subscript
    echo "$(subscript "${BASH_REMATCH[1]}")"
  else
    echo "$1"
  fi
}

phrase_from_shortcut_pattern "${@}"

That script takes one argument, a Text Replacement shortcut pattern with a ^ or _ prefix to indicate a super or subscript transformation, and returns the corresponding phrase:

$ ./tsubst _answer42
answer₄₂

$ ./tsubst ^answer42
answer⁴²
  1. Generate phrases from lists or ranges of shortcut patterns

With that script, we can use bash loops and substitutions to generate phrases from a list of shortcut patterns:

$ for p in _42 ^42; do echo "${p} -> $(./tsubst "${p}")"; done
_42 -> ₄₂
^42 -> ⁴²

Or a range of shortcut patterns:

$ for p in _{6..8} ^{9..11}; do echo "${p} -> $(./tsubst "${p}")"; done
_6 -> ₆
_7 -> ₇
_8 -> ₈
^9 -> ⁹
^10 -> ¹⁰
^11 -> ¹¹
  1. Adding text substitutions from the command line

This is the tricky part... I found one command-line utility that does its job well: shortcuts.

$ shortcuts read
0: "jrv" —> "J’arrive !"
1: "omw" —> "On my way!"

$ shortcuts create "xyz" "XYZ"

$ shortcuts read
0: "xyz" —> "XYZ"
1: "jrv" —> "J’arrive !"
2: "omw" —> "On my way!"

$ shortcuts delete xyz

$ shortcuts read
0: "jrv" —> "J’arrive !"
1: "omw" —> "On my way!"
  1. Automatically adding Text Replacement entries...

Now we can compose the two utilities to add entries to the Text Replacements preferences:

$ for p in _42 ^42 _{6..8} ^{9..11}; do
>   ./shortcuts create "${p}" "$(./tsubst "${p}")"
> done

$ ./shortcuts read
0: "^11" —> "¹¹"
1: "^10" —> "¹⁰"
2: "^9" —> "⁹"
3: "_8" —> "₈"
4: "_7" —> "₇"
5: "_6" —> "₆"
6: "^42" —> "⁴²"
7: "_42" —> "₄₂"
8: "jrv" —> "J’arrive !"
9: "omw" —> "On my way!"

And the result in the Text Replacements preferences:

enter image description here