How to Send Random Responses with TextExpander

Do you ever find that you’re sending the exact same messages over and over? Maybe this bothers you and you want to mix things up, but you don’t have the time to come up with witty and creative responses to every message. TextExpander can help you automatically send random responses to spice things up.

Recently, we were talking to David Sparks about his session at our Virtual Summit, and he asked if it’s possible to create a TextExpander Snippet that pulls random responses every time you trigger it. Specifically, he wanted to create a Snippet to nicely tell people no, giving a different variation of the response each time.

Turns out, yes it is possible, and we’ll explain how to do it. But if you want to try out the Snippet yourself, install MacSparky’s Random No Snippets Public Group. There are many supporting Snippets in the group, but the key one is no.random. Once the Snippet Group is installed, typing no.random expands into one of six phrases:

“I am about to begin a work of great importance and therefore beg that in reference to calls and correspondence you will consider me dead for the next two months.”

“I am unable to do as you ask.”

“I can not, for secret reasons.”

“I greatly regret that I cannot do what you so kindly suggest.”

“I would prefer not to.”

And then finally, we have this lengthy polite decline from Mark Twain:

“I hope I shall not offend you; I shall certainly say nothing with the intention to offend you. I must explain myself, however, and I will do it as kindly as I can. What you ask me to do, I am asked to do as often as one-half dozen times a week. Three hundred requests a year! One’s impulse is to freely consent, but one’s time and necessary occupations will not permit it. There is no way but to decline in all cases, making no exceptions, and I wish to call your attention to a thing which has probably not occurred to you, and that is this: that no man takes pleasure in exercising his trade as a pastime. Writing is my trade, and I exercise it only when I am obliged to. You might make your request of a doctor, or a builder, or a sculptor, and there would be no impropriety in it, but if you asked either of those for a specimen of his trade, his handiwork, he would be justified in rising to a point of order. It would never be fair to ask a doctor for one of his corpses to remember him by.”

These might be a little silly, but having a Snippet that can produce semi-random replies can be helpful for many fields, like customer service and sales, where you have to send out the same messages over and over again but want to add a bit of variation so you and your team don’t sound like robots.

Let’s explain how we did it, and then we’ll show you how to customize it for your own purposes, even if you don’t know how to code.

JavaScript to the Rescue

To create a Snippet that expands into random responses, we turn to TextExpander’s often-overlooked JavaScript functionality, which lets you perform advanced operations like this. The no.random Snippet uses JavaScript to call another Snippet—entriesForRandom, which is a list of other Snippets that are possible choices for the randomizer.

The script counts the number of possible Snippets to expand, generates a random number, and expands the Snippet corresponding to that number.

try {
// Check if entriesForRandom snippet exists.
eval('%snippet:entriesForRandom%');
// Get count of random entries
const count = entriesForRandom.length;
// Pick random index
const randomIndex = Math.floor(Math.random() * count);
// Return entry with randomly chosen index
entriesForRandom[randomIndex];
}catch (err) {
// Post helpful error if entriesForRandom does not exist
"Please create a snippet with Content: JavaScript, " +
"abbreviation 'entriesForRandom', and snippet content of:\n" +
"\tconst entriesForRandom = [\n"+
"\t\t\"one\",\n" +
"\t\t\"two\",\n" +
"\t\t\"three\",\n" +
"\t\t\"four\",\n" +
"\t\t];\n";
}

If you ever create your own Snippet group with this code, or any JavaScript code, be sure to set the Content Type to JavaScript so TextExpander understands that it’s working with code.

As we mentioned, this Snippet calls the entriesForRandom Snippet to get a list of possible Snippets to expand into:

var entriesForRandom = [
"%snippet:no.work%",
"%snippet:no.unable%",
"%snippet:no.secret%",
"%snippet:no.regret%",
"%snippet:no.long%",
"%snippet:no.prefer%",
];

Again, Content Type is set to JavaScript. This little bit of code creates an array of Snippets, a programming term for a list, and assigns that array to the variable entriesForRandom.

With a basic understanding of how the Snippet works, let’s walk through how you can make a copy of the Snippet Group and customize it to generate your own random responses.

Customize “Random No” for Your Own Random Responses

First, you must install the MacSparky’s Random No Snippets Public Group. Once it’s loaded in TextExpander, find it in the sidebar, right-click it (or Command-click on a Mac), and choose Duplicate from the menu. This creates an exact copy in your Personal library.

Next, find “MacSparky’s Random No Snippets copy” in the sidebar. Let’s rename it by right-clicking the Group name and choosing Group Settings. In the Name field, select all the text and replace it. For this example, we’re going to create a “Random Yes” Snippet Group.

Now go into the Snippet Group, select every Snippet other than no.random and entriesForRandom and delete it. Shift-click is handy here: click the first Snippet in the Group, and then Shift-click the no.prefer Snippet. Then press Backspace or Delete and click Delete Snippets.

Next, let’s rename the David Sparks Random Nos Snippet and give it a new abbreviation. In this case, we named our Snippet Random Yes with an abbreviation of yes.random.

Then in the group, create the Snippets to feed into the randomizer. Here are the abbreviations and what they expand into:

yes.love: “I’d love to!”

yes.happy: “Happy to do so!”

yes.glad: “I’d be glad to!”

Now we need to modify the entriesForRandom Snippet to tailor it to our new Snippet Group.

Change the Label and Abbreviation to something unique. In our example, we renamed it to entriesForYesRandom.

Go into the Snippet content and change entriesForRandom to the new Snippet name.

Change the entries in the array to represent the new Snippets. Each Snippet needs an entry in this format: “%snippet:SNIPPET_NAME%”

One last step: edit the randomizer Snippet—yes.random in our example— to change every reference to entriesForRandom to our new random entries Snippet—entriesForYesRandom in our example.

Now, you should be able to try out your new random Snippet and have it work immediately! If it doesn’t, these are the most common causes:

Make sure you replaced all references to entriesForRandom and spelled the new Snippet correctly.

Be sure you formatted the entriesForRandom Snippet—or whatever you replaced it with—correctly. See the screenshots for examples.

You can use this technique for all sorts of random responses: random greetings, sign-offs, sales pitches, or queries for information.

Here are some more top TextExpander tips:

TextExpander Best Practices: 5 Customer Success Secrets

TextExpander Best Practices: Create and Document Snippets

Degree Symbol: Type It Easily with TextExpander