Advertisement
  1. Computer Skills
  2. TextExpander
Computers

Text Expander III: Scripting Fill Ins

by
Difficulty:AdvancedLength:MediumLanguages:

TextExpander is a powerful Mac productivity app. It automatically expands keyboard shortcuts—or abbreviations—into longer blocks of text—snippets

If you repeatedly send the same basic emails or write the same information, such as an address or phone number, TextExpander makes life easier.

In the previous two tutorials, in this series, I’ve looked at getting started with TextExpander and how to use more advanced features like fill ins and time and date macros.

In this tutorial, I’ll look at using scripting languages like JavaScript and AppleScript in fill ins.

Screencast

TextExpander and Scripting Languages

TextExpander supports three scripting languages: Shell Script, JavaScript and AppleScript. For the purposes of this tutorial I’m going to be focussing on the latter two as they are the most flexible. With scripting languages you can add logic to your TextExpander fill ins.

For example, in one of the example snippets in this tutorial, I use the fill in returns a different result depending on what day it is when you use it. Another example snippet returns a random response from an array of multiple possible responses. These are both impossible to do in TextExpander without a scripting language.

Of the three offered, JavaScript is the most useful as it also works with TextExpander Touch, the companion iOS app I’ll look at in the next tutorial in this series.

Prerequisites

To use this tutorial you’ll need the latest version of TextExpander installed. It costs $44.95—although there’s a 30-day free trial—and can be downloaded from the developer’s website.

You’ll also need to be familiar with TextExpander and fill ins. If you aren’t already, the best way to get up to speed is to check out the two previous tutorials in this series:

Further, in this tutorial, I’m using some very simple JavaScript and AppleScript. You don’t need to be familiar with either language although it helps. 

To learn more about JavaScript you can check out this Code.Tuts+ tutorial and to learn more about AppleScript you can check out this tutorial right here on Computers.Tuts+.

Using Simple Logic With JavaScript

The first snippet I’m creating returns either I’ll get back to you tomorrow or I’ll get back to you after the weekend depending on whether it’s a weekday, or a weekend day—including Friday.

For this snippet I’m using JavaScripts’ built-in date function. For more information on the specifics, you can find the documentation here.

Create a new snippet and set its Content to JavaScript. Assign it a simple and relevant abbreviation.

Next, you need to create a variable to hold today’s date information. To do that, enter var today = new Date(); as the first line of the snippet.

On the next line, enter today.setDate(TextExpander.baseDate.getDate());. This sets the today variable to the current date.

The result of a snippet that uses a scripting language by default is the last statement executed in the snippet. 

To finish, you need to check whether today is a Friday, Saturday or Sunday. The following if loop does that and, depending on the result, returns one of two possible sentences. Add the code below to your snippet to finish.

When you enter the abbreviation, depending on the day, you’ll either tell people you’ll respond tomorrow or after the weekend.

Tip: Remember, snippets can be embedded within other snippets. This day dependant reply snippet can be used within any longer email snippets you have.

Creating a Randomised Fill In With JavaScript

One issue with snippets is that they can be very rigid. If every one of your emails has the same structure with little or no variation, you’ll appear stuffy and overly formal. With scripting languages you can overcome this by adding some randomness to your snippets.

The TextExpander team suggest one snippet that randomly selects a closing salutation from a number of acceptable ones like Yours Truly, Sincerely and Peace Out. This is a great use for this method. For this tutorial, however, I’m using it for something a little different.

random fill in results
The results of running the random question fill in a number of times.

I’ve been trying to get better about keeping a journal, however, quite often nothing much has happened during the day that’s worth writing about. When that happens I don’t write, thereby breaking the habit. 

To overcome this, I created the following snippet that prompts me with a random thing to write about any time I need something.

Again, start by creating a new snippet and setting its Content to JavaScript. Assign it an abbreviation such as xjournal.

This snippet requires two parts, an array storing all the possible values and the logic to select one at random. First, I'll create the array. This is just the simplest method of declaring an array with JavaScript.

Copy and paste the code below into your snippet. Feel free to change the contents of the array to any collection of snippets you want to randomly select from.

With the array created, it’s time to randomly select from it. This requires three lines of code: one to count how many snippets there are, one to select a random number between that’s less than or equal to that, and one to select the snippet that falls at the random number’s position in the array.

Add the following three lines to your snippet to do just that.

Now when you enter the abbreviation, a random question will be returned.

Tip: The underscores make the questions italic when you write in a journaling app—such as Day One—that supports Markdown.

The Same Snippet With AppleScript

Although I prefer to use JavaScript because it works with TextExpander Touch, AppleScript also has its fans. Below is the code for the random journal question written in AppleScript. You can see it uses the exact same logic, the only difference is the language used.

Conclusion

In this tutorial I looked at using scripting languages to add simple logic to your snippets. While I’ve shown you how to have a snippet return different values either at random or depending on the day, that’s only the basics of what you can do with scripting languages and TextExpander. If you can write it in JavaScript, you can run it in TextExpander.

I’d love to hear how other people use scripting languages with TextExpander so, if you do, please let me know in the comments below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.