How to Reverse Characters in a Variable

On my machine, it isn’t.

It works correctly with the “simple” kind of strings like résumé, but fails with the Rượu đế test string from above. (The diacritics ́̂ and ̛̣ get detached from the letters they belong to.)

(The AppleScriptObjC method works correctly with all.)



For ease of testing, here a macro with all posted solutions so far:

[test] Ways to Reverse String.kmmacros (18.5 KB)

  1. Open the macro in KM Editor.
  2. Use “Try Action” from the contextual menu on the first action to set the variable.
  3. Open KM Editor > Preferences > Variables and select the “INPUTVALREVERSE” variable. Leave that window open and visible on screen.
  4. Now apply “Try Action” on a script action you want to test and watch the output in the variables window.

As said, for display I’m using the variables window, because the font (Menlo) used in the KM Shell Script Results window cannot handle all characters correctly.

The colors of the actions reflect the grade of Unicode compliance of the script:

  • red: fails completely
  • orange: OK with résumé | Käse, fails with Rượu đế
  • green: OK with all

Untitled-pty-fs8

The green actions should produce this result:

07-pty-fs8

2 Likes