## Building the Macro

### What I’m Building

In this tutorial I’ll create a journalling macro. When I press a keyboard shortcut, I’m prompted to create a journal entry. This is saved to a text file.

The way the macro works, if I create another entry the same day, it will be added to the same text file. The macro is also customisable so I can have a new text file for every day, week or even month.

### Getting the Journal Input

The first step in any journal is to get the actual journal entry. To do that, I need two things:

1. Some way to trigger the macro
2. Some way to capture the entry

Start by creating a new macro. Call it something like Quick Journal

For the trigger, you have a few options. If you want to journal at the same time every day, give it an At Time trigger. If, like me, you want a more freeform journal, then a Hotkey Trigger is best. I’ve used the shortcut Command-Shift-, but use whatever you wants.

The first thing the macro does is capture the journal entry input. Add a Prompt for User Input from the Variables section.

The only thing I need to get is the journal entry, so click the Plus button under Variables and Default Values and create a variable called Journal. I’ve given it a default value of No Entry which I’ll use later to make sure I’m not accidentally adding journal entries.

The buttons, OK and Cancel are okay, so other than changing the Title and Prompt I don’t have to do anything.

### Testing for a Journal Entry

Now that I’ve got a way to capture a journal entry, I need a way to ensure I don’t journal by mistake. This means using a Control Flow action to test that the journal I’ve entered has some value.

From the Control Flow category, add an If Then Else Action. Set the conditions so that If All the Following Are True The Variable Journal Is Not No Entry

This means that as long as I enter anything other than No Entry into the journal dialog, it will be saved. If I leave it to the default value, it won’t.

### Saving the Date to a Variable

For the journal, I want each day (or possibly week) to have its own text file. I need to create a text file, but first, I need to have some way to name each file uniquely. Since this is a journal, I want each file named with the date. This means I need to store the date in a variable so I can use it later.

From the Variables category, add a Set Variable to Text action under Execute the Following Actions

Call the Variable something like JournalDate. Either click on Insert Token > Date > Formatted ICU Date Time and edit it to say %ICUDateTime%YYY-MM-d% or just enter the text directly. This sets the variable JournalDate to the current year, month and day, for example, for today, it would set the variable to 2018-01-22.

Note: If you’d sooner have the macro create weekly files rather than daily ones, set the JournalDate variable to %ICUDateTime%YYY-MM-w%.

### Saving the Entry to the Text File

With the journal entry and the day’s date recorded to variables, we need some way to save them to a text file. From the File category, add an Append Text to a File Action.

I want to be able to journal multiple times in one day, which means we need to have each journal entry proceeded by a time stamp. Under Append Text, enter:

Ensure you add the new line after the underscore. Otherwise, you’ll have formatting issues.

Next, for to file, click the purple folder and use the dialog to navigate to the folder where you want to save your journal entries.

Give them the title Journal %Variable%JournalDate%.txt; this means the macro will create a new text file every day or week called Journal [Date] the first time you run it.

Since I used an Append action rather than a Write action, if the file already exists the date will be added to the end.

Next add another Append Text to File action. Click on Append Text and change it to Append Variable. Enter Journal for the value and use the same File Address as in the previous step.

The final thing to do is add a separator at the end of the journal entry. Add another Append Text to File Action and give it the value of:

Again, watch the line breaks before and after, and make sure the file address is the same as in the previous steps.

And with that done, the macro is ready to use.

### Finishing the Else Statement

Since I used an If Then Else Statement, the macro won’t run if I don’t enter a journal entry.

While having the macro fail silently is fine, it’s better than I’m notified it fails.

From the Notification category select Notification and drag it under Otherwise Execute the Following Actions. Set the Title to Failed to Enter a Journal

Now, if the journal macro fails, I get a notification.

## Wrapping Up

Writing to text files is a very useful Keyboard Maestro function. It’s a simple way to track or log things. Keyboard Maestro can create or add text to files as you need.