Search and replace for multiple text items within a variable?

Hi. I'd appreciate any suggestions as to how I can efficiently perform many, many search and replace operations within a variable.

I know how to do this using the Search and Replace Variable action, acting on the text of the variable, where each Search and Replace Variable entry covers one instance of searching for something and replacing it with something else. And that works great.

The problem is that my list of phrases to search and replace is exceeding 50 now, so using a Search and Replace Variable action for each one is getting unwieldy. And I want to keep on adding, so the problem is only going to get worse!

The sort of text replacements I need to do are:

Change grade book to gradebook
Change course 318 to PROJ 318
Change brightspace to Brightspace
Change 1:00 p.m. to 1 p.m.
Change kailyn to Cailynn
Change my office location to my office, which is B123 in the Quad Tower

So, there isn't much connecting each thing to be searched for and replaced, pattern-wise. They're all pretty distinct.

If there's a way I could enter the list of search text and replace text in a variable in Keyboard Maestro, or maybe a csv file on disk, that'd make what has become an essential part of my workflow a lot more usable as it continues to grow.

Thanks in advance for any assistance. I've found other posts here on these forums that touch on this topic, but nothing that quite matches what I'm trying to do I think.

You could use a dictionary for the search and replace pairs. This way, when you have new ones, just add them to the dictionary. You can also use a JSON-formated external source.

For example:

[demo] Search and Replace from Dictionary.kmmacros (4.2 KB)





Not sure if it is this you are looking for.

Edit 2020-01-29:

Corrected an error in a regex.


You could also simply list each regex as a separate line of a txt document. Then use a For Each action to read each regex search (one at a time) to do the appropriate search and replace. As you add regex searches to the growing txt document, the For Each macro will adapt and do the additional searches.

1 Like

I enforce a publication style on texts I publish using a Perl script in an Execute Shell Script action. Attached is your list, one substitution per line, most of them ignoring case.

It's pretty easy to expand this and it runs like lightning (just select the text you want to search). You can move the Perl code to a text file (changing where the action looks for it in the popup) and even run the Perl code independently of Keyboard Maestro.

Style Example.kmmacros (4.8 KB)

P.S. Not the Rob Galbraith?

1 Like

Tom, Mike, that's exactly what I'm looking for, a single field or document to put the search and replace phrases, and your suggested search phrases also give some good ideas about how to simplify certain ones that do have a pattern to them. Thanks so much for your time and for the sample macros as well. I'm going to start implementing right away. Long live Keyboard Maestro!

Thanks too, thoffman666, that gives me something to think about.

Also, Mike: hello! Yup, I'm the Rob Galbraith you're thinking of, I'm pretty sure, since we both once worked in the same corner of the Internet. Hope you're well and that Photo Corners is thriving. As my question here reveals, I'm a teacher now, and enjoying it.

Delighted to bump into you here, Rob! And glad to hear you still enjoy teaching.

(NB: Rob was the Walter Cronkite of the digital photography corner of the Internet in its early days.)

1 Like