TextExpander logo TextExpander Smile logo
 

Help: Script Snippets

AppleScript Snippets

In the content format drop-down menu, choose "AppleScript" as the format type. In the content field, enter an AppleScript such as this one:

-- Adjust greetingsList values to your preferred greeting
set greetingsList to {"Sincerely", "Yours Truly", "Peace Out", "Have a good day", "kthxbai"}
set listLength to count greetingsList
set randomNumber to (random number from 1 to listLength)
set selectedGreeting to item randomNumber of greetingsList
return selectedGreeting

The script executes in the context of the TextExpander application. The script can perform various actions such as controlling or requesting information from other applications, but the snippet will expand to whatever text is returned. The script should not use display dialog or display alert because all keystrokes are buffered while the snippet is expanding.

Shell Script Snippets

In the content format drop-down menu, choose "Shell Script" as the format type. In the content field, enter a shell script such as this one:

#!/usr/bin/perl
print "Hello from Perl!\n";

Your shell script must have the “shebang” (#!) on its first line and that must specify the interpreter to be used for your script.

Here is an example of a Unix shell script

#!/bin/bash
echo "Hello from Bash!"

The snippet will expand to the standard output of the shell script.

Shell scripts inherit TextExpander’s environment, so no shell configuration or profile, such as .bashrc / .bash_profile, is executed. TextExpander sets LANG to [current locale].UTF-8 before executing a shell script. The result of a shell script must be encoded in UTF-8.

JavaScript Snippets

In the content format drop-down menu, choose "JavaScript" as the format type. In the content field, enter a JavaScript such as the example below.

In the content field, you may enter:

  • Plain old JavaScript, as you might write for a web browser, in an environment provided by TextExpander. This script can also operate in TextExpander touch for iOS.
  • JavaScript for Automation (JSA) which is a superset of JavaScript and can be used to automate OS X applications, but it cannot share context with other embedded script snippets, and the expansion text is always the value of the last statement executed in the script. It cannot operate in TextExpander touch for iOS.

TextExpander will automatically detect JSA because almost all JSA scripts must contain some specific keywords. If needed, force the script to execute in JavaScript for Automation by setting the first line to: //JSA or // JSA, (a comment with or without a Space to start).

Here is an example of plain old JavaScript:

 a = "Hello, ";
 b = "world!";
 a + b;
 

Here is an example of JavaScript for Automation:

 Mail = Application('Mail');
 firstMessage = Mail.inbox.messages[0];
 firstMessage.subject();
 

In both cases, the snippet expands to the content of the last expression.

The TextExpander environment includes a global TextExpander variable with the following properties:

appendOutput()
This function allows the script to build up the text to be expanded, otherwise the expansion will default to the value of the last statement executed in the script (see also TextExpander.ignoreOutput)
ignoreOutput
A boolean flag the script can set indicating it will return no text for expansion. Useful in the case where the script only has some side effect. Default value is false.
triggeringAbbreviation
A string which tells what abbreviation triggered the expansion of the script. The triggering abbreviation may not be the abbreviation of this snippet if the snippet was embedded as discussed below. The abbreviation may be empty if the snippet was expanded via the TextExpander menu.
baseDate
A Date holding the time the snippet is being expanded.
adjustedDate
Initially holds the same Date as TextExpander.baseDate, but may hold a different time if a snippet embedding this one used a Date Math macro. The script can modify TextExpander.adjustedDate, then that date will be used for subsequent date and time macros.
pasteboardText
A string with the text contents of the pasteboard. The script can modify this to place the specified text on the pasteboard after the expansion is completed.
expansionContext
A string with the bundle identifier of the application where the snippet is being expanded. May not always be available. Will be "com.textexpander.preview" when previewing the snippets, or when creating text to be Copied or Shared.
filledValues
Holds fill-in field values as properties, or nil when snippet is not a fill-in. Text fields, text areas, and popup menus have string values, conditional parts have values 1 or 0. Values are accessed via their field name. If no field name has been specified, a default name is built with the format string "Variable %d".

If JavaScript snippets are embedded as discussed below, all the scripts share the same context, so global variables created in one snippet are available in following snippets.

An example TextExpander environment JavaScript snippet, which only adjusts the date used for date macros to four non-weekend days from the current date:

// Find the first non-weekend day four days from now
var p4bd = new Date();
p4bd.setDate(TextExpander.baseDate.getDate() + 4); // add 4 days to current
// skip Saturdays and Sundays
while (p4bd.getDay() == 6 || p4bd.getDay() == 0) {
    p4bd.setDate(p4bd.getDate() + 1);
}
TextExpander.adjustedDate = p4bd; // adjust for all snippets in this expansion
TextExpander.ignoreOutput = true;

Nesting Script Snippets

Script snippets can be nested within text snippets using the nested snippet macro %snippet:[snippet name]%.

Example: if the AppleScript snippet above had the abbreviation "eend", and the date-adjusting JavaScript had the abbreviation "+4nwd", you could use them both in a text snippet:

Dear Reader,

Thank you for your feedback on my book. I will analyze your question in depth and get back to you %snippet:+4nwd%on or before %A, %B %e.

%snippet:eend%,

- Author

which would expand to:

Dear Reader,

Thank you for your feedback on my book. I will analyze your question in depth and get back to you on or before Monday, March 23.

Have a good day,

- Author

The promised date is four non-weekend days in the future, and the signature is one of the random choices in greetingsList.

Feel free to share your cool scripts with us.


For information on other macros used in TextExpander, see Date, Time, and Math Macros and Advanced Snippet Functions: Macros.


 
 
© 2006-2015 SmileOnMyMac, LLC, dba Smile. All rights reserved.
TextExpander is a registered trademark, Smile and the Smile logo are trademarks of SmileOnMyMac, LLC, dba Smile.