How To: Make Cross-Platform Random TextExpander Snippets

Randomize Snippets

You can set up a snippet to expand randomly selected content on Mac, Windows **, iPad, and iPhone using JavaScript.

Some of you are already planning how to use this to make your life easier.

If you’re wondering what pedestrian use this can have, suppose you answer a lot of email, a pleasant greeting and closing statement are standard. Now you can randomize the “Thanks for writing”s “Great to hear from you”s and “Hope things are going well”s without the effort of picking in the moment. Just come up with an array of sentences to use, and the randomization is taken care of. Nest your random greetings snippet in your normal email snippet templates, and you’re set.

Make a snippet for randomizing content

First, you’ll set up the snippet to make the random choice:

  1. Click New Snippet (or tap + in the Snippets view on iOS)
  2. Set the “Content:” field to “JavaScript” to create a JavaScript snippet
  3. Set the “Abbreviation:” field to “jsrandom,” or another abbreviation of your choosing
  4. Copy and paste the following as the snippet content:
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";
}

Next, you’ll set up the potential choices.

  1. Click New Snippet (or tap + in the Snippets view on iOS)
  2. Set the “Content:” field to “JavaScript” to create a JavaScript snippet
  3. Set the “Abbreviation:” field to “entriesForRandom”
  4. Copy and paste the following as the snippet content, replacing “one,” “two,” “three,” and “four” with whatever content you would like to appear at random:
const entriesForRandom = [
    "one",
    "two",
    "three",
    "four",
    ];

Alternatively, let’s say you already have snippets with abbreviations “random1”, “random2”, “random3”, and “random4”. In that case, set the snippet content for the “entriesForRandom” snippet to:

const entriesForRandom = [
    "%snippet:random1%",
    "%snippet:random2%",
    "%snippet:random3%",
    "%snippet:random4%",
    ];

Feel free to substitute the snippet abbreviations above with abbreviations of your existing snippets. For example, replace “random1” with “myAbbreviation” and “random2” with “myOtherAbbreviation”. Likewise, feel free to add additional lines to the entriesForRandom array.

Do you have a TextExpander tip of your own? Share it with us, and we can link to or re-publish it here.

(** TextExpander for Windows requires a TextExpander subscription.)