Seeking Guidance on Conditional Text Replacement in Keyboard Maestro

Hello Keyboard Maestro Community,

As a newcomer to this powerful tool, I'm in need of some assistance with a specific script. My goal is to modify certain text segments within select emails, but I'm facing some challenges due to the variability in their formatting. Specifically, I'm trying to find the first instance of "From:" and replace the ensuing header with a simplified version, such as "On Wednesday, November 29, 2023 at 19:35, 'First Last' wrote:". This script is intended to run only on certain emails, not all.

Here's an example of the standard format I'm dealing with:

From: First Last <first.last@example.com>
Date: Wednesday, November 29, 2023 at 19:35
To: to@example.com
Cc: cc@example.com
Subject: New list

The main issue arises with the 'From' field, which might contain just a first name at times, and the occasional absence of the 'Cc' field. I would be extremely grateful for any advice on crafting a macro in Keyboard Maestro that can effectively handle these variations.

Thank you all in advance for your time and assistance!

Warm regards,
Marjorie

Hi, @Macaniac; welcome to the Keyboard Maestro forum.

Since you are a new forum user, I'm going to refer you to four references that will make your time here much more efficient:

Once you've read the above information and dive in, you'll find that there are many friendly users that are happy to assist.


Regarding your objective, I have a couple questions/comments:

  • What email application are you using?

  • Finding and selecting the text block to be replaced would be the most difficult, if not impossible (depends on the email program), automation challenge. However, if you selected the text, and triggered a macro (e.g., with a hot key) it is relatively easy to replace the selected block with the format you specified. See below.


Download: Replace Selected Mail From Block.kmmacros (7.9 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.2 (23C5030f) PRE-RELEASE SEED SOFTWARE
  • Keyboard Maestro v11.0.1

Thanks @_jims
I am trying to create this macro for Outlook for Mac app. In your macro, the output is coming in two lines:

On Monday, December 4, 2023 at 10:25
, 'Microsoft' wrote:

Thank you for helping!

Hi, @Macaniac. Would you please share the block of text you are selecting before you trigger the macro?

@_jims here is the block

From: Microsoft <microsoft-noreply@microsoft.com>
Date: Monday, December 04, 2023 at 10:25
To: Name <name@example.com>
Subject: You’ve renewed your Microsoft 365 Business Standard subscription

Interesting. It works for me in Apple Mail or any text editor that I tried.

2023-12-04 15.00.56


It appears that there is some odd issue with Outlook.

Here's a new version that includes two other options to try.

Download: Replace Selected Mail From Block.kmmacros (14 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 14.2 (23C5030f) PRE-RELEASE SEED SOFTWARE
  • Keyboard Maestro v11.0.1

I'd try Option 2 first and if that doesn't work, disable Option 2, enable Option 3 and give that a try.

Keyboard Maestro Export

Sorry I don't have Outlook installed; otherwise I'd test it myself.

MS using \r line delimiters ?

as in:

s.split(/\r\n|\n|\r/u)

?