We regularly get requests for a way to choose a random snippet from among a set of snippets. To date, we’ve pointed folks to this technique:
Now that TextExpander itself is AppleScriptable (as of version 3.3), there’s an easier and more flexible way to do this:
- Choose File -> New Group
- Name the new group Random
- Add snippets that you’d like to be chosen at random into the Random group. You can drag existing snippets from other groups into Random, or you can create snippets expressly for use in Random.
Tip: if you are creating new snippets for use only as random snippets, you can save time and work by not assigning abbreviations to these snippets.
- Make a new snippet in a group other than the Random group
- Set the Content: of the new snippet to AppleScript
- Enter this for the snippet:
tell application "TextExpander" set groupCount to count (snippets of group "Random") set randomIndex to random number from 1 to groupCount return plain text expansion of snippet randomIndex of group "Random" end tell
- Set an abbreviation for this new snippet, for example: rrand
Any time you type rrand, you’ll get a random selection from among the snippets in your Random group.
The advantages of this technique over the one linked above are:
- You need not know the abbreviations for all of the snippets in advance
- You can add to and remove from your Random group without changing anything
(Thanks to Sheree Peña of Black Pixel for the inspiration for this post.)