in Automation

Managing lists with Textexpander

TextExpander is a great utility I use on a daily basis to manage text snippets on my Mac and, thanks to the builtin synchronization mechanism, my iOS devices.

The last version number 5, launched in May, allows you to run on both OsX and iOS Javascript scripts inside snippets: it means that using TextExpander custom keyboard, you can run Javascript from within every application on every supported device.

In this post I will present you few snippets I use more on Mac and on the iPad, and some trick to implement more of the same type.

The idea at the base of all the snippets is to do some transformation with a list rapresented by a multiline text copied in the clipboard.

List to insert

This first snippet becomes handy when you have a list you want to use in a SQL (for example a SELECT or an INSERT).

Generally you have to take each item of the list (or row of the text if you prefer), add one single quote at the beginning, one at the end, plus a comma for each line and open/close brackets before and after the text.

Then you can use the transformed list in a SELECT … WHERE x IN {LIST} or INSERT … VALUES {LIST}.

What you have to do with TextExpander is to write a snippet like this:

List to insert

List to insert

Here is the code if you need to copy it, and at the end of the article I will link the exported TextExpander group with all the macros.

var out = "";
var row = "";
var rows = TextExpander.pasteboardText.toLowerCase().split('\n');
for (x=0;x<rows.length;x++) {
    row = rows[x].trim();
    (row!="") ? out += "'"+row+"',\n": "";
}
out = "(\n"+out.substring(0, out.length - 2)+"\n)\n";
TextExpander.appendOutput(out);

The script is very simple and, beside transforming each lines, it also remove blank lines and trim spaces.

If you want you can modify the script by using for example .isNaN method to check if the value is numeric or not and then use or no quotes but I don’t like this idea because very often I put numeric values in text fields, so I prefer the script to output always with quotes and maybe then delete them manually.

Remember when you use .isNaN that it returns true even if you specify a number within quotes due to Javascript implicit casting.

List to text

This script simply transform a multi line text in a “one line” text by removing the “\n” char at the end of each line and using “,” as a value separator.

var out = "";
var row = "";
var rows = TextExpander.pasteboardText.toLowerCase().split('\n');
for (x=0;x<rows.length;x++) {
    row = rows[x].trim();
    (row!="") ? out += row+", ": "";
}
out = out.substring(0, out.length - 2);
TextExpander.appendOutput(out);

Remove chars left/right

Sometimes it happens that you need to remove the first of the last X character for each line of a list.

Doing that is super-easy with TextExpander but if you want to write a snippets capable of running on OsX and iOS, unfortunately you need to write a snippet for each value of X: infact TextExpander for OsX does not support text fill-ins, so you cannot pass a parameter to the script.

But an interesting trick could be rewrite the snippet in a way it gets the number of chars to delete from the last character of the abbreviation: doing that you can’t skip the problem of creating multiple snippets, but you can copy always the same code changing only the abbreviation according to your needs (e.g. ‘,,l1′,’,,l2′,’,l5′).

So here’s the cod for removing N chars (depending by the abbreviation) on the right…

var out = "";
var row = "";
var rows = TextExpander.pasteboardText.split('\n');
var trg = TextExpander.triggeringAbbreviation;
var charToDelete = trg.substring(3,4)
for (x=0;x<rows.length;x++) {
    row = rows[x].trim();
    out  += row.substring(charToDelete, row.length)+"\n";
}
TextExpander.appendOutput(out);

… and the one to remove N chars on the left:

var out = "";
var row = "";
var rows = TextExpander.pasteboardText.split('\n');
var trg = TextExpander.triggeringAbbreviation;
var charToDelete = trg.substring(3,4)
for (x=0;x<rows.length;x++) {
    row = rows[x].trim();
    out  += row.substring(0, row.length-charToDelete)+"\n";
}
TextExpander.appendOutput(out);

As told before, if you don’t mind to keep the compatibility with iOS, you can use a fill-in in your snippet to specify the number of chars you want to remove at “expanding-time”, but it’s out of scope for this post.

Enumerate (prepend each line)

Last script enumerate each line of a list starting from zero. As you can see it’s super easy:

var out = "";
var row = "";
var rows = TextExpander.pasteboardText.split('\n');
for (x=0;x<rows.length;x++) {
    row = rows[x].trim();
    out  += x + " - " + row +"\n";
}
TextExpander.appendOutput(out);

Download the group

Managing lists with TextExpander on OsX and iOS let you save time by cutting off repetitive tasks.

In this article I presented some snippets I found useful for my daily job, but I’m sure that many other could be written on more specific needs.

If you want to download all these snippets and import them in TextExpander directly, you can follow Download snippets group.