How to create a Keyboard Maestro Counter and Read Text From a File

The next step in our Keyboard Maestro series is to create a macro that increments a counter, which we'll use to create a sequence number for every email we send, and we'll read text from a text file that we'll use to populate the address fields in our email message.

Creating a Counter

As a reminder, the macros we're creating are based on macros I use to address and send email messages to members of a production staff for plays that I'm stage managing. In order to make these messages easy to find and catalog, every email I send out has specific information in it: The show name, the show season and/or the date of the report, what type of report it is, and the number of the report in sequence. Most of the information used to do this is static text or can be pulled using system variables, but in order to sequence each email message, I need to keep track of the previous sequence number using a variable, add "1" to that variable, and update the variable to today's sequence number. I call the macro I use to do this a Counter. To create this counter you're going to use the variables and clipboard we created in our last segment.

Here's how the Counter works: The clipboard we created, RRCounter, stores the last used number in our email sequence. Remember: Keyboard Maestro's named clipboards are able to retain information stored in them through restarts, so even if I have to restart my Mac, that number is still there. Our macro needs to read the number stored in RRCounter, store it in the variable CurrentCounter, perform some simple math on CurrentCounter, then store the result in RRCounter for the next time we need to increment the number.

Here's how we do that:

  1. Create a new Macro in your iMore Macros group called Counter
  2. Click the "+" next to New Action
  3. Use the search bar to find the Action called Set variable to text and add it to your macro
  4. In the Set Variable field type the text "CurrentCounter"
  5. Select the Insert token menu that appears above the To: field
  6. Select Named Clipboard
  7. Select the RRCounter clipboard

This action should look something like this:

Next we need to perform the calculation:

  1. Find and add the Set Variable to Calculation Action
  2. In the Set Variable field type "CurrentCounter"
  3. In the To: field type "CurrentCounter + 1"

This Action should now look something like this:

For the final step in our calculation we need to replace what's on the clipboard with the new calculation:

  1. Find and add the Set Named Clipboard to Text Action
  2. Using the menu next to Set select "RRCounter"
  3. From the menu next to To select "Plain text"
  4. From the Insert Token menu select "Variable" and then select "CurrentCounter"

Your completed action should look like this:

Read Text from a Text File

Our final step for this segment is to read text from a text file into a variable so we can paste it into the appropriate fields in an email message. Why read this info from a file? Because it provides a simple way to get and update the information we need, anyone can add an email address to the text file and it makes it simple to reuse the macro in the future. All you need to do is make changes to the text files and you're done.

Here we go:

  1. Create a new macro called "Read and Paste Crew"
  2. Add the Read a File action
  3. In the Read File field use the folder icon to browse for and locate the file you created for the Cast email list in our last segment
  4. Click the to menu and select "Named Clipboard"
  5. From the new menu that appears, select your "Crew Email Addresses" clipboard
  6. Now add the Paste from Named Clipboard action
  7. Select your "Crew Email Addresses" clipboard
  8. Close the Action window

You should see something like this:

Now, rather than repeat this process, we're going to copy the existing Macro and just make changes to it for the Cast email addresses.

  1. Select your "Read and Paste Crew" macro
  2. Use Command-C or use the Edit->Copy menu to copy the macro
  3. Type Command-V or use the Edit->Paste menu to paste a ciopy of the macro
  4. Rename your new macro "Read and Paste Cast"
  5. Change the Read File so it reads your "Cast Email Addresses" file
  6. Change the clipboard so it copies to your "Cast Email Addresses" clipboard
  7. Change the Paste from Named Clipboard action so it pastes from the "Cast Email Addresses" clipboard

When you're done it should look something like this:

Next, we're going to combine these actions with others to open the Mail app and automatically address our email.

Jeffery Battersby

Jeff is a writer, actor, Apple Certified Trainer, and IT consultant, born and raised in A-town and now living in NY. You can often catch him behind the scenes and on stage at County Players, Falls Theatre. Up next? He's stage managing *Cat on a Hot Tin Roof* at the aforementioned County Players.