Help With Embedding a Calculation Within an Email

Hi All,

I've hit a wall trying to do something in TextExpander, and wonder if KM could have an answer. I want to send an email that contains the person's name in a couple of places, which is fine using TE's fill-in snippets. But I also want to introduce a calculation where I want them to increase their contribution by 25%. So the email would have a pop-up where I'd enter an amount such as $4.00 and then a calculation would convert the increase of 25% so they'd see $5.00 as the new amount. In plain silly English (as an example!)

Dear Dr. Who, I'm so happy to have you as a supporter! You know, Dr. Who, last year you contributed $4 toward my jelly bean fund, and if you gave 25% more for my 25th birthday, your gift would only be $5 or only $1 more. Thank you Dr. Who for making my 25th year so memorable."

I looked at the calculation variable and at the wiki about it, and my number-challenged brain isn't putting the pieces together right. So I'm hoping that someone will find my invoking Dr. Who to make them smile will help me figure out a macro. Alas, I don't do script, though I have aspirations....

Could I do all of this using KM, or am I better off incorporating elements of TE into KM?

Thank you!

Hey @Pommette,

Here's a very basic proof-of-concept.

You can get very sophisticated if you want to.


Email Template ⇢ Add 25% to Donation Amount v1.00.kmmacros (8.7 KB)


Keyboard Maestro Export

Thanks @ccstone! What a great teach-the-gal-to-fish experience, truly appreciated.

So I copied your macro line by line, and when I trigger it, the box comes up to input variables just fine. But after entering them, the KM display window opened where my cursor was, but without the entered information--just the script. Also when I clicked on the ok button for that box, it went away without leaving any text at all.

I'm using Apple's Mail app, and I assume I'm supposed to trigger the macro where I want the text to appear, right?


Why would you copy it, when I've provided a downloadable macro for you to import?

Import and run my macro. It should do what it's supposed to do correctly.

You don't expect me to do all your work for you do you?  :sunglasses:

See the Send Mail Message action.


Thanks @ccstone, and sorry for the slow response.
Hilarious! I learned by typing though. Any advice about where to start at the very beginning?)
Ok, I downloaded it and ran it, and yay- it works perfectly, thank you! So now for the "still clueless part": should I be running it from within the Mail app where I'd be typing the message? Or should I fill in the name and amount as prompted, then add actions to copy the text that's in the KM Display Text box, click ok, open a new message in mail, then paste?
I realize that for people immersed in KM my question must sound like asking if I need to pick up my foot to walk. But I'm really good at a few things, and this is stretching me to new ones. :slight_smile: Thanks again!

Hey @Pommette,

You haven't really explained your workflow step-by-step.

For instance:

  1. How many of these things are you sending at once?
  2. What is the source of your email address?
  3. What is the source of your original donation amount?

In my macro the display text action is simply to show you a result.

You can take that result and do other things with it.

You can put it on the clipboard.

You can create a new email with it using an Send Mail Message action.

You can paste it into the front message in Mail?


Without understanding your process I really can't advise very clearly.


Fair questions (and useful) @ccstone!
I'm sending a version of the same email to multiple people. Here's how I imagine doing it:

  1. Open an email in the Mail app
  2. Type the address
  3. (Here's where the automation would start): paste in subject line with something personal like Pommette to [name]: help celebrate our 25th birthday
  4. Jump to message area
  5. Text in message area: Dear [name] followed by an opening sentence or two, followed by the part you've helped me with: [last year's amount] with a 25% increase meaning a contribution this year of [new amount]. Thank you, [name]. Signature.

After hitting a trigger key, I'd enter the name and the original amount, KM magic would happen, and it would be ready to send. Maybe I'd add a personal tweak or two, then I'd hit send.
Open new email, repeat.

I hope this helps you help me!
Thanks again @ccstone!

I’d recommend considering using a Mail merge app like Serial Mailer.

Thanks @vincent_ardern. If this was a bigger job, I'd consider it. But this is about 40 emails max. Good to know though.

Hey Pommette,

You're going about that in a very labor intensive way.

Create a text file with this format:

(I recommend using BBEdit, but any plain text editor will do including TextEdit.)



Save it somewhere – my choice is:

~/Documents/Mail Merge/Pommette – Mail Merge List.txt

Alter the path in in the orange Read-File action if necessary.

Run the appended macro.

TEST with only a couple of sets of data to make sure it's working.

Once it's working emplace all your data, and run the macro.

N emails will be created. You can then edit them as desired and send them.

This task can be adapted to use a spreadsheet in Excel or Numbers instead of a text file.


Email Template ⇢ Add 25% to Donation Amount v1.50.kmmacros (20 KB)
Pommette – Mail Merge (1005 Bytes)


Thanks @ccstone! I tried this a few times with a number of variations, but the macro doesn't run. I think it's because of my basic scripting illiteracy--as closely as I try to follow your excellent instructions, I suspect I'm in that place where I'm doing the equivalent of looking for the "ANY" key. So don't laugh too hard as I explain what I did:

  • Opened TextEdit
  • Created a file named Pommette – Mail Merge List & saved it to the Documents folder where I believe it's saved as ~/Documents/Pommette – Mail Merge List.txt (at first it was saving as .rtf so I changed to plain text in preferences; it doesn't display an extension, but I'm assuming it's .txt)
  • I replaced the path in the Orange Read File action to: ~/Documents/Pommette – Mail Merge List
  • Meanwhile, in the TextEdit document called Pommette - Mail Merge List, I typed this as the first line (thinking this was what you meant by "with this format"):

Followed immediately (no line breaks) with the names & addresses (these are made up, but I entered real ones):

George Jetson $4.00
Homer Simpson $8.00
Peter Pan $100.00

If you're throwing up your hands, please tell me the best place to get basic scripting literacy to make the best use of KM. I want to learn, but I need extreme basics, like how writing the letters A, B, C. We all start somewhere!


Hey @Pommette,

Personally I think no one who uses a Mac should turn OFF file extensions in the Finder preferences.

Screen Shot 2021-11-07 at 07.22.53

When they are on you can tell at a glance what file type you're working with, and you don't have to guess what your path is.

See the difference in the two path strings? If you did in fact use the second string, then you have an incomplete path.

Never type a path string if you don't have to.

The Finder will let you copy paths of an item by typing C.

  • Select a file in the Finder.
  • Hold down the Option key.
  • You'll see Copy change to Copy as path.

You can also drag and drop items into Keyboard Maestro's path fields, and there's a file-selector button as well.

This line does not belong – it was just to illustrate the table format:


What belongs in in the data file is a tab-separated table like I thought I provided... I meant to include an example file along with the macro, and it looks like I failed to do so. (Now remedied in my post #10 above.)

Here's what it should look like:

Robert Heinlein			5.00
Roger Zelazny				10.00
  • Each row is a data record.
  • Each tab-separated value is a data field.

The fields may be separated by 1 (or more) tab characters to make a nice neatly ordered table.

One reason I'd have preferred that you used BBEdit for this task is that you can't accidentally save as RTF or RTFD.

It also has many tools for dealing with text that TextEdit does not have.