How Text Files Help Me Automate Common Text Expansions

I‘ve discussed how I use TextExpander to speed up commonly typed things (like my email address). Or things that I can’t always remember (like my home phone number). Recently, I’ve been taking a closer look at what I type over and over again, and have done some automation that allows me to automatically expand another common thing I type: what book I am currently reading.

A while back, I explained why I love plain text files, and gave an example of how I use them to maintain my reading list. One of the text files I have in my public dropbox folder is what I call now.txt. The contents of now.txt is nothing more than what I am currently reading, in a simple format. Today, the entire file looks like this:

John Adams by David McCullough (B000FC0QHA)

where the code in parentheses is the Amazon product code.

Whenever I start a new book, I update my now.txt file with what I am currently reading, and include the Amazon product code. I type it into this file once, and never have to type again.

That is because I have taught TextExpander to read the file and parse it into one of four different snippets. Reading and parsing the file is simple. Since the now.txt resides in my public Dropbox folder, and since that folder is synchronized with my Mac, the file is always accessible from my Mac. So I wrote some simple UNIX bash scripts to parse the file.

TextExpander allows you to create expansions out of shell scripts, and so I can easily call my UNIX bash script as an expansion in TextExpander. The four expansions I have created are:

1. ;;book: Whenever I type this shortcut, it expands into a line of text containing the title and author of the book that I am currently reading (the one that is in my now.txt file.) It expands wherever TextExpander works, which on my Mac is everywhere. So I never have to retype the book I am currently reading. If someone asks in email what I am currently reading, I can reply and use my shortcut, which will expand to whatever my shell script parses out of my text file. Right now, when I type the shortcut, it expands to this:

John Adams by David McCullough

Of course, if I change the contents of the file, the results of the shortcut expansion changes as well. Here is the shell script I run for the text expansion:

#!/bin/bash
cat /Users/<login>/Dropbox/Public/now.txt | sed 's/(.*)\(.*\)/\1/' %<

I won’t bore you with what it all means. Just understand that if the text is in the format I listed way up above, this will parse it correctly. Also, substitute your login name where it says <login> in the path. In TextExpander, it looks like this:

TextExpander Current Book snippet

2. ;;title: This expands to just the title of the current book I’m reading, as opposed to title and author. So right now, it expands to:

John Adams

The code for this snippet is:

#!/bin/bash
cat /Users/<login>/Dropbox/Public/now.txt | sed 's/\(.*\).*by.*/\1/' %<

3. ;;author: This expands to just the author of the book I’m currently reading. Right now it expands to:

David McCullough

The code for this snippet is:

#!/bin/bash
cat /Users/<login>/Dropbox/Public/now.txt | sed 's/.*.*by.\(.*\).(.*/\1/' %<

4. ;;amazonlink: Sometimes people ask for more information, or I decide to embed a link to the book in Amazon. This usually requires me looking up the book in Amazon, and copying the link. I sometimes do this a dozen times. Rather than do that, I include the product code in my now.txt file. Typing this shortcut then expands to the full URL to the book in Amazon:

http://www.amazon.com/gp/product/B000FC0QHA

The code for this snippet is:

#!/bin/bash
cat /Users/<login>/Dropbox/Public/now.txt | sed 's/.*.*by.*(\(.*\))/http:\/\/www\.amazon\.com\/gp\/product\/\1/' %<

This means that I only need type the information about what I am currently reading once, in my now.txt file. After that, so long as I keep that file up-to-date, my text expansions will always expand using the values in that file. You can imagine plenty of other examples of this for common information you type regularly, but that also changes fairly regularly.

Enjoy these posts? – Tell a friend

Recommending readers is one of the highest compliments you can pay to a writer. If you enjoy what you read here, or you find the posts useful, tell a friend! Find me online here:

Twitter | Facebook | Google+ | Blog | RSS

Or use one of the share buttons below. Thanks for reading!

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.