the candler blog

Some AppleScript and TextExpander Goodies for Octopress

Blogging, Octopress, Technology

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:

1
2
3
4
5
6
7
---
layout: post
title: "Post Title"
date: 2012-10-10 12:54
comments: true
categories: 
---

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):

  1. Find a cool story I want to link in Safari.
  2. Select the text I plan to quote.
  3. Fire up a blank TextWrangler document, type TextExpander snippet.
  4. Editorialize as needed.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
on replaceCommasInStringWithHTMLEntity(theTitle)
	set rubyCommand to quote & "puts " & "'" & theTitle & "'" & ".gsub( /,/, ',' )" & quote
	set strippedTitle to do shell script "ruby -e " & rubyCommand
	return strippedTitle
end replaceCommasInStringWithHTMLEntity

on stripUTMFromURL(urlToStrip)
	set rubyCommand to quote & "puts " & "'" & urlToStrip & "'" & ".gsub( /\\?utm.*$/, '' )" & quote
	set strippedURL to do shell script "ruby -e " & rubyCommand
	return strippedURL
end stripUTMFromURL

tell application "Safari"
	set pageTitle to name of document 1
	set currentURL to URL of current tab of window 1
	set selectedText to (do JavaScript "window.getSelection().toString()" in document 1)
end tell

set pageTitle to replaceCommasInStringWithHTMLEntity(pageTitle)
set currentURL to stripUTMFromURL(currentURL)
set theDate to current date

return "---" & "
" & "layout: post" & "
" & "title: " & "\"" & pageTitle & "\"" & "
" & "date: " & (do shell script "date +%Y") & "-" & (do shell script "date +%m") & "-" & (do shell script "date +%d") & " " & (do shell script "date +%H") & ":" & (do shell script "date  +%M") & "
" & "comments: true" & "
" & "categories: " & "
" & "- " & "
" & "external-url: " & currentURL & "
" & "---" & "
" & "
" & "> " & selectedText

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.

And that’s about it. Happy blogging.

Comments