TextExpander: Merge Text to a Single Line

It’s the little things that really irritate me. I’ve just got one of those personalities I guess. I can’t help myself - if there’s an itch I always have to find a way to scratch it. One of these itches for me is copying and pasting text out of PDFs and getting unwanted line breaks in the resulting plain text. I decided to address this irritation with a little bit of text manipulation.

Whilst the text may be continuous originally when formatting the text for layout in a PDF hard line breaks often seem to be added and whilst my approach is not perfect it scratches enough of the itch for me to make it more bearable. The basic assumption on my part is that I don’t want any line breaks in the text being pasted or at the very least it will be mush less effort to add a handful of desired line breaks back in rather than take a large number of line breaks out.

My tool of choice for this is Smile Software’s TextExpander. This is a text tool typically used for auto correction and insertion of boilerplate text. However TextExpander is much more powerful than that when you combine it with some of its scripting functionality.

I’ve used the JavaScript scripting option as this makes it compatible with both the Mac and iOS devices using TextExpander. Hopefully it won’t be too long before the Windows TextExpander application also supports JavaScript and then I’ll hit my device trifecta … though TextExpander for my work phone (Android) would also be welcomed.

The snippet (the name for any set of text or expansion instructions in TextExpander) takes the operating system clipboard content and substitutes any new lines (carriage return and line feed or just carriage return) for a space. Then in the resulting text and double spaces are replaced with single spaces. That space consideration may be something to keep in mind if there’s any space based formatting such as code indentation. In addition the JavaScript also trims off any blank space at the end of the line but this is much less likely to cause any issues.

Label Merge to One Line [Clipboard]
Description Take multiple lines of text on the clipboard and merge to a single line.
Abbreviation $cbm21l
//Get the clipboard, replace new lines with a space, replace double space with a single space and then trim any whitespace from the ends TextExpander.pasteboardText.replace(/\r?\n|\r/g, " ").replace("  ", " ").trim();

You may need to select, copy and paste the snippet lines above to view them properly as they are fairly long.

When I originally started looking at this I used the %clipboard token to pull in the clipboard content but this has an immediate limitation in that when the text is multiline (which it always is with what I’m trying to process) this will break the JavaScript as TextExpander inserts the clipboard content into the JavaScript. Two minutes reading the JavaScript documentation on Smile’s web site got me some information about the pasteboardText property of the TextExpander object and this sorted my issue instantly as the multiline content is then held in memory rather than being inserted into the JavaScript itself.

Once you realise that you can start manipulating multiline clipboard content in TextExpander using JavaScript this opens up a wide range of options for pre-processing content in more complex ways. The irony is I guess that in this case I’m using TextExpander for text contraction.

Author: Stephen Millard
Tags: | textexpander |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read