Sample JavaScripts for TextExpander 5.0 / TextExpander touch 3.5

TextExpander 5.0 and TextExpander 3.5 add support for JavaScript snippets. JavaScript includes a very nice math library, so in addition to having snippets which run on both OS X and iOS, there are a number of snippets which are easier to write. Here are some examples of snippets you can make by setting the Content: menu of a new snippet to JavaScript, and pasting the following into the editor:

Calculate Body Mass Index (BMI)

// Body Mass Index (BMI) from height and weight
var height = Number(%filltext:name=m:default=1.64:width=5%);// meters
var weight = Number(%filltext:name=kg:default=57.5:width=5%);// kilograms
"Body mass index for subject " + height.toFixed(2) + "m tall weighing " +
	weight.toFixed(2) + "kg is: " + Number(weight / (height * height)).toFixed(2);

Compute Hourly Rate Total

// Compute a charge by multiplying hours * hourly rate
var hours = Number(%filltext:name=Hours:default=1.5:width=5%);
var hourlyrate = Number(%filltext:name=Rate:default=30:width=5%);
"Billing for " + hours + " hours at $" + hourlyrate + " for a total of $"
	+ (hours * hourlyrate);

JavaScript snippets can include fill-ins. Here’s a quick calculator which can evaluate any valid JavaScript expression:

Quick Calculator

// Quick Calculator -- enter a valid JavaScript expression
%filltext:name=equation:default=2 + 3%;

Here is a more elaborate fill-in example used to tally a survey:

Assess Depression on PHQ-9 Depression Scale

// Depression scale
TextExpander.appendOutput("PHQ-9 Depression scale\n" +
"In last 2 weeks, how often have you experienced:\n\n" +
"  (Score for each answer: 0-'not at all'  1-'several days' " +
"   2-'more than half the days'  3-'nearly every day')\n" +
"1- little interest or pleasure in activities? %fillpopup:name=1:default=0:1:2:3%\n" +
"2- feeling down, depressed, or hopeless? %fillpopup:name=2:default=0:1:2:3%\n" +
"3- trouble falling asleep, staying asleep, or sleeping too much? %fillpopup:name=3:default=0:1:2:3%\n" +
"4- feeling tired or having little energy? %fillpopup:name=4:default=0:1:2:3%\n" +
"5- poor appetite or overeating? %fillpopup:name=5:default=0:1:2:3%\n" +
"6- feeling bad about self, feeling failure, or let self or others down? %fillpopup:name=6:default=0:1:2:3%\n" +
"7- trouble concentrating, reading newspaper, or TV? %fillpopup:name=7:default=0:1:2:3%\n" +
"8- moving or speaking so slowly that other people could have noticed? Or the opposite, being so fidgety or restless that you have been moving around a lot more than usual? %fillpopup:name=8:default=0:1:2:3%\n" +
"9- thoughts that you would be better off dead or of hurting yourself in some way? %fillpopup:name=9:default=0:1:2:3%\n\n");

var count = 0;
var sum = 0;
for (count = 1; count <= 9; count++) {
    var fieldVal = Number(TextExpander.filledValues["" + count]);
    if (fieldVal != NaN) {
        sum += fieldVal;
    else break;

TextExpander.appendOutput("Total: " + sum + "\n" +
	"   Greater than 10 => major depression\n" +
	"   Greater than 20 => severe depression\n");

Have a favorite JavaScript? Send it in and include permission so we can share it.