Hacking My Blog Post Workflow

This blog runs on a little blogging engine called Toto. It’s entirely .txt file based, which is neat for a few reasons:

  • There are no databases to deal with
  • Posts are preserved in a future-friendly format
  • Everything can be easily backed up using Dropbox, Time Machine, etc.

These things all make Toto awesome, but the process of creating a new post was causing friction for me, so I decided to hack my way to a solution using a little Ruby, and my ol' friend Alfred App.

The Problem

Before today, getting a new post started was a bit of a process. Before starting to write I had to:

  • Find and then duplicate an existing post
  • Change the filename of the new file
  • Open the file in a text editor and
  • Delete the duplicated content of the new file
  • Update the metadata of the new file
  • Start writing

The Solution

I wanted to be able to hit command+space, and then type something like: “blog: Hacking My Blog Post Workflow”, and have that automatically complete the friction steps I mentioned earlier. Here’s what I wrote in a file called newpost.rb – but first a disclaimer: I don’t really know how to program, and this code is probably terrible to even the semi-trained eye.

dateDashed = Time.new.strftime("%Y-%m-%d")
dateSlashed = dateDashed.gsub('-','/')

def prompt(default, *args)
  result = gets.strip
  return result.empty? ? default : result

title = prompt "New", "Article Title: "
urlTitle = title.gsub(' ', '-').downcase

content = "title: #{title}\ndate: #{dateSlashed}\nkind: article\n"

filename = "./articles/#{dateDashed}-#{urlTitle}.txt"

File.open filename, 'w' do |f|
    f.write content

system('open #{filename}')

This little program grabs the current date, and saves it in two formats, then asks me what the post title should be. Once it’s gathered that information it creates a new file using the date and title for the filename, and the post’s in-file metadata, and then opens that file for me in a text editor.

When I finished writing this, I was just running the file from the terminal like so:

ruby newpost.rb
> Article Title: Hacking My Blog Post Workflow

This worked, but still felt like I had to do too much work.

Enter Alfred

Fortunately I’ve been using a great little utility called Alfred. Alfred helps you with launch applications, quick calculations, checking spellings and definitions, and other useful things like that.

Just this week I discovered that Alfred also has the ability to run some commands in terminal for me, and even pass input from me to the terminal. I took advantage of this and created a new Extension that looks like this:

cd ~/Blog
ruby newpost.rb

This jumps to my Blog folder, runs my ruby file that I wrote earlier, passes it the query that I gave to Alfred, and then closes the terminal window.

Final Product

Now when I want to write a new post I just hit command-space then type “blog: Some New Post Title”. About half a second later my text editor pops up with my newly created post file. Beautiful.