Some AppleScript and TextExpander Goodies for Octopress

I’ve grown quite fond of TextExpander over the past year, but never more so than when I’m blogging with Octopress. Slowly I’ve built up an arsenal of snippets to make writing posts easier. Allow me to explain the thinking behind a few.

If you’re impatient just head to the bottom of the article for download info. And if you’re uninterested in this blogging from blogging, I’ll see you at the next post.

Rake? What Rake?

The fastest way to start up a new post is to cd into your Octopress install in a Terminal and type rake new_post["Post Title"]. This generates a brand new .markdown file in your /source/_posts/ directory with filled in YAML front matter. So with my sample rake just a few sentences back, we would get a new file called 2012-10-10-post-title.markdown pre-populated with the following:

I never really enjoyed having to go to the command line just to start a new post. Worse, I usually don’t know what I want to call a post, nor what the URL should be right away, but I like getting that YAML filled in nonetheless. Lucky for me, all I need to do is create a file in the /source/_posts/ folder with the proper file naming convention and YAML filled in. Enter TextExpander.

Editing Text via FTP

Quick sidebar here. Back in August, Gabe over at Macdrifter gave an excellent overview of how he edits text via FTP. He provides detailed instructions to set up both Sublime Text 2 and BBEdit to work with text on an FTP server. I’ve tried both but prefer the latter, with one difference: I use TextWrangler.

TextWrangler is probably the best bargain on the Mac inasmuch as it’s free. It’s essentially a stripped down version of BBEdit, but the features it’s missing hardly hamper it, if you ask me. For example, it still connects via FTP or SFTP to the server and directory of your choosing.

Following Gabe’s instructions, I set up a path right to my source/_posts directory. Now, I can open from (⌘^O) or save to (⌘^S) my server right from the keyboard. Now all I need to do is properly format that YAML and URL.

A Simple and a Not-So-Simple YAML

Let me get this URL thingamajig out of the way. Here’s what the TextExpander snippet looks like:

1

%Y-%m-%d-%|.markdown

That gives you a file with today’s date and your cursor correctly placed so you can start typing the filename. Remember, all lowercase and no spaces. I use uurl to call this one when I’m saving a file.

Now, as to the YAML front matter. I’ve got a basic one that I call with yyaml. You can dig into the snippet at the bottom to really see what’s going on, but basically it gives you a blank YAML with the time and date pre-populated and the cursor positioned to give the post a title.

Most of my posts on the candler blog, however, are link posts. For those I have to add a line to my YAML for external-url. For that I now turn to an AppleScript snippet that fills in almost everything I need to post right away.

The groundwork for this was laid by Doug Stephen with his snippet to strip out some oddities from a URL and create a new link blog type post from it. In his method he actually modified his rake task to create link posts when he entered a title with a specific format, but that’s all command line-y, so it’s no good for me. The brilliance in his work was actually stripping out the long, useless additions URLs, especially from RSS feeds, tend to have. All of that has been retained in my tweak here.

So here’s the workflow I was looking for (and have actually achieved):

Find a cool story I want to link in Safari.

Select the text I plan to quote.

Fire up a blank TextWrangler document, type TextExpander snippet.

Editorialize as needed.

The following AppleScript, when saved as a snippet, does all of that, though it’s not the snippet I prefer.

It works excellently, as advertised. And thanks to Doug Stephen’s work cluttered URLs come across clean. My one gripe was that, since the whole snippet is an AppleScript, the cursor cannot be placed within the YAML text. But I found a workaround.

Snippets All the Way Down

Until just yesterday, I had no idea one could embed a TextExpander snippet inside a TextExpander snippet. Let me say that again: you can embed a TextExpander snippet inside a TextExpander. And as many times as you like. It’s as easy as adding putting in a snippet placeholder.

With that in mind, I created separate snippets from the AppleScript above to get the page title, the URL and the selected text; three separate AppleScript snippets. Then I created my snippet for link post YAML front matter incorporating the snippets for title, URL and quoted text appropriately.

The main advantage here is that users of other static blogging engines can incorporate the AppleScript to fit their YAML template. Additionally, it’s much easier to change the text output from within TextExpander than to modify the ray AppleScript.

Download

I’m using Brett Terpstra’s nifty TextExpander tool to help you make some sense of the snippets I’ve listed above. You can see them all laid out here. Please note that I haven’t implemented Brett’s “prefix” system here, so the expansion abbreviations won’t change if you add commas as a prefix. I recommend downloading the snippets and adjusting their abbreviations as needed. Here’s the download link.