Replacements in Clipboard Content Based on a Tab-Delimited File

I'm planning to write a macro that reads the lines of a tab-delimited file and performs a Search and Replace System Clipboard action for every line of the file, using the first and second column of every line as Find and Replace string respectively, and the third column to set the 'kind of search to run'.

So the file would have the structure:

red\tgreen\searchType

Where searchType would be:

  1. sc for case sensitive searching of a string
  2. si for case insensitive searching of a string
  3. rc for case sensitive searching of a regular expression
  4. ri for case insensitive searching of a regular expression
  5. wc for case sensitive searching of a word
  6. wi for case insensitive searching of a word
  7. wa for case adaptive searching of a word
  8. wt for toggling the case of a word

Search types 1 - 4 are KM's default search types for the Search and Replace System Clipboard action:


Search type 5 and 6 would be searching with regular expressions, wrapping the search text with ''\b" to force word boundaries. Search type 7 would adapt the replacement string to the content of the clipboard: red would become green, Red would become Green, and RED would become GREEN, when the third column of the line contains wa.

Example of a file with replacement items:
Screen Shot 2022-04-03 at 08.13.49

Is there any user of KM who already has such a macro (or parts of it) on shelf and would be willing to share it?

Here's a part that I created for the case-adaptive replacements:


Case adaptive replacement.kmmacros (4.6 KB)

Have you considered just using awk, or even JavaScript ?


See for example, a sequence of any number of gsub (formatted vertically if you prefer) here:

awk '{gsub("@@","^");gsub("¤¤","\r\n");print}' file

gawk - Replacing two strings using awk - Stack Overflow

I'd like to solve this task with Keyboard Maestro actions only.

Something like this:


Replace strings in clipboard via tab-del glossary.kmmacros (10.9 KB)