Advertisement
  1. Code
  2. Coding Fundamentals
  3. Workflow

Working With Files in Keyboard Maestro: Part 3

Scroll to top
Read Time: 6 min

In the previous two tutorials in this series I've shown you some of the ways Keyboard Maestro can interact with files. 

I started by building a macro that takes a group of files and renames them as a sequence before moving on and exploring how to trigger macros using folders

In this tutorial I'll show you how a macro can create and write to text files.

Prerequisites

To get the most from this tutorial, you need to have read the two previous tutorials in this series:

You will also find the tutorial easier to follow if you’ve read my five part introductory series that covers the basics of Keyboard Maestro:

Finally, I’ve also shown you some specific functions in Keyboard Maestro. While these tutorials aren’t required reading, they will help you see how all the different functions of Keyboard Maestro can be combined.

For obvious reasons, you will need a copy of Keyboard Maestro. It’s $36 but there’s a free trial so you can check it out before committing.

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.

journaljournaljournal
The saved journal file.

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.

new macronew macronew macro
Creating a new macro.

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 promptthe promptthe prompt
The prompt action.

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

control flowcontrol flowcontrol flow
The control flow for the macro.

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

creating date variablecreating date variablecreating date variable
Storing the date as a variable.

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:

1
%LongTime%
2
3
_

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.

timestamptimestamptimestamp
This action timestamps each entry and creates the file if needed.

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. 

adding journaladding journaladding journal
This adds the actual journal entry to the file.

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:

1
_

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

the final stepthe final stepthe final step
The final action sets the file up for a new entry.

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

failed notificationfailed notificationfailed notification
If the macro fails, I want to know about it.

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.

finished macrofinished macrofinished macro
The finished macro.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.