24/9/2017

Using Alfred to start a Blot draft

After a bit of experimentation, I decided to use a Unix Epoch datetime stamp (i.e. the number of seconds elapsed since 1 January 1970) as the basis for both the filename of my Blot posts, and also the permalink. It’s not the most transparent bit of information to use, but some of my posts are short status posts with no title, so it seemed the most consistent and efficient form to use.

This is fine for posts that get created via Zapier by cross-posting from other services, but when I come to create my own drafts manually, it is a bit more fiddly than I would like, as I have to get the current datetime in the Unix Epoch format.

So I decided that I would write a little script to use as part of an Alfred workflow, which would create the file in the Drafts directory, create the correct filename and corresponding Date: entry in the front matter, and then open the file in Sublime Text for editing.

My Alfred workflow to create a draft Blot postMy Alfred workflow to create a draft Blot post

It’s pretty simple. I use a keyword (‘nsd’ for new Slipstream draft, in my case) to run the following Python code:

#!/usr/local/bin/python3

from datetime import datetime
import sys

query = "{query}"

now = datetime.utcnow()
drafts_dir = "/Users/bsag/Dropbox/Apps/Blot/Drafts"

u_stamp = now.strftime("%s")
f_name = "{0}/{1}.txt".format(drafts_dir, u_stamp)

date_tag = now.strftime("%Y-%m-%d %H:%M:%S")
date_tag = "Date: {0}\n".format(date_tag)
type_tags = "Type: post\nTags: {0}\n\n".format(query)

front_matter = date_tag + type_tags

with open(f_name, 'w+') as f:
    f.write(front_matter + '\n')

sys.stdout.write(f_name)

I enter the comma separated tags I want to use in the Tags: field in the Alfred command window, after the keyword, and these get passed to the script via the query variable. At the end of the script, the full filepath is passed to stdout, and then the Open File action opens that file with Sublime Text.

It works really nicely, and could be adapted to pre-enter more information in the draft. I love Alfred workflows for this kind of thing: it’s really easy to cobble together a little workflow that makes tasks like this a bit easier.


post coding


Previous post
I’m So Excited!, 2013 - ★★★★
Next post
Yurt Lush veggie breakfast!