Using Keyboard Maestro to create new Jekyll posts
As I mentioned recently in Up and running with Jekyll, I hadn’t given any thought whatever to how I was actually going to go about automating the creation of new posts. Tonight I decided I would tackle that and ended going with a pure Keyboard Maestro solution. (I may eventually end up just copying the AppleScript solution I came up with in Blogging with TextMate and Chromium/Chrome—which would be pretty easy to modify to handle the YAML front matter stuff I now need because of Jekyll—but I’m going to stick with the KM approach for now and see how it goes.)
The image below shows the entire KM macro I built for creating new linked-list posts. (The macro for regular posts is similar, but much simpler, and easily derivable from the following discussion.) While most of this macro probably is pretty self-explanatory, certain parts of it definitely aren’t, and so beneath the image I explain my thinking around those elements.
The first thing the macro does is invoke the If Then Else
action to determine whether I modified my hot key trigger for the macro with “Q”. I chose “Q” simply because this key press is meant to indicate whether I want to quote something in the body of the linked-list post. If “Q” was pressed along with the hot key trigger then we ensure that the variable doQuote
isn’t empty; if “Q” was not pressed, then I’m not quoting anything from the article to which I’m linking and doQuote
should remain empty. We’ll test for this later.
Next we save to the variable Quote
what’s currently on the clipboard, which will correspond to what we want to quote, assuming we want to quote something; if we don’t want to quote something, then we’ll never use this variable.
Because this macro is meant to get invoked from your browser while you’re on the page you want to link to, the next few steps simply copy the address of the current page and save it to the Link
variable.
Once this is done we display a dialog that asks for both the slug and the title of the post. The Slug
variable is put ahead of the Title
variable so that the keyboard focus will be on the input field for the slug, as that will always need to be entered, whereas the title may be fine as it is. (You of course can change this around however you like, and can even automate entirely the creation of both; that said, I prefer to define both explicitly.)
The next couple of steps create the file for the new post and open it in the text editor you specify. You’ll notice that both of these steps require the full path to where you keep your blog source files, and then it builds up the file name from ICU date/time tokens and the Slug
variable we defined earlier.
The Open a File, Folder or Application
action doesn’t actually work unless the file exists, and it seems KM doesn’t offer the ability to actually create a file (outside of simulating keystrokes, etc.). This limitation caused me to give the Write to a File
action a go, and it turns out that this will create the specified file if it doesn’t exist, and the newly-created file doesn’t need to contain anything (kind of like the Unix touch
command).
Finally, the macro uses the Insert Text by Pasting
action to fill out the YAML front matter for the new post. We test whether the doQuote
variable is empty; if it’s not empty then we include a blockquote
for the text we copied before invoking the macro, and if it is empty then we don’t include that text.
You’ll notice that the external-url
element gets the URL of the page to which we’re linking; this is the element I test against in all of my templates to determine whether I’m dealing with a linked-list post (though, yes, I could do it based off of category as well).
While you don’t have to have the date
element in the YAML header (as the year/month/day is pulled from the file name itself), it’s necessary for keeping post order correct when multiple posts are published on the same day. Similarly, the slug
element isn’t needed here either (because it too is defined by the file name), but I like to have it in here for all of my posts because it doesn’t hurt anything and could come in handy with future migrations.
The very last Set Variable
action simply “resets” the doQuote
variable; it seems its value persists between invocations of the macro and so sometimes this breaks the logic that’s based on whether it’s empty.
Alternatives
Obviously, instead of adding the quote/no-quote logic to the KM macro, you could just create two separate macros and call them with and without the Q key (or whatever it is you decide on), but I just preferred to have all of this in one macro.
Before the quote/no-quote stuff, I had it setup so that the ‘potential quote’ (i.e., whatever was on the clipboard before the macro got started) would be shown to you in the slug/title pop-up dialog, and if you didn’t want anything pasted in you could just delete whatever text was in this field. This, clearly, was not ideal, and I gave it no more thought once I was up and running using the If Then Else
actions.
Finally, before settling on the flow shown in the image, I had an alternative version that worked by creating a new file from within your text editor of choice (using simulated keystrokes), enacting the “Save As…” dialog (using simulated keystrokes), getting to the proper directory via the Insert Text by Typing
action and having KM type the full path (minus the file name), and then using the Insert Text by Pasting
action to generate the proper file name using the variables previously discussed. This worked just fine, but it wasn’t very elegant, and required Pause
actions to be inserted in a couple of places because the GUI couldn’t keep up with the macro.