Hi LimoWreck,
You can accomplish this using a Keyboard Maestro For Each Substring loop with a "Search using regex" action to look up the meaning of each character group. The attached macro handles the entire process within Keyboard Maestro, eliminating the need for multiple shell scripts. It reads your input string, loops through each two-character code, looks up the corresponding value in your table, and builds the final translated string.
Translate two character groupings into variable.kmmacros (9.4 KB)
This macro automates the entire translation process in four main steps:
-
Get Input: It starts with your string of character codes (e.g., 49 46 58...).
-
Load Lookup Table: It reads your lookuptable.txt file into memory. You just need to provide the file path.
-
Translate: It loops through the input string, automatically finding each two-character code (49, 46, 58, etc.) and ignoring the spaces. For each code, it looks up the corresponding character in your table and appends it to a result variable.
-
Output: Once finished, it can display the final translated string, copy it to the clipboard, or paste it directly.
EXPLANATION:
1. What do you want to translate? (Green Section)
This section sets up the initial variables.
- Set Variable Instance__String2Translate : This is where you put the string you want to translate. The macro currently has your example string hardcoded. You would replace this with your variable, VarX, or your Clipboard (%SystemClipboard%)
2. What is the meaning of each character grouping? (Purple Section)
This section loads your lookup table. The macro shows two ways to do this:
-
(Enabled) Set Variable Instance__lookupTable to Text : This action has the lookup table hardcoded directly into the macro. This is useful for portability, as you don't need a separate file.
-
(Disabled) Read File to Variable : This action is exactly what you asked for. To use your lookuptable.txt file, you would enable this action and disable or delete the one above it . Then, you would put the full path to your .txt file in the "Read file" field.
3. Translate each character grouping (Yellow/Brown Section)
This is the core of the macro where the translation happens.
-
For each Instance__CharacterPair... : This is the two character grouping that we will translate.
-
The substrings: \w\w : I loves this in KM! it uses a regular expression \w\w to find all substrings that are exactly two "word" characters (letters or numbers) long. It automatically finds 49, then 46, then 58, etc., and completely ignores the spaces in between.
-
Look up the character grouping... : Inside the loop, for each two-character code (e.g., 49):
-
Search Environment Variable Instance__lookupTable : It searches the lookup table you loaded in the previous step.
-
using Regular Expression : The search term is %Variable%Instance__CharacterPair%\t(.+)
-
%Variable%Instance__CharacterPair% inserts the current code (e.g., 49).
-
\t matches the tab character that separates your columns.
-
(\w+) captures the translated character that follows the tab (e.g., I).
-
-
and save capture groups to... : The captured character (I) is saved into a variable named Instance__translatedCharacter.
-
-
Append Variable Instance__Translation : The found character is appended to the Instance__Translation variable, building your final string.
-
otherwise : If a code is not found in your lookup table, nothing happens by default, but you could add an action here to append an error character like a ? if you wanted.
4. What do you want to do with the translated string? (Green Section)
This final section determines what happens with the completed translation. The macro performs three actions, but you can keep (enable/disable) only the ones you need.
-
Display Text : Shows the final translated string in a window for you to see.
-
Set System Clipboard : Copies the final string to the clipboard.
-
Insert Text by Pasting : Pastes the final string into the active application.
How to Adapt This for Your Use:
To make this macro work for you, you only need to make a few small changes:
-
In the first section, change the Set Variable action to get its value from your variable VarX. It would look like this: Set variable "Instance__String2Translate" to text "%Variable%VarX%", or your Clipboard (%SystemClipboard%)
-
In the second section, disable the action that hardcodes the table (or paste its contents) and enable the Read File action . Put the path to your lookuptable.txt file in that action. Make sure that you have "2 columns" (separated by a "Tab" character "\t")
-
In the last section, keep, disable, or delete the output actions based on what you want to do with the final string.
I hope you find it useful!