TextExpander: Clipboard Snippets Using Javascript

Smile Software have just released a new update to their TextExpander product that brought a particularly interesting new feature. The option to run JavaScript based snippets. At first this may appear unremarkable given that the previous OS X snippets could take advantage of AppleScript, or shell script based options such as BASH, Perl and Python (to name just a few). The exciting thing to me however was that the JavaScript based snippets could also run on the iOS version; whereas AppleScript and shell script based snippets cannot.

As an initial foray into using JavaScript snippets on my iOS devices I decided I’d try recreating one of my existing snippet groups.

Processing the Clipboard

The group I decided to recreate was one of my most popular. It contains a number of snippets that output modified contents of the device’s clipboard. The original group included five snippets:

  1. Paste Clipboard Text (Lower case).
  2. Paste Clipboard Text (Sentence case).
  3. Paste Clipboard Text (Title/Proper case).
  4. Paste Clipboard Text (Upper case).
  5. Sort Lines Alphabetically.

You can read about the original snippets in the Change Clipboard Case post and the Sort Lines post.

As a bonus I actually also decided to add an additional snippet to provide a reverse alphabetic sort - it was pretty trivial to include.

Changing Case

JavaScript has quite a few useful functions for manipulating text and two of these relate to changing the case. To upper and lower. So reproducing at least two of the snippets proved to be very straight forward.

TextExpander provides a JavaScript object that has a property allowing the clipboard text to be retrieved. After that it is a simple matter of applying the upper case or lower case function.

The sentence and title/proper case snippets took a little more work. Each of these uses a function (set as a string prototype function) to modify the string it is passed. Each uses a regular expression to determine how to chunk up the string of text for capitalising. Both work on the same principle; we want to capitalise the first letter of each chunk. For sentence case we want to chunk into sentences and for title/proper case we want to do chunk into words. The regular expressions are therefore relatively similar in approach. For each chunk returned, the remainder of the function is the same. Set the first character to be a capital letter and the remaining characters to be lower case letters.

Sorting Lines

As well as having upper and lower case functions and regular expressions, JavaScript also has a sort function for arrays. Therefore to sort lines of text I simply needed to split the string by line into an array, sort the array and then concatenate the array back into a string using new lines as a separator.

Since JavaScript also had a reverse function just sitting there I also created a reverse sort snippet by using the same process as the sort above, but immediately after sorting the array I reverse its order.

With the exception of figuring out the regular expressions to use (it usually takes me a little while to get them right) using JavaScript for the snippets was actually really quick and simple. The ability to use them on my iOS devices though is the key gain here for me. I already had these snippets, but the ability to use them cross platform is hugely valuable.

Get the Snippet Group

The snippet group containing these snippets can be downloaded from the link below and installed into your own copy of TextExpander. You may also want to check out my earlier post on how to import and export snippet groups in TextExpander.

The Snippets

Label Convert Case to Lower [Clipboard] (JS)
Description Output the clipboard contents in all lower case.
Abbreviation $cblow
Content
TextExpander.pasteboardText.toLowerCase( );
Label Convert Case to Upper [Clipboard] (JS)
Description Output the clipboard contents in all upper case.
Abbreviation $cbup
Content
TextExpander.pasteboardText.toUpperCase( );
Label Convert Case to Sentence [Clipboard] (JS)
Description Output the clipboard contents where every character is lower case except for the start of the sentence which is in upper case.
Abbreviation $cbsent
Content
String.prototype.toProperCase = function( ) {
    return this.replace(/((?:\S[^\.\?\!]*)[\.\?\!]*)/g, function(strInput){return strInput.charAt(0).toUpperCase( ) + strInput.substr(1).toLowerCase( );});
};

TextExpander.pasteboardText.toProperCase( );
Label Title
Description Output the clipboard contents where every character is lower case except for the start of each word which is in upper case.
Abbreviation $cbtitle
Content
String.prototype.toProperCase = function( ) {
    return this.replace(/([^\W_]+[^\s-]*) */g, function(strInput){return strInput.charAt(0).toUpperCase( ) + strInput.substr(1).toLowerCase( );});
};

TextExpander.pasteboardText.toProperCase( );
Label Sort Lines Alphabetically [Clipboard] (JS)
Description Output the lines of text on the clipboard sorted into alphabetic order.
Abbreviation $cbsortalpha
Content
TextExpander.pasteboardText.split("\n").sort( ).join("\n");
Label Sort Lines Reverse Alphabetically [Clipboard] (JS)
Description Output the lines of text on the clipboard sorted into reverse alphabetic order.
Abbreviation $cbsortrevalpha
Content
TextExpander.pasteboardText.split("\n").sort( ).reverse( ).join("\n");

Final Tips

A couple of quick tips for anyone else tinkering with JavaScript snippets directly on their iOS device.

If you want to set a snippet as JavaScript, long press in the edit area of the snippet and in the pop-up menu you’ll see the option to set it as JavaScript.

Check out the help for details on the TextExpander JavaScript object.

Author: Stephen Millard
Tags: | javascript | mac | textexpander | ios | ipad | iphone |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read