TextExpander: Clipboard Underlining

A while back I refreshed and expanded upon my TextExpander snippet groups and promised to start covering some of the inner workings of what the snippets actually do. Today I found a bit of time to make a start on that and I’m beginning with some of the snippets related to underlining.

I write in text files quite frequently and find myself wanting to underline text. I think it’s a habit that I developed when writing documentation via a green screen mainframe terminal - anything that helps to give plain text some sense of formatting can really help. I guess that’s partly the essence of markdown these days; which I also use frequently.

The basic concept for the snippets was to take whatever text was held on the clipboard and to output that text followed by a new line and a number of characters such that those characters underlined each character on the line above - assuming a fixed width font of course. My original snippets to do this were written in shell script, but with the introduction of JavaScript on the iOS side, this was a prime candidate to rewrite in JavaScript.

The core of the snippets is a function called Underline. The function takes a single parameter of a text string that is to be used as the underline. I personally only pass in a single character, but you could pass more than one to have say alternating characters. I haven’t included any snippets that do this (I always just use a single character), but you could write your own snippet to do this and the Underline function will handle it.

The Underline function is relatively straight forward. It begins by grabbing a copy of the clipboard content. It the checks how long it is and uses that to generate a copy of the underline string guaranteed to be long enough to underline the text (unless the parameter had zero length of course - which is no underline). The clipboard content, a new line and the underline trimmed to the correct length are then returned and output.

Label * Library - Underline (JS)
Description Helper function for clipboard text underlining
Abbreviation lib_ta_underline
Content

//This snippet is called by other snippets and should not be used on its own.
function Underline(p_strUnderlineWith)
{
    //Start with the clipboard
    var strOutput = TextExpander.pasteboardText;

    //Get the length of the clipboard content
    var intClipboardLength = strOutput.length;

    //Set-up an underlining string
    var strUnderline = "";

    //Now the underlining string for each character of the original clipboard content
    for (intCounter = 0; intCounter < intClipboardLength; intCounter++) { 
        strUnderline += p_strUnderlineWith;
    }

    //If we didn't have a single character underline string, we'll need to trim the underline string
    //Append a new line and the underlining string to the original clipboard content and return it
    return strOutput + "\n" + strUnderline.substring(0, intClipboardLength);
}

Rather than including this function along with every underline snippet I want to use, I can simply include it in other snippets - which means I only have one copy of the function to maintain. Here’s an example JavaScript snippet for how this all comes together. It would output the clipboard underlined by percentage symbols.

%snippet:lib_ta_underline%

Underline('%');

The first line simply includes the Underline function and the last line calls it using the percentage symbol as the underlining parameter. Even if you’re not familiar with JavaScript this should mean you could still roll your own custom underlines.

The Underline function snippet along with a number of underlining and othe snippets are available in the Thought Asylum - Text Snippets Group. Currently the underlining snippets included in the group are for underscore (_), hyphen (-), period (.), asterisk (*), hash (#), plus (+), equals (=), tilde(~) and there’s a bonus snippet that uses a TextExpander fill-in to allow you to enter any string you like (its set to be one character wide but you can type more if you wanted to).

I hope that you find those snippets useful and have some level of understanding about how they work. If you have any suggestions you would like me to consider for future snippets, please feel free to ping a suggestion to me over on Twitter or just follow me for updates on TextExpander snippet updates, posts and all manner of useful stuff.

Author: Stephen Millard
Tags: | textexpander |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read