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:
- Create a new Macro in your iMore Macros group called Counter
- Click the "+" next to New Action
- Use the search bar to find the Action called Set variable to text and add it to your macro
- In the Set Variable field type the text "CurrentCounter"
- Select the Insert token menu that appears above the To: field
- Select Named Clipboard
- Select the RRCounter clipboard
This action should look something like this:
Next we need to perform the calculation:
- Find and add the Set Variable to Calculation Action
- In the Set Variable field type "CurrentCounter"
- 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:
- Find and add the Set Named Clipboard to Text Action
- Using the menu next to Set select "RRCounter"
- From the menu next to To select "Plain text"
- 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:
- Create a new macro called "Read and Paste Crew"
- Add the Read a File action
- 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
- Click the to menu and select "Named Clipboard"
- From the new menu that appears, select your "Crew Email Addresses" clipboard
- Now add the Paste from Named Clipboard action
- Select your "Crew Email Addresses" clipboard
- 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.
- Select your "Read and Paste Crew" macro
- Use Command-C or use the Edit->Copy menu to copy the macro
- Type Command-V or use the Edit->Paste menu to paste a ciopy of the macro
- Rename your new macro "Read and Paste Cast"
- Change the Read File so it reads your "Cast Email Addresses" file
- Change the clipboard so it copies to your "Cast Email Addresses" clipboard
- 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.
Get more iMore in your inbox!
Our news, reviews, opinions, and easy to follow guides can turn any iPhone owner into an Apple aficionado
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.