Automate Expansion of Strings in TextExpander

Automate
Photo by Philip Estrada on Unsplash

TextExpander for macOS 6.2.2 adds a new scripting command: “expand string” to allow automating the expansion of text strings. Here’s an example of how you might use it in AppleScript:

tell application "TextExpander" to expand string "ddate :flag-us:"

The result: “August 21, 2017 🇺🇸”

Note: The example assumes you have the Emoji Cheat Sheet Public Group installed.

We think exposing expansion to automation will allow using TextExpander abbreviations for writing in shorthand, using text filters to perform expansion.

For example, the following AppleScript works as a BBEdit Text Filter:

on RunFromBBEdit(fromBBEdit)
    set unexpandedText to fromBBEdit as string
    tell application "TextExpander"
        set expandedText to expand string unexpandedText
    end tell
    return expandedText
end RunFromBBEdit

To see for yourself:

  1. Launch Script Editor
  2. Copy the above script into Script Editor
  3. Save the file to: [Home]/Library/Application Support/BBEdit/Text Filters

I named mine “Expand Using TextExpander”.

If I have a BBEdit document open with:

Tx rocks!

…then I choose Text -> Expand Using TextExpander, I get…

TextExpander rocks!

You do have to have a snippet with an abbreviation of Tx and content of TextExpander for that to work, but I think you get the idea.

The new “expand string” command supports nested snippets. It does not support fill-ins. Feel free to send us a request and a sample use case if that’s something you’d like to see. Likewise, if you have a cool use for this, let us know.