Search & Replace Array? Or: How to address line x of variable

I don't know if that's the right word for what I'm looking for.

My problem is, that some of my text-processing macros will screw up certain characters.

For example, this line…
äöüÄÖÜßâéè–—€

Gets turned into this:
äöüÄÖÜßâéè–—â‚

I'm not even sure if the problem lies with Keyboard Maestro or with the app I'm pasting from or pasting to.

In any case, I'd like to add a step to these macros to ensure that problems like that, if they occur, get fixed automatically.

So far, I've done it using the Search & Replace action.

But what's annoying about this is that I need one action for each character. So, for the characters from the above example, I'd need 15 actions. That is not only cumbersome to set up, but also clutters up the macro.

I would like a solution where I have one row of "from" values, and one row of "to" values, and then tell Keyboard Maestro to replace line by line. This would create less clutter, and would make it easier to add new characters later on.

What I got so far is this:

But now how do I tell Keyboard Maestro to…

  • go through each line of variable "from"
  • search the Clipboard for its content
  • replace it with the content found at the corresponding line in variable "to"?

Thanks & Best,
Leo

Hey Leo,

You're on the right track; an array is indeed what you want in this case. Here's the simplest way I could think of to do this with native KM actions (i.e. no scripting):

Search and Replace Array.kmmacros (8.1 KB)


###Results:

The information I used to create this macro can be found here: https://wiki.keyboardmaestro.com/manual/Variables#Variable_Arrays
In this macro, the LocalTo variable is accessed as an array like this:

%Variable%LocalTo[LocalCounter]Return%

Essentially, the LocalCounter variable is used to keep each line of the LocalTo variable in sync with the current line being processed in the For Each action, and the Return is used to make the array's delimiter a new-line character instead of the default comma (in KM, %Return% can be used as a substitute for a new-line character, i.e. what is inserted when the Enter/Return key is typed, but in a calculation field like a variable array, the surrounding % characters are not necessary).

Hopefully this all makes some semblance of sense! As always, feel free to ask any further questions you may have.

1 Like

Hey Leonard,

You're having Unicode characters get mangled for some reason.

You need to tell us what apps this is happening in and what text filters you're using.

Hopefully we can figure out how to fix the problem at its source rather than patch it with an after-the-fact search/replace.

If we get stuck with a patch action I myself would use the Satimage.osax's change command for simplicity.

set testVar to "
ä
ö
ü
Ä
Ö
Ü
ß
â
é
è
"

set newVar to change {"ä", "ö", "ü", "Ä", "Ö", "Ãœ", "ß", "â", "é", "è"} into {"ä", "ö", "ü", "Ä", "Ö", "Ü", "ß", "â", "é", "è"} in testVar

But Gabe's solution looks perfectly fine.

-Chris

1 Like