Take a look at Literary Toolbox III and Text Toolbox I, specifically the Text Toolbox. It includes what you're doing but it also shows how to do it in two different ways (some case changes use Keyboard Maestro actions and others use Perl).
1 Like