TextExpander Adds JavaScript Support

Philip Blog, TextExpander, TextExpander Tips

TextExpander 5 and TextExpander touch 3.5 now include support for standard JavaScript snippets, and TextExpander 5 also includes support for JavaScript for Automation on OS X.

As a basic example, a scientist with a frequent need for an approximation of Pi to eight digits of precision could set up a JavaScript snippet with abbreviation zpi and content:


Any time the user types zpi, it will expand to: 3.14159265.

Here is a more complex example. It's a TextExpander fill-in snippet which tells the user their zodiac sign:

zodiacSigns = ['monkey', 'rooster', 'dog', 'pig', 'rat', 'ox',
                'tiger', 'rabbit', 'dragon', 'snake', 'horse', 'goat'];
var index = %filltext:name=Enter your birth year:default=1984% % 12;
"Your Zodiac sign is the " + zodiacSigns[index] + ".\n";
// %filltop%

When expanded, the user is prompted for their birth date, and the snippet returns their zodiac sign. For example, if the user enters 1972, the snippet expands to “Your Zodiac sign is the rat.”

Did you notice %filltop%? That tells TextExpander to duplicate any single line and popup fields at the top of the fill-in window and hide the script. You'll find that via the insert menu under Fill-ins > Show at top.

Content Object

TextExpander includes a JavaScript context object, which exposes the following items you might find useful. Precede these with TextExpander, for example, TextExpander.appendOutput(“Hello, world!”).triggeringAbbreviation – the abbreviation which triggered the expansion [boolean, read/write] ignoreOutput – do not use the final statement as the expansion [boolean, read/write] appendOutput(text) – add text to the expansion [function] baseDate – date and time at which the snippet is expanded [date, read only] adjustedDate – date and time used to expand the snippet [date, read/write] pasteboardText – clipboard contents [string, read/write] expansionContext – bundle ID within which snippet is being expanded [string, read only; can be nil] filledValues – fill-in field values [associative array of strings, read/write]

JavaScript for iOS and OS X

You can take advantage of JavaScript support on both OS X and iOS to create snippets which run on both platforms — something you can't do with AppleScripts and Shell Scripts. You can write a snippet to make the clipboard content lowercase in JavaScript like this:


JavaScript for Automation

On OS X, you can use JavaScript for Automation (JSA) as an alternative to AppleScript. This form of JavaScript will not function in iOS. TextExpander should automatically detect JSA, or you can force JavaScript to be treated as JSA by starting it with a comment: //JSAHere is a JSA script to create a new message, set its subject, and set its content:

Mail = Application('Mail');
message = Mail.OutgoingMessage().make()
message.subject = "New Message";
message.content = "This is a new message created via JavaScript."

You might enjoy this article from MacStories on Getting Started with JavaScript for Automation.If you have a favorite JavaScript, please send it in, and let us know if we can share it with the world.